双Y轴图上的误差线--ggplot2

时间:2020-08-22 13:07:21

标签: r ggplot2

我正在尝试向双Y轴图形添加误差线,但是运行时,它会完全破坏图形。我在下面附上一张图片。我还添加了代码。

如果您需要完整的数据集,请告诉我!提前非常感谢您!

scalefactor <- max(Complete_Seasonality_Data$PRCP)/max(Complete_Seasonality_Data$Temp_C)
p <- ggplot(Complete_Seasonality_Data, aes(x = NewMonths5))
p <- p + geom_point(aes(y = PRCP, colour = "Precipitation"))  
p <- p + geom_line(aes(y = PRCP, colour = "Precipitation", group=1))
p <- p + geom_point(aes(y = Temp_C*scalefactor, colour = "Temperature")) 
p <- p + geom_line(aes(y = Temp_C*scalefactor, colour = "Temperature", group=1))
p <- p + scale_y_continuous(sec.axis = sec_axis(~./scalefactor, name = ylabseasonality))
p <- p + scale_colour_manual(values = c("blue", "red"))
p <- p + labs(y = "Precipitation (in)",
              x = "Month",
              colour = "Parameter")
p <- p + theme_bw() 
p <- p + theme(axis.text.x = element_text(angle = 90), legend.position = c(.99, .01)) 
p <- p + geom_errorbar(aes(ymin = TempSummary$mean - StdErrorTemp, ymax = TempSummary$mean + StdErrorTemp), position=position_dodge(.9), width=0.2)
p <- p + geom_errorbar(aes(ymin = PrecipSummary$mean - StdErrorPrecip, ymax = TempSummary$mean + StdErrorPrecip), position=position_dodge(.9), width=0.2)

p

我如何计算标准错误

TempSummary<- Summarize(Temp_C~ Month,
                                data=Chara_Data,
                                digits=3)

View(TempSummary)

StdErrorTemp<- (TempSummary$sd)/ (sqrt(TempSummary$n))

View(StdErrorTemp)

PrecipSummary<- Summarize(PRCP ~ Group.1,
                        data=Complete_Seasonality_Data,
                        digits=3)

StdErrorPrecip<- (PrecipSummary$sd/ sqrt(PrecipSummary$n))

完整的数据集!

