我有一个散点图,在其中绘制两组统计数据。点的形状指示该数据点用于哪个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()
当我尝试取消注释图例行时,出现“ + scale_shape_manual(名称=“ X人口”,标签= c(“ PopOne”,:一元运算符的无效参数“错误)
这是我没有传说的情节
有人可以帮我弄清楚我在做什么错吗?谢谢!
我需要图例具有一部分指示颜色和一部分指示形状。我的实际数据集有四种颜色和七个形状,因此我无法提供说明所有组合的图例。
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))
您需要在行的末尾加上+,而不是下一行的开始。
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退出时,我都会犯此错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句