我将两个因素映射到ggplot中的color
参数,并希望从图例中删除一个。
我有这些数据。
Data <- structure(list(StudyArea = c("AAA", "BBB", "CCC", "AAA", "BBB",
"CCC", "AAA", "BBB", "CCC"), Obs = c(190L, 481L, 219L, 190L,
481L, 219L, 190L, 481L, 219L), InSituPred = c(180, 462, 199,
180, 462, 199, 180, 462, 199), InSituSE = c(9.57382456553708,
16.5306359391421, 9.51070020039693, 9.57382456553708, 16.5306359391421,
9.51070020039693, 9.57382456553708, 16.5306359391421, 9.51070020039693
), variable = c("ExSituAAA", "ExSituAAA", "ExSituAAA", "ExSituBBB",
"ExSituBBB", "ExSituBBB", "ExSituCCC", "ExSituCCC", "ExSituCCC"
), value = c(NA, 500, 172, 156, NA, 161, 200, 562, NA), SE = c(20.8552264204181,
16.1382421185167, 21.43693858142, 20.8552264204181, 16.1382421185167,
21.43693858142, 20.8552264204181, 16.1382421185167, 21.43693858142
)), .Names = c("StudyArea", "Obs", "InSituPred", "InSituSE",
"variable", "value", "SE"), row.names = c(NA, -9L), class = "data.frame")
使用下面的代码,可以制作如下图。
ggplot(Data)+
geom_point(aes(x=StudyArea, y=value, color=variable),size=3, shape=1)+
geom_errorbar(aes(x=StudyArea, ymin=value-SE, ymax=value+SE, color=variable),lty = 2, cex=0.75)+
geom_point(aes(x=StudyArea, y=InSituPred, color=StudyArea),size=3, shape=1)+
geom_errorbar(aes(x=StudyArea, ymin=InSituPred-InSituSE, ymax=InSituPred+InSituSE, color=StudyArea),lty=1,cex=0.75)+
geom_point(aes(x=StudyArea, y=Obs, color=StudyArea),shape="*",size=12)
我想从图例(AAA:CCC)中删除StudyArea颜色,以便它只包含ExSituAAA,ExSituBBB,ExSituCCC。
ADDITION 使用下面的代码(以及@shadow的有用评论)我可以创建下图。
p <- ggplot(Data, aes(x=StudyArea))+
geom_point(aes(y=value, color=variable),size=3, shape=1)+
geom_errorbar(aes(ymin=value-SE, ymax=value+SE, color=variable),lty = 2, cex=0.75)+
geom_point(aes(y=InSituPred, color=StudyArea),size=3, shape=1)+
geom_errorbar(aes(ymin=InSituPred-InSituSE, ymax=InSituPred+InSituSE, color=StudyArea),lty=1,cex=0.75)+
geom_point(aes(y=Obs, color=StudyArea),shape="*",size=12) +
scale_color_discrete(breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC"))
p + scale_color_manual(name="Study Area \nPrediction",
values=c("red", "blue", "darkgreen","red","blue","darkgreen"),
breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC"))
我想在图例中添加linetype
,指定实线= InSitu和虚线= ExSitu。
我使用linetype
手动指定了aes
(而不是lty
中的因素),因为我还需要指定color
。查看下面的head
,数据$ SE为lty
= 2,数据$ InSituSE为lty
1.
&GT;
head(Data)
StudyArea Obs InSituPred InSituSE variable value SE
1 AAA 190 180 9.573825 ExSituAAA NA 20.85523
2 BBB 481 462 16.530636 ExSituAAA 500 16.13824
3 CCC 219 199 9.510700 ExSituAAA 172 21.43694
4 AAA 190 180 9.573825 ExSituBBB 156 20.85523
5 BBB 481 462 16.530636 ExSituBBB NA 16.13824
6 CCC 219 199 9.510700 ExSituBBB 161 21.43694
因此,有关如何使用实线添加linetype
图例的任何建议= InSitu和虚线= ExSitu将不胜感激。
示例:我想添加...
提前致谢。
答案 0 :(得分:2)
您可以使用?scale_color_discrete
指定中断。在您的情况下,这可能类似于以下内容:
ggplot(Data, aes(x=StudyArea))+
geom_point(aes(y=value, color=variable),size=3, shape=1)+
geom_errorbar(aes(ymin=value-SE, ymax=value+SE, color=variable),lty = 2, cex=0.75)+
geom_point(aes(y=InSituPred, color=StudyArea),size=3, shape=1)+
geom_errorbar(aes(ymin=InSituPred-InSituSE, ymax=InSituPred+InSituSE, color=StudyArea),lty=1,cex=0.75)+
geom_point(aes(y=Obs, color=StudyArea),shape="*",size=12) +
scale_color_discrete(breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC"))
编辑:是的,可以指定颜色。因为我真的不明白你想要什么着色方案,所以这里有一些例子(并非所有都是完全认真的)。
p <- ggplot(Data, aes(x=StudyArea))+
geom_point(aes(y=value, color=variable),size=3, shape=1)+
geom_errorbar(aes(ymin=value-SE, ymax=value+SE, color=variable),lty = 2, cex=0.75)+
geom_point(aes(y=InSituPred, color=StudyArea),size=3, shape=1)+
geom_errorbar(aes(ymin=InSituPred-InSituSE, ymax=InSituPred+InSituSE, color=StudyArea),lty=1,cex=0.75)+
geom_point(aes(y=Obs, color=StudyArea),shape="*",size=12)
p + scale_color_manual(name="Study Area \nPrediction",
values=c("red", "blue", "darkgreen","red","blue","darkgreen"),
breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC"))
p + scale_color_manual(name="Study Area \nPrediction",
values=c("black", "black", "black", "red","blue","darkgreen"),
breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC"))
p + scale_color_manual(name="Study Area \nPrediction",
values=c("white", "yellow", "pink", "red","blue","darkgreen"),
breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC"))
附加:如果您在绘图前清理和重组数据,这将更容易。这是我的豁免:
df <- with(Data, data.frame(area=rep(StudyArea, 2),
exarea=c(variable,rep(variable[c(1,4,7)], 3)),
value=c(value, InSituPred),
se=c(SE, InSituSE),
obs = rep(Obs, 2),
situ=rep(c("in", "ex"), each=nrow(Data))))
df <- df[!duplicated(df),]
然后绘图变得更容易:
p <- ggplot(df, aes(x=area))+
geom_point(aes(y=value, color=exarea),size=3, shape=1)+
geom_errorbar(aes(ymin=value-se, ymax=value+se, color=exarea, lty=situ), cex=0.75)+
geom_point(aes(y=obs, color=exarea),shape="*",size=12)
p + scale_color_manual(name="Study Area \nPrediction",
values=c("red", "blue", "darkgreen"),
breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC")) +
scale_linetype_manual(name="Situ",
values=c(1,2),
breaks=c("in", "ex"),
labels=c("InSitu", "ExSitu"))
EDIT2:可以使用原始数据。您必须将lty
放在aes
- 函数中,然后像以前一样使用scale_linetype_manual
。在这里:
p <- ggplot(Data, aes(x=StudyArea))+
geom_point(aes(y=value, color=variable),size=3, shape=1)+
geom_errorbar(aes(ymin=value-SE, ymax=value+SE, color=variable, lty="2"), cex=0.75)+
geom_point(aes(y=InSituPred, color=StudyArea),size=3, shape=1)+
geom_errorbar(aes(ymin=InSituPred-InSituSE, ymax=InSituPred+InSituSE, color=StudyArea, lty="1"),cex=0.75)+
geom_point(aes(y=Obs, color=StudyArea),shape="*",size=12)
p + scale_color_manual(name="Study Area \nPrediction",
values=c("red", "blue", "darkgreen","red","blue","darkgreen"),
breaks=c("ExSituAAA", "ExSituBBB", "ExSituCCC")) +
scale_linetype_manual(name="Situ",
values=c(1,2),
breaks=c("1", "2"),
labels=c("InSitu", "ExSitu"))
通常更好的做法是重组数据。如果您想对此代码进行任何更改,那将很难做到。代码已经很难阅读了。因此,如果您可以重新构建数据集(通常是这样),那么请考虑采用上述方法。