在ggplot2()中使用geom_tile的热图 - 寻找旧代码的等价物

时间:2017-11-15 15:32:24

标签: r plot ggplot2

我有一个包含三个变量的数据框:location,weather和wc。

我想在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"))

1 个答案:

答案 0 :(得分:0)

以下是更新版本:

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()

enter image description here