为ggplot添加图例和结构化数据

时间:2013-12-04 00:41:43

标签: r ggplot2

在下面的数据中,我有三个站点(AAA,BBB,CCC)和每个站点内的个人(分别为7,12,7)。对于每个人,我观察到了值(ObsValues)和三组预测值,每个值都有标准误差。我有26行(即26个人)和9列。

此处的数据通过dput()

包含在此处
help <- structure(list(StudyArea = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L), .Label = c("AAA", "BBB", "CCC"), class = "factor"), 
    Ind = structure(1:26, .Label = c("AAA_F01", "AAA_F17", "AAA_F33", 
    "AAA_F49", "AAA_F65", "AAA_F81", "AAA_F97", "BBB_P01", "BBB_P02", 
    "BBB_P03", "BBB_P04", "BBB_P05", "BBB_P06", "BBB_P07", "BBB_P08", 
    "BBB_P09", "BBB_P10", "BBB_P11", "BBB_P12", "CCC_F02", "CCC_F03", 
    "CCC_F04", "CCC_F05", "CCC_F06", "CCC_F07", "CCC_F08"), class = "factor"), 
    ObsValues = c(22L, 50L, 8L, 15L, 54L, 30L, 11L, 90L, 6L, 
    53L, 9L, 42L, 72L, 40L, 60L, 58L, 1L, 20L, 37L, 2L, 50L, 
    68L, 20L, 19L, 58L, 5L), AAAPred = c(28L, 52L, 6L, 15L, 35L, 
    31L, 13L, 79L, 6L, 58L, 5L, 42L, 88L, 49L, 68L, 60L, 1L, 
    26L, 46L, 0L, 34L, 71L, 20L, 15L, 35L, 5L), AAAPredSE = c(3.5027829, 
    4.7852191, 1.231803, 2.5244013, 4.873907, 3.8854192, 2.3532752, 
    6.3444402, 1.7387295, 5.605111, 1.667818, 4.4709107, 7.0437967, 
    5.447496, 6.0840486, 5.4371275, 0.8156916, 3.5153847, 4.698754, 
    0, 3.8901103, 5.993616, 3.1720272, 2.6777869, 4.5647313, 
    1.4864128), BBBPred = c(14L, 43L, 5L, 13L, 26L, 32L, 14L, 
    80L, 5L, 62L, 4L, 44L, 67L, 44L, 55L, 42L, 1L, 20L, 47L, 
    0L, 26L, 51L, 15L, 16L, 34L, 6L), BBBPredSE = c(3.1873435, 
    4.8782831, 1.3739863, 2.5752273, 4.4155679, 3.8102168, 2.3419518, 
    6.364606, 1.7096028, 5.6333421, 1.5861323, 4.4951428, 6.6046699, 
    5.302902, 5.9244328, 5.1887055, 0.8268689, 3.4014041, 4.6600598, 
    0, 3.8510512, 5.5776686, 3.0569531, 2.6358433, 4.5273782, 
    1.4263518), CCCPred = c(29L, 53L, 7L, 15L, 44L, 32L, 15L, 
    86L, 8L, 61L, 5L, 46L, 99L, 54L, 74L, 67L, 1L, 30L, 51L, 
    1L, 37L, 94L, 21L, 17L, 36L, 6L), CCCPredSE = c(3.4634488, 
    4.7953389, 0.9484051, 2.5207022, 5.053452, 3.8072731, 2.2764727, 
    6.3605968, 1.6044067, 5.590048, 1.6611899, 4.4183913, 7.0124638, 
    5.6495918, 6.1091934, 5.4797929, 0.8135164, 3.4353934, 4.6261147, 
    0.8187396, 3.7936333, 5.6512378, 3.1686123, 2.633179, 4.5841921, 
    1.3989955)), .Names = c("StudyArea", "Ind", "ObsValues", 
"AAAPred", "AAAPredSE", "BBBPred", "BBBPredSE", "CCCPred", "CCCPredSE"
), class = "data.frame", row.names = c(NA, -26L))

head()的{​​{1}}和dim()也低于

help

我是ggplot的一个相对较新的人,我正在尝试制作一个图表,显示每个研究区的不同颜色的每个人的观察值和预测值。我可以手动添加点并使用下面的代码强制颜色,但是这种感觉相当笨拙而且也没有产生图例,因为我没有在aes()中指定颜色。

    head(help)
  StudyArea     Ind ObsValues AAAPred AAAPredSE BBBPred BBBPredSE CCCPred CCCPredSE
1       AAA AAA_F01        22      28  3.502783      14  3.187343      29 3.4634488
2       AAA AAA_F17        50      52  4.785219      43  4.878283      53 4.7953389
3       AAA AAA_F33         8       6  1.231803       5  1.373986       7 0.9484051
4       AAA AAA_F49        15      15  2.524401      13  2.575227      15 2.5207022
5       AAA AAA_F65        54      35  4.873907      26  4.415568      44 5.0534520
6       AAA AAA_F81        30      31  3.885419      32  3.810217      32 3.8072731

