在不同的x轴上绘制标准偏差线

时间:2015-11-25 18:27:18

标签: r plot ggplot2 lattice

在以下示例中,我希望在zone轴上放置y,然后在D1轴上标记D1sd及其标准差(阴影)x 。接下来,我想在第二个D1b轴上添加x及其标准偏差。我的第二个问题是,是否可以将第二组数据D2绘制在第一组数据旁边。我正在考虑spplot将面板放在一起的方式。谢谢!

zone    D1           D1sd         D1b        D1bsd    D2            D2sd        D2b      D2bsd
-10 6.018198819 1.353674355 0.820238734 0.299921523 6.149905542 1.559112995 0.71903318  0.281436916
-9  6.016694189 1.348320178 0.790463895 0.320471326 6.225247218 1.810133214 0.690944285 0.291123921
-8  6.075920068 1.268199241 0.792396958 0.295767298 6.452827975 1.890055573 0.698130383 0.285354803
-7  6.014926533 1.15754388  0.826652396 0.269340472 6.364786271 1.677836628 0.748784125 0.262342978
-6  5.934024155 1.097224151 0.876312952 0.287715603 6.167672962 1.558124318 0.755995918 0.265152681
-5  6.180879693 1.115373166 0.911045374 0.302416557 6.429580579 1.485044161 0.783518016 0.255475422
-4  6.215761357 1.287465467 0.930981232 0.302896699 6.579955644 1.388358072 0.810873074 0.234479504
-3  6.191414137 1.297136068 0.859521028 0.301839757 6.72533907  1.383269712 0.786424272 0.242793151
-2  6.249558839 1.484243431 0.870789671 0.315339266 6.738830636 1.39348093  0.822833797 0.28853238
-1  6.279693424 1.462642241 0.890051094 0.313090388 6.665698185 1.272444414 0.849884276 0.309606843
0   6.389352438 1.653046732 0.911295197 0.332748249 6.623842834 1.3384852   0.860175975 0.311888845
1   6.421109477 1.954238381 0.917046385 0.349039084 6.633736605 1.627187751 0.880706612 0.346350393
2   6.187522396 1.994178951 0.881417644 0.38571426  6.422238767 1.685610306 0.875399565 0.351651773
3   5.975654953 2.180870669 0.871365681 0.444535385 6.245207747 1.925609129 0.915266481 0.424662193
4   5.681784682 2.182018258 0.846469896 0.38550673  6.004553419 1.947533306 0.890484046 0.404342645
5   5.550390285 2.189799132 0.834608476 0.340348644 5.831848009 1.849502381 0.887486532 0.387460845
6   5.382758749 2.460409982 0.832118248 0.360057614 5.810419947 2.06423957  0.954814407 0.38078381
7   4.819027419 2.643911373 0.78895866  0.38043413  5.42194855  2.259929373 0.935858628 0.37891625
8   3.782918423 2.584426217 0.643611576 0.335647266 4.418220284 2.186679796 0.790979174 0.364691895
9   3.064023314 2.528951519 0.496242154 0.294101493 3.64670387  2.091471213 0.592464821 0.341064247
10  2.62392179  2.707531426 0.380282732 0.249942178 3.159422995 2.392110771 0.452474888 0.334645666

1 个答案:

答案 0 :(得分:3)

加载数据

