【问题标题】:How to remove y-axis in a heat map and put the years horizontally如何在热图中删除 y 轴并将年份水平放置
【发布时间】:2019-07-16 15:25:00
【问题描述】:

我正在绘制一张热图,我的 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(), strip.text.y = element_text(angle = 90))这样的吗?

标签: r ggplot2 data-visualization heatmap


【解决方案1】:

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

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

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

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

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

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

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    相关资源
    最近更新 更多