当手动“闪避”ggplot中的某个点时,出现“提供给连续刻度的离散值”错误

时间:2014-03-11 05:49:19

标签: r ggplot2

我正在尝试手动dodge一个点与错误栏对齐。

此处包含head我的数据,我的完整数据集通过dput

包含在帖子的底部
> head(Data, n=15)
   StudyArea      ID Obs variable value       SE   id
1       Cali CAL_F01  22 CaliPred    NA       NA  0.0
2       Cali CAL_F17  50 CaliPred    NA       NA  0.0
3       Cali CAL_F19   8 CaliPred    NA       NA  0.0
4       Cali CAL_F23  15 CaliPred    NA       NA  0.0
5       Cali CAL_F43  54 CaliPred    NA       NA  0.0
6       Cali CAL_M33  30 CaliPred    NA       NA  0.0
7       Cali CAL_M36  11 CaliPred    NA       NA  0.0
8       Colo COL_P01  90 CaliPred    77 6.314889 -7.1
9       Colo COL_P03  53 CaliPred    55 5.497654 -7.1
10      Colo COL_P05   9 CaliPred     5 1.642908 -7.1
11      Colo COL_P06  42 CaliPred    38 4.518088 -7.1
12      Colo COL_P07  72 CaliPred    84 6.877948 -7.1
13      Colo COL_P08  40 CaliPred    47 5.483234 -7.1
14      Colo COL_P09  60 CaliPred    65 6.076180 -7.1
15      Colo COL_P10  58 CaliPred    61 5.504660 -7.1

如下图所示,这些点未正确对齐。误差线是手动dodged idData(下面代码的第三行)。

p <-ggplot(Data , aes(x=ID))+
  geom_point(aes(y=value, color=variable),size=3, )+
  geom_errorbar(aes(ymin=value-SE, ymax=value+SE, x=as.numeric(factor(ID))+id, color=variable),lty = 2, cex=1, width = 0.5)+
  geom_point(aes(y=Obs),shape="*",size=12)+
  facet_wrap(~StudyArea,scales="free",ncol=1) +
  theme(axis.text.x=element_text(angle=30, hjust=1))
pp <- p + scale_color_manual(name="Area", 
                        values=c("red", "blue","darkgreen"), 
                        breaks=c("Pred", "Pred", "Pred"))+
        theme(legend.position="bottom")

当我将x=as.numeric(factor(ID))+id添加到该点的aes参数时(如下面的代码所示),我收到错误Error: Discrete value supplied to continuous scale

p <-ggplot(Data , aes(x=ID))+
  geom_point(aes(y=value, x=as.numeric(factor(ID))+id, color=variable),size=3, shape=1)+
  geom_errorbar(aes(ymin=value-SE, ymax=value+SE, x=as.numeric(factor(ID))+id, color=variable),lty = 2, cex=1, width = 0.5)+
  geom_point(aes(y=Obs),shape="*",size=12)+
  facet_wrap(~StudyArea,scales="free",ncol=1) +
  theme(axis.text.x=element_text(angle=30, hjust=1))
pp <- p + scale_color_manual(name="Area", 
                        values=c("red", "blue","darkgreen"), 
                        breaks=c("Pred", "Pred", "Pred"))+
        theme(legend.position="bottom")
pp

问题:如何正确dodge点与手动指定的误差线对齐?

fig

提前致谢!

所有数据