structure(list(Group.1 = c("April", "August", "December", "February", 
"January", "July", "June", "March", "May", "November", "October", 
"September"), Season = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_), Month = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_), Year = c(2017.05882352941, 2016.6, 2016.6, 2017.6, 
2017.6, 2016.6, 2017.05882352941, 2017, 2017.05882352941, 2016.6, 
2016.6, 2016.6), Date = structure(c(1494315952.94118, 1490691600, 
1500316560, 1506183120, 1504163520, 1487501280, 1499108611.76471, 
1489840800, 1496798682.35294, 1498314240, 1496087280, 1493421840
), class = c("POSIXct", "POSIXt")), Site = c(8.17647058823529, 
8.125, 7.775, 7.775, 6.375, 6.375, 8.20588235294118, 6.80555555555556, 
6.55882352941176, 6.375, 8.1, 6.375), PercentCover = c(0.765882352941176, 
0.7125, 0.7505, 0.7775, 0.8625, 0.867, 0.763529411764706, 0.83, 
0.850588235294118, 0.848, 0.7065, 0.834), AveHt = c(60.1684438927086, 
50.2311192279942, 58.9048701298701, 57.3448097041847, 55.2253291847042, 
64.6965656565657, 57.9602622867329, 56.672138047138, 64.4076426024955, 
57.1465322871573, 54.3781565656566, 58.3185831529582), SE = c(7.07246013321596, 
7.79305525403115, 7.00224498332823, 6.46671176266333, 6.32495719718401, 
7.04611575726224, 8.09695750051648, 5.65899377193264, 7.28959135811987, 
6.24571692582705, 7.32819802238581, 7.05669314452393), MaxHt = c(88.3823529411765, 
81.625, 87.75, 85, 85.875, 96.425, 92.9117647058823, 82.5, 98.6764705882353, 
88.125, 79.75, 89.65), green = c(0.350962665193537, 0.278211058736042, 
0.183934291894458, 0.197711422851132, 0.179043270311077, 0.335751664926552, 
0.186533536107468, 0.256634190010066, 0.319397625619223, 0.204519948331115, 
0.249063275007846, 0.277894684744482), yellow = c(0.556643767952726, 
0.569690303836593, 0.686152813243381, 0.654331042886853, 0.594548585049017, 
0.554485584960289, 0.581008683220038, 0.609988063809375, 0.594827659217835, 
0.620510694031593, 0.633793562346056, 0.600527348262596), brown = c(0.0923935668537371, 
0.14983619398845, 0.122185622134889, 0.145933312808728, 0.226114026992848, 
0.10976275011316, 0.229212761734686, 0.132653108499399, 0.0857747151629417, 
0.174675239990233, 0.114398064606882, 0.121577966992922), Temp = c(78.4411764705882, 
82.975, 75.65, 74.75, 74.3, 82.2051282051282, 81.0882352941177, 
75.8333333333333, 79.8823529411765, 78.6, 80.1944444444444, 83
), Temp_C = c(25.8006535947712, 28.3194444444444, 24.25, 23.75, 
23.5, 27.8917378917379, 27.2712418300654, 24.3518518518519, 26.6013071895425, 
25.8888888888889, 26.7746913580247, 28.3333333333333), Vis = c(1.98823529411765, 
2.12820512820513, 2.2125, 2.07, 2.1625, 2.07179487179487, 2.05, 
2.02777777777778, 2.11764705882353, 2.205, 2.11, 2.17375), Nests = c(12.4117647058824, 
17.1, 7.1, 6.275, 4, 8.9, 13.8787878787879, 4.88888888888889, 
7.38235294117647, 2.8, 13.025, 5.6), SickorDeadFish = c(0.0882352941176471, 
0.2, 0.175, 0.075, 0.05, 0.117647058823529, 0.0882352941176471, 
0.166666666666667, 0.0294117647058824, 0.25, 0.333333333333333, 
0.275), Cladophora = c(0.0866666666666667, 0.0492857142857143, 
0.0471428571428571, 0.0907142857142857, 0.0264285714285714, 0.0154545454545455, 
0.0380952380952381, 0.0295238095238095, 0.0161904761904762, 0.0178571428571429, 
0.0407142857142857, 0.03), Comments = c(NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN), STATION = c(NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_), NAME = c(NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_), DATE = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_), MONTH = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_), AWND = c(6.52626966292135, 5.97866090712743, 5.85811926605505, 
6.31656097560976, 6.181, 6.1103908045977, 6.23947727272727, 6.5154211663067, 
6.0985313174946, 5.64997635933806, 5.43263157894737, 5.54940639269406
), FMTM = c(1412.13333333333, 1431.1935483871, 1411.77419354839, 
1535.16666666667, 1339.24137931034, 1439.77419354839, 1378.3, 
1398.8064516129, 1353.12903225806, 1362.96666666667, 1408.45161290323, 
1381.46666666667), PGTM = c(1394.1095890411, 1394.96774193548, 
1306.83333333333, 1412.0511627907, 1327.90350877193, 1435.51769911504, 
1372.37674418605, 1389.12328767123, 1376.75576036866, 1373.45341614907, 
1346.2774566474, 1396), PRCP = c(0.0205869074492099, 0.0248701298701299, 
0.0663425925925926, 0.0481472684085511, 0.0360991379310345, 0.0101144164759725, 
0.00790067720090293, 0.0762693156732892, 0.0298491379310345, 
0.0472985781990521, 0.034965034965035, 0.0243778801843318), SNOW = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), SNWD = c(0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0), TAVG = c(78.5333333333333, NaN, NaN, 61.1052631578947, 
68.6333333333333, 80.2903225806452, 79.4, 72.5161290322581, 77.8709677419355, 
NaN, NaN, NaN), TMAX = c(83.6826484018265, 88.8509719222462, 
81.4940617577197, 80.6938271604938, 80.8072562358277, 88.1520737327189, 
86.8795454545455, 81.3290043290043, 84.6048034934498, 83.8289786223278, 
86.3615560640732, 88.1009174311927), TMIN = c(67.5423340961098, 
72.5917926565875, 66.4394299287411, 64.9283950617284, 64.5600907029478, 
71.9654377880184, 70.6772727272727, 65.7597402597403, 68.6527472527472, 
68.9643705463183, 70.558352402746, 71.7821100917431), TSUN = c(NaN, 
NaN, NaN, 0, 0, NaN, NaN, NaN, NaN, NaN, NaN, NaN), WDF2 = c(115.538116591928, 
100.905172413793, 133.577981651376, 143.965936739659, 149.438444924406, 
91.141876430206, 99.5022624434389, 131.612903225806, 124.279569892473, 
109.693396226415, 119.450800915332, 115.068493150685), WDF5 = c(107.545045045045, 
97.6077586206897, 124.528735632184, 133.031784841076, 140.826086956522, 
82.5229357798165, 90.972850678733, 120.634573304158, 115.714285714286, 
103.720379146919, 109.266055045872, 104.736842105263), WSF2 = c(15.2026905829596, 
14.8530172413793, 14.6919724770642, 15.4111922141119, 15.1332613390929, 
14.9070938215103, 15.083257918552, 15.4161290322581, 14.8625806451613, 
14.322641509434, 14.3432494279176, 14.5600456621005), WSF5 = c(22.1105855855856, 
21.9961206896552, 20.8029885057471, 20.8081145584726, 20.4824675324675, 
22.4052752293578, 22.2158371040724, 21.9317286652079, 21.130303030303, 
20.8722748815166, 20.493119266055, 21.0052511415525), WT01 = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), WT02 = c(NaN, 1, NaN, 1, 1, 
NaN, NaN, 1, 1, NaN, 1, NaN), WT08 = c(1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1), WT10 = c(NaN, NaN, NaN, NaN, NaN, NaN, 1, NaN, NaN, 
NaN, NaN, NaN), NewMonths2 = structure(c(17295, 17253, 17364, 
17432, 17409, 17216, 17350, 17243, 17324, 17341, 17315, 17284
), class = "Date")), row.names = c(NA, -12L), class = "data.frame")

