使用x和y轴标签创建第三个变量的图?

时间:2014-02-04 06:07:26

标签: r plot graphic

我正在尝试绘制与此graphic类似的内容,请参见第34页第23页。

enter image description here

似乎很简单,但我无法通过在线搜索找到具体的例子。如果有人可以提供帮助,我会非常感激。

1 个答案:

答案 0 :(得分:2)

这个怎么样:

library(ggplot2)

df<-expand.grid(x=LETTERS,y=1:20)
df$var<-runif(nrow(df))    

ggplot(df[sample(1:nrow(df),200),]) + theme_bw() + # subset of df to include blanks
  geom_tile(aes(x=x,y=y,fill=var)) + #geom_tile
  scale_fill_gradient2(low="green",mid="yellow",high="red",midpoint=0.5) + # add fill gradient
  scale_y_discrete(breaks=1:20,labels=1:20) +
  coord_fixed(ylim=c(0.5,20.5))

你可以修改scale_fill...函数来实现你想要的任何布局,基于var(连续或离散)或一些规则。

enter image description here