Data <- structure(list(StudyArea = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 
3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Cali", 
"Colo", "Pata"), class = "factor"), ID = structure(c(1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L), .Label = c("CAL_F01", 
"CAL_F17", "CAL_F19", "CAL_F23", "CAL_F43", "CAL_M33", "CAL_M36", 
"COL_P01", "COL_P03", "COL_P05", "COL_P06", "COL_P07", "COL_P08", 
"COL_P09", "COL_P10", "COL_P12", "COL_P13", "PAT_F03", "PAT_F04", 
"PAT_F05", "PAT_M02", "PAT_M03", "PAT_M04"), class = "factor"), 
    Obs = c(22L, 50L, 8L, 15L, 54L, 30L, 11L, 90L, 53L, 9L, 42L, 
    72L, 40L, 60L, 58L, 20L, 37L, 50L, 67L, 20L, 19L, 58L, 5L, 
    22L, 50L, 8L, 15L, 54L, 30L, 11L, 90L, 53L, 9L, 42L, 72L, 
    40L, 60L, 58L, 20L, 37L, 50L, 67L, 20L, 19L, 58L, 5L, 22L, 
    50L, 8L, 15L, 54L, 30L, 11L, 90L, 53L, 9L, 42L, 72L, 40L, 
    60L, 58L, 20L, 37L, 50L, 67L, 20L, 19L, 58L, 5L), variable = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("CaliPred", "ColoPred", 
    "PataPred"), class = "factor"), value = c(NA, NA, NA, NA, 
    NA, NA, NA, 77, 55, 5, 38, 84, 47, 65, 61, 25, 43, 34, 66, 
    18, 15, 34, 5, 18, 49, 6, 13, 31, 27, 12, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, 32, 60, 18, 14, 32, 5, 30, 53, 7, 
    15, 49, 32, 14, 86, 62, 5, 48, 93, 52, 73, 68, 28, 47, NA, 
    NA, NA, NA, NA, NA), SE = c(NA, NA, NA, NA, NA, NA, NA, 6.31488929298672, 
    5.497653791366, 1.64290834339061, 4.51808782802661, 6.87794805206761, 
    5.48323374891446, 6.07617986453451, 5.50465952513403, 3.46640687133807, 
    4.67925642640691, 3.89436451780002, 5.95337845105602, 3.16947277410086, 
    2.65632106571484, 4.54262669561671, 1.48630436319955, 3.38512609777978, 
    4.89449176574693, 1.22686235133486, 2.55142136173085, 4.65844412010727, 
    3.84103820176873, 2.38653357702158, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, 3.92516789504607, 5.92416243616404, 3.14626294785635, 
    2.67848085040132, 4.48377790891048, 1.49789328005297, 3.43538336956585, 
    4.86558548401014, 0.927955791077904, 2.51531952706457, 5.07056696779931, 
    3.85815493933999, 2.34767760968492, 6.39770960804122, 5.58054623577558, 
    1.64529737748105, 4.38254693434801, 6.95044087111244, 5.54875633450289, 
    6.16583286016463, 5.32461337007243, 3.47652151729081, 4.68257865863345, 
    NA, NA, NA, NA, NA, NA), id = c(0, 0, 0, 0, 0, 0, 0, -7.1, 
    -7.1, -7.1, -7.1, -7.1, -7.1, -7.1, -7.1, -7.1, -7.1, -17.1, 
    -17.1, -17.1, -17.1, -17.1, -17.1, -0.1, -0.1, -0.1, -0.1, 
    -0.1, -0.1, -0.1, -7, -7, -7, -7, -7, -7, -7, -7, -7, -7, 
    -16.9, -16.9, -16.9, -16.9, -16.9, -16.9, 0.1, 0.1, 0.1, 
    0.1, 0.1, 0.1, 0.1, -6.9, -6.9, -6.9, -6.9, -6.9, -6.9, -6.9, 
    -6.9, -6.9, -6.9, -17.7, -17.7, -17.7, -17.7, -17.7, -17.7
    )), .Names = c("StudyArea", "ID", "Obs", "variable", "value", 
"SE", "id"), row.names = c(NA, -69L), class = "data.frame")

1 个答案:

答案 0 :(得分:3)

这似乎是你要找的东西(???)

library(ggplot2)
gg <- na.omit(Data)
p <-ggplot(gg, aes(x=ID))+
  geom_point(aes(y=value, color=variable),size=3,position=position_dodge(1))+
  geom_errorbar(aes(ymin=value-SE, ymax=value+SE, color=variable), position=position_dodge(1),cex=1, width = .5)+
  geom_point(aes(y=Obs),shape="*",size=12)+
  facet_wrap(~StudyArea,scales="free",ncol=1) +
  theme(axis.text.x=element_text(angle=30, hjust=1))+
  scale_color_manual(name="Area", values=c("red", "blue","darkgreen"))+
  theme(legend.position ="bottom")
p

您收到该错误是因为您尝试将离散值 - aes(x=ID,...)与连续值 - aes(x=as.numeric(ID)+id,...)组合在一起。这毫无意义。如上所示,如果您在Data

中删除了NA,则可以使用道奇获得所需的(??)结果

IMO这是表示数据的更好方式:

p <-ggplot(gg, aes(x=ID))+
  geom_point(aes(y=value, color=variable),size=3,position=position_dodge(1))+
  geom_errorbar(aes(ymin=value-SE, ymax=value+SE, color=variable), position=position_dodge(1),cex=1, width = .5)+
  geom_point(aes(y=Obs),shape="*",size=8)+
  facet_grid(~StudyArea,scales="free") +
  theme(axis.text.x=element_text(angle=-90, vjust=.3))+
  scale_color_manual(name="",values=c("red", "blue","darkgreen"))+
  theme(legend.position ="bottom")
p