使用未缩放的x轴将阴影区域添加到ggplot

时间:2016-05-11 15:21:28

标签: r ggplot2

使用底部提供的数据,我想在左边添加一个阴影区域,在下面的ggplot中在每个facet的垂直线右边添加一个不同的阴影区域。因为每个方面的x轴只是一个独特的标签,我正在努力应对这个应用,因为我没有明确的xmin / xmax来提供。

所以我的问题是,是否可以在没有连续x轴的情节中为每个面添加阴影区域?

enter image description here

> dput(WF_postdist_melt)
structure(list(Species = c("ABDU", "AMCO", "AMWI", "BUFF", "BWTE", 
"CANG", "CNV", "COGO", "GADW", "GWTE", "HOME", "MALL", "MERG", 
"NOPI", "NSHO", "REDH", "RNDU", "SCAUP", "SNGO", "SWAN", "WODU", 
"ABDU", "AMCO", "AMWI", "BUFF", "BWTE", "CANG", "CNV", "COGO", 
"GADW", "GWTE", "HOME", "MALL", "MERG", "NOPI", "NSHO", "REDH", 
"RNDU", "SCAUP", "SNGO", "SWAN", "WODU", "ABDU", "AMCO", "AMWI", 
"BUFF", "BWTE", "CANG", "CNV", "COGO", "GADW", "GWTE", "HOME", 
"MALL", "MERG", "NOPI", "NSHO", "REDH", "RNDU", "SCAUP", "SNGO", 
"SWAN", "WODU", "ABDU", "AMCO", "AMWI", "BUFF", "BWTE", "CANG", 
"CNV", "COGO", "GADW", "GWTE", "HOME", "MALL", "MERG", "NOPI", 
"NSHO", "REDH", "RNDU", "SCAUP", "SNGO", "SWAN", "WODU", "ABDU", 
"AMCO", "AMWI", "BUFF", "BWTE", "CANG", "CNV", "COGO", "GADW", 
"GWTE", "HOME", "MALL", "MERG", "NOPI", "NSHO", "REDH", "RNDU", 
"SCAUP", "SNGO", "SWAN", "WODU", "ABDU", "AMCO", "AMWI", "BUFF", 
"BWTE", "CANG", "CNV", "COGO", "GADW", "GWTE", "HOME", "MALL", 
"MERG", "NOPI", "NSHO", "REDH", "RNDU", "SCAUP", "SNGO", "SWAN", 
"WODU"), Coefficient = structure(c(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, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L), class = "factor", .Label = c("AmeliaIntercept", 
"AmeliaWLCoef", "AmeliaRCCoef", "BUGSIntercept", "BUGSWLCoef", 
"BUGSRCCoef")), Mean = c(-4802, -4805, -4800, 30050, -4801, 36090, 
-4807, -4802, -4803, -4803, -128800, -4802, 43430, -4787, -4801, 
-4802, -4802, -4805, -4803, -4801, -4800, 2505, -1437, 20, -1543, 
271, -1260, 242, 5609, 778, 465, 1113, 1187, -364, 1469, 754, 
376, 573, -1539, 9, 924, 261, 2625, 1440, -242.9, -1268, 34.87, 
-217.8, 503.5, 4765, 1026, -185.1, -591.4, -90.39, 273.3, -612.9, 
-7.29, 197.2, 16.38, 519.6, 636.3, 116.9, -812.5, 16250, 16260, 
16250, -50800, 16260, 2496, 16240, 16250, 16260, 16250, 61630, 
16250, 51830, 16270, 16250, 16260, 16250, 16260, 16250, 16250, 
16250, 2749, -1690, 157, -336, 183, 347, -54, 5231, 203, -700, 
-771, 1074, -677, 987, 663, 227, 321, -948, -740, 703, 81, 2688, 
-554.9, -252.8, 1183, -103.9, -1333, 1336, 5160, -498.8, 280.1, 
-770, -233.8, -183, -597.4, -146.1, 97.26, 139.7, 295.5, 137.5, 
85.01, -82.17), LowerCI = c(-5091, -5046, -5087, 21390, -5089, 
25700, -5135, -5089, -5087, -5093, -165800, -5092, 31220, -5066, 
-5090, -5088, -5091, -5095, -5102, -5091, -5092, 981, -1444, 
-58, -1545, -128, -1260, 235, 3964, 723, 411, 1113, 645, -365, 
1461, 307, 34, 52, -1621, -161, 483, 208, 738.3, 1430, -297.2, 
-1269, -378.2, -218, 495.4, 2734, 619.1, -245.3, -594.4, -543.9, 
268, -627.8, -500.7, -273.3, -680.2, 236.6, 500.6, -426.2, -939, 
16010, 15870, 16010, -71570, 16010, -1601, 15960, 16010, 16010, 
16020, 49940, 16010, 42920, 16060, 16010, 16010, 16010, 16020, 
16010, 16010, 16010, 1200, -1697, 106, -337, -82, 347, -59, 3607, 
130, -834, -771, 749, -677, 980, 386, -104, 2, -1038, -915, 358, 
23, 741, -561.7, -308.5, 1183, -512.3, -1336, 1316, 3280, -559.5, 
216.5, -793.4, -720.1, -185.1, -607.8, -456.5, -287.4, -239.5, 
195.7, 46.58, -417.2, -140.6), UpperCI = c(-4502, -4511, -4505, 
40620, -4499, 48580, -4494, -4501, -4505, -4499, -97720, -4501, 
57960, -4299, -4508, -4501, -4500, -4510, -4509, -4503, -4501, 
4051, -1428, 88, -1542, 504, -1259, 251, 7319, 825, 512, 1114, 
1727, -364, 1476, 1146, 734, 1134, -1480, 153, 1366, 322, 4470, 
1447, -185.6, -1267, 622.4, -217.6, 513.4, 6706, 1619, -117.2, 
-585.9, 646.6, 279.7, -603.1, 572.2, 578, 573.9, 772, 743.1, 
605.5, -693.7, 16500, 16550, 16510, -33780, 16510, 6005, 16480, 
16510, 16510, 16520, 75220, 16500, 62640, 16560, 16510, 16510, 
16510, 16510, 16500, 16510, 16500, 4137, -1683, 206, -335, 468, 
348, -45, 6914, 273, -569, -770, 1363, -676, 993, 930, 566, 666, 
-861, -572, 938, 119, 4485, -549, -209.9, 1185, 265.6, -1330, 
1352, 7023, -446.9, 404.1, -764.4, 145.4, -181.7, -586.1, 234.3, 
480.2, 590.2, 396.4, 224.7, 706.7, -16.03), Imputation_Method = c("Amelia", 
"Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", 
"Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", 
"Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", 
"Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", 
"Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", 
"Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", 
"Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", 
"Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", 
"Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "Amelia", "BUGS", 
"BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", 
"BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", 
"BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", 
"BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", 
"BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", 
"BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", 
"BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS", 
"BUGS", "BUGS", "BUGS", "BUGS", "BUGS", "BUGS"), CoefficientType = c("Intercept", 
"Intercept", "Intercept", "Intercept", "Intercept", "Intercept", 
"Intercept", "Intercept", "Intercept", "Intercept", "Intercept", 
"Intercept", "Intercept", "Intercept", "Intercept", "Intercept", 
"Intercept", "Intercept", "Intercept", "Intercept", "Intercept", 
"WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", 
"WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", 
"WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", 
"RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", 
"RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", 
"RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", 
"Intercept", "Intercept", "Intercept", "Intercept", "Intercept", 
"Intercept", "Intercept", "Intercept", "Intercept", "Intercept", 
"Intercept", "Intercept", "Intercept", "Intercept", "Intercept", 
"Intercept", "Intercept", "Intercept", "Intercept", "Intercept", 
"Intercept", "WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", 
"WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", 
"WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", "WLCoef", 
"WLCoef", "WLCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", 
"RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", 
"RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", "RCCoef", 
"RCCoef", "RCCoef")), .Names = c("Species", "Coefficient", "Mean", 
"LowerCI", "UpperCI", "Imputation_Method", "CoefficientType"), row.names = c(NA, 
-126L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00000000001a0788>)

ggplot构建:

ggplot(data=WF_postdist_melt,aes(x=Species,y=Mean,colour=CoefficientType,
                                 group=Coefficient)) + 
  # geom_rect(aes(xmin=-Inf,xmax=0,ymin=-10000,ymax=10000)) +
  geom_errorbar(aes(ymin=LowerCI,ymax=UpperCI),size=1.25,width=1,
                position=pd) +
  geom_point(position=pd,size=1.5,shape=21,fill="white") +
  geom_hline(yintercept=0) +
  ylab("Waterfowl Model Posterior Estimate") + xlab(NULL) +
  scale_colour_hue(name="Parameter",
                   breaks=c("AmeliaIntercept","AmeliaWLCoef",
                            "AmeliaRCCoef","BUGSIntercept","BUGSWLCoef",
                            "BUGSRCCoef"),
                   labels=c("Intercept (Amelia)",
                            "Water Level Coefficient (Amelia)",
                            "Rice Cover Coefficient (Amelia)",
                            "Intercept (BUGS)",
                            "Water Level Coefficient (BUGS)",
                            "Rice Cover Coefficient (BUGS)"),
                   l=40) +
  facet_wrap(~Species,scales="free") + theme_bw() +
  theme(
    axis.title=element_text(size=20, face="bold"),
    axis.text=element_text(size=10, face="bold"),
    legend.title=element_text(face="bold")
  )

1 个答案:

答案 0 :(得分:1)

分类x轴绘制在以1开头的整数值上,您可以在添加时参考,例如,矩形表示某些区域。

由于每个小平面有一个x值,每个x轴标签的中心值为1,每个小平面的x轴从.5到1.5。

要为每个构面添加矩形,一个在轴标签和网格线的左侧和右侧,您可以使用x轴整数坐标系并添加如下内容:

geom_rect(xmin = .5, xmax = 1, ymin = -Inf, ymax = Inf) +
geom_rect(xmin = 1, xmax = 1.5, ymin = -Inf, ymax = Inf, fill = "blue")