如何删除热图中的 y 轴并将年份水平放置

How to remove y-axis in a heat map and put the years horizontally

我正在绘制热图,我的 y 轴有两个值。相应的垂直年份是可以的(我也想将它们水平放置)但默认情况下这些年份以我无法摆脱的方式出现。我怎样才能删除它们并将剩余的年份水平放置?

这是我得到的情节和下面的代码

  # Convert months to factors to re-order them, otherwise they will be sorted alphabetically

  rates_fed$month <- factor(rates_fed$month, levels=c("Jan", "Feb", "Mar", "Apr", "May", "June", 
    "July", "Aug", "Sept", "Oct", "Nov", "Dec"))

  # Plot the data

  rates_fed %>% 
  group_by(year) %>% 
  ggplot(aes(rate_fed, x = month, y = year)) +
  geom_tile(aes(fill = rate_fed, width = 3, height = 3)) +
  geom_text(aes(label = rate_fed), color = "white", size = 4) + 
  scale_fill_viridis_c("rate", option = "D", limits=c(0.05, 5.5)) + 
  facet_grid(year~month, scales = "free", space = "fixed", switch = "y")+
  theme_minimal(base_family = "mono")+
  theme(panel.grid = element_blank(), 
        axis.text.x = element_text(size = 11), # change the size according to the viz
        axis.text.y = element_text(size = 15), # change the size according to the viz
        axis.title.x = element_text(size = 15), # change the size according to the viz
        axis.title.y = element_text(size = 15), # change the size according to the viz
        plot.title = element_text(size = 15)) + # change the size according to the viz
  labs(x = "month", y = "year",
       title = "Monthly Average Federal Reserve Interest Rates", 
       caption = "Data: Federal Reserve of St. Louis Bank Data")

看起来你可以简单地修改主题:

...+theme(axis.text.y = element_blank())

我没有您的数据,因此无法对此进行测试。请注意,您可以使用 dput 将数据转换为可以 copy/paste.

的格式

在我看来,您可能不需要为 facet_grid 操心。就您的目的而言,geom_tile 似乎就足够了。

要旋转文本元素,您可以使用以下方法将其旋转一定度数:

...+theme(axis.text.y = element_text(angle = 90))

希望对您有所帮助。