enter image description here

**编辑以添加完整的数据集以及我如何进行标准错误

临时摘要

structure(list(Month = c("April", "August", "December", "February", 
"January", "July", "June", "March", "May", "November", "October", 
"September"), n = c(34, 40, 40, 40, 40, 40, 34, 36, 34, 40, 40, 
40), nvalid = c(34, 40, 40, 40, 40, 39, 34, 36, 34, 40, 36, 40
), mean = c(25.801, 28.319, 24.25, 23.75, 23.5, 27.892, 27.271, 
24.352, 26.601, 25.889, 26.775, 28.333), sd = c(0.478, 0.978, 
0.921, 0.793, 0.551, 0.463, 0.632, 1.47, 0.905, 0.763, 0.928, 
0.534), min = c(25, 26.667, 22.778, 21.667, 21.667, 27.222, 26.111, 
22.778, 25, 25, 25.556, 27.222), Q1 = c(25.556, 27.778, 23.889, 
23.333, 23.333, 27.778, 27.222, 23.333, 26.111, 25.556, 25.556, 
27.778), median = c(25.556, 27.778, 23.889, 23.889, 23.333, 27.778, 
27.222, 23.889, 26.667, 25.556, 27.222, 28.333), Q3 = c(25.972, 
28.889, 25, 24.444, 23.889, 28.333, 27.639, 24.583, 27.222, 26.111, 
27.361, 28.889), max = c(26.667, 30, 25.556, 25, 24.444, 28.889, 
28.889, 27.222, 27.778, 27.778, 28.333, 29.444)), class = "data.frame", row.names = c(NA, 
-12L))

摘要摘要

structure(list(MONTH = c("April", "August", "December", "February", 
"January", "July", "June", "March", "May", "November", "October", 
"September"), n = c(446, 464, 436, 422, 465, 437, 444, 465, 465, 
424, 438, 439), nvalid = c(443, 462, 432, 421, 464, 437, 443, 
453, 464, 422, 429, 434), mean = c(0.021, 0.025, 0.066, 0.048, 
0.036, 0.01, 0.008, 0.076, 0.03, 0.047, 0.035, 0.024), sd = c(0.094, 
0.184, 0.342, 0.211, 0.142, 0.047, 0.047, 0.343, 0.14, 0.24, 
0.243, 0.112), min = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Q1 = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), median = c(0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0), Q3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    max = c(1.1, 3.06, 4.94, 2.61, 1.5, 0.47, 0.76, 3.32, 1.43, 
    3.29, 3.64, 1.25), percZero = c(81.264, 87.662, 76.389, 75.534, 
    77.802, 86.728, 86.682, 75.717, 84.267, 77.962, 83.916, 81.797
    )), class = "data.frame", row.names = c(NA, -12L))

临时摘要结果 enter image description here

摘要摘要结果 enter image description here

1 个答案:

答案 0 :(得分:0)

我建议采用下一种方法。请注意误差条的值。同样,比例因子也必须应用于误差条。这就是为什么您的情节混乱。这里的代码使用您添加的数据:

library(ggplot2)
#Create var
Complete_Seasonality_Data$NewMonths5 <- as.Date(Complete_Seasonality_Data$Date)
#Computing
StdErrorTemp<- (TempSummary$sd)/ (sqrt(TempSummary$n))
StdErrorPrecip<- (PrecipSummary$sd/ sqrt(PrecipSummary$n))
#Scale factor
scalefactor <- max(Complete_Seasonality_Data$PRCP)/max(Complete_Seasonality_Data$Temp_C)
#Plot
p <- ggplot(Complete_Seasonality_Data, aes(x = NewMonths5))
p <- p + geom_point(aes(y = PRCP, colour = "Precipitation"))  
p <- p + geom_line(aes(y = PRCP, colour = "Precipitation", group=1))
p <- p + geom_errorbar(aes(ymin = PrecipSummary$mean - StdErrorPrecip,
                           ymax = PrecipSummary$mean + StdErrorPrecip),
                       position=position_dodge(.9), width=0.2)
p <- p + geom_point(aes(y = Temp_C*scalefactor, colour = "Temperature")) 
p <- p + geom_line(aes(y = Temp_C*scalefactor, colour = "Temperature", group=1))
p <- p + scale_y_continuous(sec.axis = sec_axis(~./scalefactor, name = 'Temperature'))
p <- p + geom_errorbar(aes(ymin = TempSummary$mean*scalefactor - StdErrorTemp,
                           ymax = TempSummary$mean*scalefactor + StdErrorTemp),
                       position=position_dodge(.9), width=0.2)
p <- p + scale_colour_manual(values = c("blue", "red"))
p <- p + labs(y = "Precipitation (in)",
              x = "Month",
              colour = "Parameter")
p <- p + theme_bw() 
p <- p + theme(axis.text.x = element_text(angle = 90), legend.position = c(.99, .01)) 
p

输出:

enter image description here