【问题标题】:heatmap using geom_tile in ggplot2() - looking for the equivalent of older code在 ggplot2() 中使用 geom_tile 的热图 - 寻找旧代码的等价物
【发布时间】:2017-11-15 15:32:23
【问题描述】:

我有一个包含三个变量的数据框:位置、天气和厕所。

我想在 ggplot2() 中使用 geom_tile 制作热图,这样天气在 y 轴上,位置在 x 轴上,wc 是填充。我在https://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/ 上找到了一些代码,产生了我正在寻找的那种情节。

问题在于,虽然我可以制作基本情节 (p),但我无法获得最后润色的代码(网页上有最终产品的视觉效果)。据我所知,其中一些代码适用于旧版本的 ggplot2(),但我无法弄清楚更新后的等价物是什么。

任何帮助将不胜感激。

起点(df):

df1 <- data.frame(location=c("az","az","az","bi","bi","ca","ca","ca"),weather=c(1,2,3,2,3,1,2,3),wc=c(2,1,1,2,1,2,2,1))

当前代码:

p <- ggplot(df1, aes(location,weather)) + geom_tile(aes(fill = wc),colour = "white") +
 scale_fill_gradient(low = "white", high = "steelblue")

p + theme_grey(base_size = base_size) + labs(x = "", y = "") + 
    scale_x_discrete(expand = c(0, 0)) + scale_y_discrete(expand = c(0, 0)) + 
    opts(legend.position = "none", axis.ticks = theme_blank(), axis.text.x = theme_text(size = base_size * 0.8, angle = 330, hjust = 0, colour = "grey50"))

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    这是一个更新的版本:

    base_size <- 9
    ggplot(df1, aes(location,weather)) + geom_tile(aes(fill = wc),colour = "white") +
        scale_fill_gradient(low = "white", high = "steelblue") +
        theme_grey(base_size = base_size) + labs(x = "", y = "") + 
        scale_x_discrete(expand = c(0, 0)) + scale_y_discrete(expand = c(0, 0)) + 
        theme(legend.position = "none",
              axis.ticks = element_blank(),
              axis.text.x = element_text(size = base_size * 0.8,
                                          angle = 330,
                                          hjust = 0,
                                          colour = "grey50"))  
    

    opts 下的所有参数都应该在theme

    theme_text() 替换为 element_text()
    theme_blank() 替换为 element_blank()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多