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

时间:2019-07-16 15:25:00

标签: r ggplot2 data-visualization heatmap

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

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

enter image description here

  # 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")

1 个答案:

答案 0 :(得分:2)

您似乎可以简单地修改主题:

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

由于我没有您的数据,因此无法测试。请注意,您可以使用dput将数据转换为可以复制/粘贴的格式。

在我看来,您可能不需要打扰facet_grid。出于您的目的,geom_tile似乎就足够了。

要旋转文本元素,可以使用以下命令将其旋转一定程度:

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

我希望有帮助。