R ggplot - geom_point图例

时间:2015-08-07 04:57:10

标签: r ggplot2

我是新来的,我有这个传奇问题,我真的需要帮助 - 在网上找不到类似的问题。

我正在制作热图,以展示我的数据如何随着两个实验参数的变化而变化。我用一个颜色条来解释热图本身。我还在geom_point钻石中加入了显示实际数据点的位置,这些钻石的大小由任何给定数据点的标准误差决定。现在我希望为我的各种实验标准化的是标准错误图例a),其中可以固定不同大小的钻石的数量(例如,4种不同的尺寸),以及b)它们的相应值可以是固定的(例如最大的)钻石对应的值为0.02)。

我怀疑我需要改变底部附近的引导线或代码最底部的geom_point线:

df <- read.table("input.txt",header = TRUE,sep = "\t")

topo.loess <- loess (VALUE ~ YVAL * XVAL, df, degree = 2, span = 0.2)

x <- seq (min (df$XVAL), max (df$XVAL), .025)
y <- seq (min (df$YVAL), max (df$YVAL), .5)
interpolated <- predict (topo.loess, expand.grid (XVAL = x,YVAL = y))
colnames(interpolated) <- gsub("YVAL=","",colnames(interpolated))
rownames(interpolated) <- gsub("XVAL=","", rownames(interpolated))

data.m = melt(interpolated)
contourdata = melt(interpolated)
minvalue = min(data.m$value)
names(contourdata) <- c("x", "y", "z")
mp = mean(data.m$value, na.rm=TRUE)
max = max(data.m$value, na.rm=TRUE)

p <- ggplot(data.m, aes(XVAL, YVAL)) + 
  geom_tile(aes(fill = value)) + 
  scale_x_continuous(expand=c(0.01, 0.01),breaks=c(4.5, 4.9, 5.3, 5.7, 6.1, 6.5, 6.9, 7.3)) + 
  scale_y_continuous(expand=c(0.01, 0.01),breaks=c(25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80)) +
  theme(axis.title.x=element_text(size=16, vjust=-0.3)) + 
  theme(axis.title.y=element_text(size=16, vjust=1.8)) + 
  theme(plot.title=element_text(face="bold", size=20, vjust=2)) +
  scale_fill_gradientn(colours=c("navyblue","skyblue","green","yellow","red"), limits=c(-0.01,0.4)) + 
  theme(axis.text.x=element_text(angle=90,vjust = 0.5,size=12,color="black"),axis.text.y=element_text(size=12,color="black")) + 
  guides(fill = guide_colorbar(barwidth = 1, barheight = 10, title = "Values", order = 1), size=guide_legend("Standard\nError", order = 2)) +
  theme(panel.grid = element_blank(), panel.background = element_blank()) + 
  theme(legend.key = element_blank()) +
  geom_contour(data=contourdata, aes(x=x, y=y, z=z), color="black", binwidth=0.05, size=0.05) +
  geom_point(data = df,aes(XVAL,YVAL,size=STANDARD_ERROR),color="black",shape=9)
p

enter image description here

1 个答案:

答案 0 :(得分:0)

您正在寻找带有自定义中断的scale_size。例如,要在图例中绘制4个钻石(具有任意值):

+ scale_size_continuous(breaks=c(0.0025, 0.0050,0.1,0.2))