如何在ggplot2中为颜色和形状创建图例

时间:2016-03-25 19:14:42

标签: r plot ggplot2

我有一个散点图,我正在绘制两组的统计数据。点的形状指示数据点所针对的X轴群,并且点的颜色指示用于

的Y轴群。

我的表看起来像这样

comp    source2 y   SEy source1 x   SEx color   shape
PopA    PopAlpha    0.363658    0.002966    PopOne  0.213503    0.002152    green   1
PopB    PopAlpha    0.354646    0.002923    PopOne  0.211894    0.002159    green   1
PopC    PopAlpha    0.347382    0.002714    PopOne  0.213501    0.00204 green   1
PopA    PopBeta 0.231174    0.002729    PopOne  0.213503    0.002152    blue    1
PopB    PopBeta 0.228363    0.002614    PopOne  0.211894    0.002159    blue    1
PopC    PopBeta 0.227815    0.002481    PopOne  0.213501    0.00204 blue    1
PopA    PopAlpha    0.363658    0.002966    PopTwo  0.227151    0.002284    green   8
PopB    PopAlpha    0.354646    0.002923    PopTwo  0.225023    0.002247    green   8
PopC    PopAlpha    0.347382    0.002714    PopTwo  0.226903    0.002133    green   8
PopA    PopBeta 0.231174    0.002729    PopTwo  0.227151    0.002284    blue    8
PopB    PopBeta 0.228363    0.002614    PopTwo  0.225023    0.002247    blue    8
PopC    PopBeta 0.227815    0.002481    PopTwo  0.226903    0.002133    blue    8

我的代码就是这样

read.table("example.txt", header = TRUE) -> tbl
require(ggplot2)
plot <- ggplot(data = tbl,aes(x = x,y = y)) + 
  geom_point(colour=tbl$color, mapping=aes(shape=tbl$shape)) +  
  geom_errorbar(aes(ymin = y-SEy,ymax = y+SEy,width=0),colour=tbl$color) + 
  geom_errorbarh(aes(xmin = x-SEx,xmax = x+SEx,height=0),colour=tbl$color) +
  geom_abline(intercept = 0, slope = 1) + scale_shape_identity()
  #+ scale_colour_manual(name = "Y population",labels = c("PopAlpha", "PopBeta"),values = c("green", "blue"))
  #+ scale_shape_manual(name = "X population",labels = c("PopOne","PopTwo"),values = c(1,8))  
ggsave(plot=plot,height=6,width=7, filename="example2.pdf", useDingbats=FALSE)
plot(plot)
dev.off()

当我尝试取消注释图例线时,我得到&#34;错误+ scale_shape_manual(名称=&#34; X人口&#34;,标签= c(&#34; PopOne&#34;,:   一元运算符的无效参数&#34;

这是我没有传奇的情节

plot

有人可以帮我弄清楚我做错了什么吗?谢谢!

我需要图例有一部分表示颜色,一部分表示形状。我的实际数据集有四种颜色和七种形状,所以我不能有一个解释所有组合的图例。

structure(list(comp = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("PopA", "PopB", "PopC"), class = "factor"), source2 = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("PopAlpha", "PopBeta"), class = "factor"), y = c(0.363658, 0.354646, 0.347382, 0.231174, 0.228363, 0.227815, 0.363658, 0.354646, 0.347382, 0.231174, 0.228363, 0.227815), SEy = c(0.002966, 0.002923, 0.002714, 0.002729, 0.002614, 0.002481, 0.002966, 0.002923, 0.002714, 0.002729, 0.002614, 0.002481), source1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("PopOne", "PopTwo"), class = "factor"), x = c(0.213503, 0.211894, 0.213501, 0.213503, 0.211894, 0.213501, 0.227151, 0.225023, 0.226903, 0.227151, 0.225023, 0.226903), SEx = c(0.002152, 0.002159, 0.00204, 0.002152, 0.002159, 0.00204, 0.002284, 0.002247, 0.002133, 0.002284, 0.002247, 0.002133), color = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L), .Label = c("blue", "green"), class = "factor"), shape = c(1L, 1L, 1L, 1L, 1L, 1L, 8L, 8L, 8L, 8L, 8L, 8L)), .Names = c("comp", "source2", "y", "SEy", "source1", "x", "SEx", "color", "shape"), class = "data.frame", row.names = c(NA, -12L))

2 个答案:

答案 0 :(得分:1)

你需要在行的末尾加上+而不是下一行的开头。

ggplot(data = tbl,aes(x = x,y = y)) + 
  geom_point(colour=tbl$color, mapping=aes(shape=tbl$shape)) +  
  geom_abline(intercept = 0, slope = 1) + scale_shape_identity()
到目前为止,

代码在语法上是完整的。因此,下一行被视为一起添加而不是添加到前一行。

 + scale_colour_manual(name = "Y population",labels = c("PopAlpha", "PopBeta"),values = c("green", "blue"))
 + scale_shape_manual(name = "X population",labels = c("PopOne","PopTwo"),values = c(1,8))  

只需将+ s移动到前一行的末尾,即行不完整。

每次我把ggplot2拿出来都会犯这个错误。

答案 1 :(得分:0)

如果你想在图例中获得一个变量,你只需要将它包含在aes中包含该变量。

此外,由于shape是一个数字变量,因此需要将其转换为因子

ggplot(data = tbl,aes(x = x,y = y)) + 
geom_point(mapping=aes(shape=factor(shape),color=color)) +  
geom_abline(intercept = 0, slope = 1) +
scale_colour_manual(name = "Y population",labels = c("PopBeta", "PopAlpha"),values = c("blue", "green"))+
scale_shape_manual(name = "X population",labels = c("PopOne","PopTwo"),values = c(1,8))