dim(help)
> dim(help)
[1] 26  9

Figure so far

在上图中,星号是观察值,值是预测值,每个研究区都有一个。

我试图融化()数据,但遇到了更多问题。话虽这么说,我怀疑melt()ing或reshape()是最好的选择。

有关如何最佳地更改/重新构建require(ggplot2) ggplot(help, aes(x=Ind, y=ObsValues))+ geom_point(color="red", pch = "*", cex = 10)+ geom_point(aes(y = AAAPred), color="blue")+ geom_errorbar(aes(ymin=AAAPred-AAAPredSE, ymax=AAAPred+AAAPredSE), color = "blue")+ geom_point(aes(y = BBBPred), color="darkgreen")+ geom_errorbar(aes(ymin=BBBPred-BBBPredSE, ymax=BBBPred+BBBPredSE), color = "darkgreen")+ geom_point(aes(y = CCCPred), color="black")+ geom_errorbar(aes(ymin=CCCPred-CCCPredSE, ymax=CCCPred+CCCPredSE), color = "black")+ theme(axis.text.x=element_text(angle=30, hjust=1)) 数据的任何建议,以便我可以为每个StudyArea绘制具有不同颜色的每个人的观察值和预测值,我们将不胜感激。

我也希望产生一个图例 - 一旦数据格式正确,可能就是默认

注意:一旦我对ggplot有了更好的处理,确实可能会简化得到的数字非常繁忙。

提前感谢。

3 个答案:

答案 0 :(得分:4)

试试这个:

library(reshape2)
x.value <- melt(help,id.vars=1:3, measure.vars=c(4,6,8))
x.se    <- melt(help,id.vars=1:3, measure.vars=c(5,7,9))
gg      <- data.frame(x.value,se=x.se$value)
ggplot(gg)+
  geom_point(aes(x=Ind, y=ObsValues),size=5,shape=18)+
  geom_point(aes(x=Ind, y=value, color=variable),size=3, shape=1)+
  geom_errorbar(aes(x=Ind, ymin=value-se, ymax=value+se, color=variable))+
  theme(axis.text.x=element_text(angle=-90))

产生这个:

编辑:回复@ B.Davis的问题如下:

您必须将ObsValuesStudyArea分组,而不是variable。但是当你这样做时,你得到六种颜色,三种用于StudyArea,三种用于预测器组(variable)。如果我们给预测器组(例如,AAAPred等)提供与StudyArea组相同的名称(例如AAA等),那么ggplot只生成三种颜色。

gg$variable <- substring(gg$variable,1,3)   # removes "Pred" from group names
ggplot(gg)+
  geom_point(aes(x=Ind, y=ObsValues, color=StudyArea),size=5,shape=18)+
  geom_point(aes(x=Ind, y=value, color=variable),size=3, shape=1)+
  geom_errorbar(aes(x=Ind, ymin=value-se, ymax=value+se, color=variable))+
  theme(axis.text.x=element_text(angle=-90))

产生这个:

答案 1 :(得分:4)

与@jlhoward解决方案类似,但我选择将ObsValues视为变量,以便在图例中获取它。

help <- dat
x.value <- melt(help,id.vars=1:2, measure.vars=c(3,4,6,8))
x.se    <- melt(help,id.vars=1:2, measure.vars=c(3,5,7,9))
gg      <- data.frame(x.value,se=x.se$value)
ggplot(gg)+
    geom_point(aes(x=Ind, y=value, color=variable),size=3, shape=1)+
    geom_errorbar(data= subset(gg,variable!='ObsValues'),
           aes(x=Ind, ymin=value-se, ymax=value+se, color=variable))+
    theme(axis.text.x=element_text(angle=-90))

enter image description here

答案 2 :(得分:3)

这有点笨拙,但能得到你想要的东西:

# jlhoward's melting is more elegant.
require(reshape2)
melted.points<-melt(help[,c('Ind','ObsValues','AAAPred','BBBPred','CCCPred')])
melted.points$observed<-ifelse(melted.points$variable=='ObsValues','observed','predicted')
melted.points.se<-melt(help[,c('Ind','AAAPredSE','BBBPredSE','CCCPredSE')])
melted.points.se$variable<-gsub('SE','',melted.points.se$variable,)
help2<-merge(melted.points,melted.points.se,by=c('Ind','variable'),all.x=TRUE)
help2<-rename(help2,c(value.x='value',value.y='se'))

现在是实际情节:

ggplot(help2,aes(x=Ind,y=value,color=variable,size=observed,shape=observed,ymin=value-se,ymax=value+se)) + 
  geom_point() +
  geom_errorbar(size=1) +
  scale_colour_manual(values = c("red","blue","darkgreen", "black")) + 
  scale_size_manual(values=c(observed=4,predicted=3)) +
  scale_shape_manual(values=c(observed=8,predicted=16))

enter image description here