dat <- read.table(text = "zone    D1           D1sd         D1b        D1bsd    D2            D2sd        D2b      D2bsd
-10 6.018198819 1.353674355 0.820238734 0.299921523 6.149905542 1.559112995 0.71903318  0.281436916
-9  6.016694189 1.348320178 0.790463895 0.320471326 6.225247218 1.810133214 0.690944285 0.291123921
-8  6.075920068 1.268199241 0.792396958 0.295767298 6.452827975 1.890055573 0.698130383 0.285354803
-7  6.014926533 1.15754388  0.826652396 0.269340472 6.364786271 1.677836628 0.748784125 0.262342978
-6  5.934024155 1.097224151 0.876312952 0.287715603 6.167672962 1.558124318 0.755995918 0.265152681
-5  6.180879693 1.115373166 0.911045374 0.302416557 6.429580579 1.485044161 0.783518016 0.255475422
-4  6.215761357 1.287465467 0.930981232 0.302896699 6.579955644 1.388358072 0.810873074 0.234479504
-3  6.191414137 1.297136068 0.859521028 0.301839757 6.72533907  1.383269712 0.786424272 0.242793151
-2  6.249558839 1.484243431 0.870789671 0.315339266 6.738830636 1.39348093  0.822833797 0.28853238
-1  6.279693424 1.462642241 0.890051094 0.313090388 6.665698185 1.272444414 0.849884276 0.309606843
0   6.389352438 1.653046732 0.911295197 0.332748249 6.623842834 1.3384852   0.860175975 0.311888845
1   6.421109477 1.954238381 0.917046385 0.349039084 6.633736605 1.627187751 0.880706612 0.346350393
2   6.187522396 1.994178951 0.881417644 0.38571426  6.422238767 1.685610306 0.875399565 0.351651773
3   5.975654953 2.180870669 0.871365681 0.444535385 6.245207747 1.925609129 0.915266481 0.424662193
4   5.681784682 2.182018258 0.846469896 0.38550673  6.004553419 1.947533306 0.890484046 0.404342645
5   5.550390285 2.189799132 0.834608476 0.340348644 5.831848009 1.849502381 0.887486532 0.387460845
6   5.382758749 2.460409982 0.832118248 0.360057614 5.810419947 2.06423957  0.954814407 0.38078381
7   4.819027419 2.643911373 0.78895866  0.38043413  5.42194855  2.259929373 0.935858628 0.37891625
8   3.782918423 2.584426217 0.643611576 0.335647266 4.418220284 2.186679796 0.790979174 0.364691895
9   3.064023314 2.528951519 0.496242154 0.294101493 3.64670387  2.091471213 0.592464821 0.341064247
10  2.62392179  2.707531426 0.380282732 0.249942178 3.159422995 2.392110771 0.452474888 0.334645666", header = T)

第一个简单的解决方案

第一次尝试。第一种方式是这种“正常”的方式。通常情况下,我们可以使用coord_flip()翻转x和y,但不幸的是,这不适用于小平面和自由缩放。

library(ggplot2)
dat2 <- data.frame(D = rep(c("D1", "D1b", "D2", "D2b"), each = nrow(dat)),
                   group = rep(c('1', '2'), each = nrow(dat) * 2),
                   zone = dat$zone,
                   value = unlist(dat[c(2, 4, 6, 8)]),
                   SD = unlist(dat[c(3, 5, 7, 9)]))

ggplot(dat2, aes(zone, value, ymin = value - SD, ymax = value + SD, fill = group)) + 
  geom_point() + geom_line() + geom_ribbon(alpha = 0.2) +
  facet_wrap(~D, scales = 'free') +
  theme_bw()

enter image description here

具有翻转轴的解决方案

手动绘制多边形时,实际上可以获得翻转轴。这段代码很不错,但你应该明白这个想法。

polydat <- data.frame(D = rep(c("D1", "D1b", "D2", "D2b"), each = nrow(dat) * 2),
                      value = c(dat$D1 - dat$D1sd, rev(dat$D1 + dat$D1sd),
                                dat$D1b - dat$D1bsd, rev(dat$D1b + dat$D1bsd),
                                dat$D2 - dat$D2sd, rev(dat$D2 + dat$D2sd),
                                dat$D2b - dat$D2bsd, rev(dat$D2b + dat$D2bsd)),
                      zone = c(dat$zone, rev(dat$zone)),
                      group = rep(c('1', '2'), each = nrow(dat) * 4))

ggplot(dat2, aes(value, zone, fill = group)) +
  geom_point() + geom_path() +
  geom_polygon(data = polydat, alpha = 0.2) +
  facet_wrap(~D, scales = 'free') +
  theme_bw()

enter image description here

将此分为两个图的一种方法是首先将数据标准化为一个公共x轴(例如,使用scale)。