控制图例和ggplot2中的点形状

时间:2014-09-09 13:38:27

标签: r ggplot2

我使用data.frame f:

创建了一个ggplot
fe  ci1 median  ci2 obs mode    mode2
s9  0.00283 0.07459 0.336   0   global  global2
s8  0.00273 0.07343 0.3373  0   global  global2
s7  0.00280 0.07435 0.3366  0   global  global2
gc10    0.5163  0.8201  0.9716  0.2 global  global2
gc9 0.2997  0.6065  0.8653  0.4 global  global2
gc8 0.1377  0.3933  0.7015  0   global  global2
gc7 0.0276  0.1806  0.4867  0   global  global2
s9  0.00282 0.07451 0.3348  0   general general2
s8  0.00283 0.07399 0.3376  0   general general2
s7  0.00294 0.07403 0.3382  0   general general2
gc10    0.5171  0.8207  0.9722  0.2 general general2
gc9 0.2998  0.6054  0.8631  0.4 general general2
gc8 0.1361  0.393   0.7001  0   general general2
gc7 0.0275  0.1792  0.4824  0   general general2
s9  0.00634 0.1584  0.6003  1   proximal    proximal2
s8  0.00627 0.159   0.6042  1   proximal    proximal2
s7  0.00638 0.1587  0.6006  0   proximal    proximal2
gc10    0.401   0.8406  0.9936  1   proximal    proximal2
gc9 0.4007  0.8418  0.9937  1   proximal    proximal2
gc8 0.4012  0.8411  0.9936  1   proximal    proximal2
gc7 0.3972  0.8411  0.9937  1   proximal    proximal2

此代码生成并行误差条,其中变量median和obs的值为点:

gpf <- ggplot( f, aes(x= fe, y= median, fill= mode), color = mode, group= mode, fill= mode ) +
geom_errorbar(aes(ymin = ci1, ymax = ci2), width = .2, position = pdf, color = aes(mode) ) +
geom_point(position = pdf, aes(shape = mode)) +
geom_vline(xintercept = 17.5, linetype = "longdash") + 
geom_point(position = pdf, aes(x= fe, y= obs, shape = mode2)) +
scale_shape_manual(values=c(15:17, 0:2))
print( gpf + theme_bw() + theme(axis.title.x = element_blank()) + theme(axis.title.y = element_text(face="bold")) )

如何为变量中位数设置空心点,为变量中位数设置实心空心点(每组保持相同的形状)?另外,如何获得模式的唯一图例?

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

我无法按原样运行您的代码,但根据因素,您可以在geom_point中为每个变量设置不同的形状:

geom_point(position = pdf, aes(x= fe, y= obs, shape = factor(mode2)))

答案 1 :(得分:0)

经过相当多的证明和错误之后,我想我找到了答案:

gpf <- ggplot( f, aes(x= fe, y= median, fill= mode), color = mode, group= mode, fill= mode ) +
geom_errorbar(aes(ymin = ci1, ymax = ci2), width = .2, position = pdf, color = aes(mode) ) +
geom_point(position = pdf, aes(shape = mode)) +
geom_vline(xintercept = 17.5, linetype = "longdash") + 
geom_point(position = pdf, aes(x= fe, y= obs, shape = mode2)) +
guides(shape= guide_legend("Mode of\n replacement")) +
scale_shape_identity() +
scale_shape_manual(values=c(16,1, 17,2, 15,0))
print( gpf + theme_bw() + theme(axis.title.x = element_blank()) + theme(axis.title.y = element_text(face="bold")) + guides(fill=FALSE) )