简体   繁体   中英

ggplot2 geom_errorbar in reshaped data

In the data.frame appended I would like to trend device sensitivity at 2 distances with measurement t date, facetted by monitor. This is pretty straightforward in ggplot2 . First melt the data usinge the reshape2 package

data.melt <- melt(big_df[,c(1:3,6)],id=c("Date","Monitor"))

then use ggplot2

ggplot(data.melt, aes(x=Date, y=value, col=variable)) + geom_point() + facet_wrap(~Monitor)

I would also like to add geom_errorbar to these points. The problem I have is the error bars differ for the 50cm and 100cm measurements and I am unsure how to define the limits. ie if variable = Sensitivity.100cm set the limits to value+/-Error.100cm and for variable = Sensitivity.50cm limits = value+/-Error.50cm . How can I best approach this task?

structure(list(Date = c("18/10/2012", "19/10/2012", "22/10/2012", 
"23/10/2012", "23/10/2012", "26/10/2012", "29/10/2012", "31/10/2012", 
"11/01/2012", "11/02/2012", "11/06/2012", "11/08/2012", "11/09/2012", 
"20/11/2012", "27/11/2012", "18/12/2012", "14/01/2012", "23/01/2013", 
"18/01/2013", "16/02/2013", "23/04/2013", "30/04/2013", "07/05/2013", 
"14/05/2013", "21/05/2013", "17/10/2012", "18/10/2012", "19/10/2012", 
"22/10/2012", "23/10/2012", "24/10/2012", "26/10/2012", "29/10/2012", 
"31/10/2012", "11/01/2012", "11/02/2012", "11/06/2012", "11/08/2012", 
"11/09/2012", "20/11/2012", "27/11/2012", "18/12/2012", "14/01/2013", 
"23/01/2013", "18/02/2013", "16/02/2013", "14/04/2013", "30/04/2013", 
"07/05/2013", "14/05/2013", "21/05/2013", "16/10/2012", "18/10/2012", 
"19/10/2012", "22/10/2012", "23/10/2012", "24/10/2012", "26/10/2012", 
"29/10/2012", "31/10/2012", "31/10/2012", "01/11/2012", "02/11/2012", 
"06/11/2012", "08/11/2012", "09/11/2012", "20/11/2012", "27/11/2012", 
"18/12/2012", "14/01/2013", "23/01/2013", "18/02/2013", "16/04/2013", 
"23/04/2013", "30/04/2013", "07/05/2013", "14/05/2013", "21/05/2013", 
"16/04/2013", "23/04/2013", "30/04/2013", "07/05/2013", "14/05/2013", 
"21/05/2013", "16/04/2013", "23/04/2013", "30/04/2013", "07/05/2013", 
"14/05/2013", "21/05/2013"), Sensitivity.100cm = c(23.9310344827586, 
23.6792452830189, 23.0708661417323, 23.75, 31.3333333333333, 
25.1351351351351, 25.9770114942529, 24.5192307692308, 25.4347826086957, 
22.987012987013, 23.1451612903226, 22.4822695035461, 23.4375, 
23.9495798319328, 23.5245901639344, 24.041095890411, 25.4198473282443, 
23.9097744360902, 25.8536585365854, 26.1940298507463, 28.0794701986755, 
26.4041095890411, 24.3875968992248, 24.7019867549669, 26.4383561643836, 
21.2328767123288, 23.5172413793103, 24.0566037735849, 30.7874015748032, 
23.1111111111111, 29.5, 23.0405405405405, 26.2068965517241, 25, 
24.3478260869565, 23.5064935064935, 22.0161290322581, 23.468085106383, 
22.8125, 25.3781512605042, 22.2131147540984, 24.7945205479452, 
23.6923076923077, 24.1353383458647, 25.2439024390244, 26.9402985074627, 
27.9470198675497, 23.8835616438356, 25.3798449612403, 25.0331125827815, 
25.7534246575342, 31.1627906976744, 30.6896551724138, 28.7735849056604, 
29.7637795275591, 28.5294117647059, 40, 29.7972972972973, 33.448275862069, 
33.4653465346535, 33.4653465346535, 36.9565217391304, 31.4285714285714, 
30.8870967741935, 28.5106382978723, 29.0625, 29.4117647058824, 
31.3934426229508, 33.5616438356164, 29.0151515151515, 30.8270676691729, 
29.6341463414634, 34.3283582089552, 32.7152317880795, 37.2602739726027, 
38.2945736434108, 35.8940397350993, 33.5616438356164, 40.5223880597015, 
36.0264900662252, 33.8356164383562, 34.4186046511628, 36.158940397351, 
33.7671232876712, 36.7910447761194, 36.0264900662252, 33.8356164383562, 
30.5426356589147, 36.158940397351, 33.7671232876712), Sensitivity.50cm = c(89.448275862069, 
89.4339622641509, 88.0314960629921, 88.4558823529412, 94.6666666666667, 
85.9459459459459, 92.2988505747126, 93.6538461538461, 91.5217391304348, 
88.2467532467532, 98.1451612903226, 85.6028368794326, 88.28125, 
90, 85.655737704918, 87.7397260273973, 88.7786259541985, 90.8270676691729, 
92.1341463414634, 89.6268656716418, 96.6887417218543, 91.1986301369863, 
89.7364341085271, 87.0198675496689, 90.4794520547945, 80.8219178082192, 
83.8620689655172, 85.188679245283, 82.992125984252, 88.1481481481482, 
93.3333333333333, 87.9054054054054, 90.6896551724138, 89.3269230769231, 
89.1304347826087, 90.1298701298701, 82.9032258064516, 82.6879432624114, 
87.265625, 88.8235294117647, 87.7868852459016, 90.5479452054795, 
91.7692307692308, 83.5338345864662, 92.0121951219512, 94.1044776119403, 
88.0132450331126, 90.8013698630137, 89.7984496124031, 87.6158940397351, 
88.2191780821918, 116.434108527132, 110, 116.509433962264, 112.44094488189, 
110.147058823529, 123.333333333333, 107.5, 119.655172413793, 
127.524752475248, 127.524752475248, 118.478260869565, 96.3636363636364, 
112.338709677419, 105.815602836879, 116.5625, 116.806722689076, 
118.27868852459, 129.452054794521, 113.106060606061, 115.789473684211, 
115, 133.582089552239, 132.05298013245, 136.575342465753, 154.573643410853, 
118.675496688742, 122.602739726027, 133.805970149254, 132.05298013245, 
136.575342465753, 154.573643410853, 132.185430463576, 136.506849315069, 
133.805970149254, 132.05298013245, 136.575342465753, 154.573643410853, 
132.185430463576, 136.506849315069), Error.100cm = c(1.3139695781557, 
1.56444565582802, 1.40192864683188, 1.36970117722038, 1.67497927018681, 
1.33092672997245, 1.78068199825628, 1.60608587389328, 1.71862916313499, 
1.29219147676184, 1.40378186980074, 1.29420479368047, 1.39754248593737, 
1.46276430130498, 1.42679468733846, 1.31571045974648, 1.42197984810665, 
1.39046932397796, 1.30208271366236, 1.4335352770372, 1.39230437361779, 
1.37754694254238, 1.41544528946403, 1.3095178763765, 1.37669529056451, 
1.81215843223602, 1.30671002217217, 1.60377358490566, 1.7304929902233, 
1.34969386357693, 1.63724022536571, 1.28022265685787, 1.79545969561073, 
1.60896158948861, 1.69788036432753, 1.30840530387077, 1.36621567307237, 
1.32512183966048, 1.37108818553002, 1.49853399160731, 1.39344262295082, 
1.33165904761805, 1.38033526499434, 1.40462719490747, 1.28772634645993, 
1.45283002492028, 1.38599003552691, 1.31267619160706, 1.44319351564034, 
1.32118790352715, 1.35955022200543, 1.5848874651365, 1.47108475915183, 
1.72669860544558, 1.61753059717907, 1.49248405369003, 1.86338998124982, 
1.44758684362355, 2.02050526795942, 1.88379184063767, 1.88379184063767, 
2.06235499576199, 1.47504112877929, 1.60684345525575, 1.45000342555042, 
1.53888403153064, 1.61641882871188, 1.63318515124355, 1.54679312200496, 
1.50945900342207, 1.55913092882163, 1.38241268887252, 1.63499270897065, 
1.48969826214596, 1.61504467432214, 1.74375533010884, 1.56437242530036, 
1.54679312200496, 1.76125727182178, 1.56156637384789, 1.54071532591808, 
1.65536096930718, 1.55875527098541, 1.54223702043804, 1.68034779838772, 
1.56156637384789, 1.54071532591808, 1.56197222322557, 1.55875527098541, 
1.54223702043804), Error.50cm = c(2.49898909406806, 2.94121835247545, 
2.66090476357729, 2.57562939531426, 2.84312035153866, 2.42491330336843, 
3.28542665010493, 3.03760942287141, 3.183873590408, 2.43051523015752, 
2.83178223646443, 2.48024233333982, 2.64934764965831, 2.77310924369748, 
2.66991761883645, 2.46860565629316, 2.61888806082572, 2.63908703380214, 
2.39518528553687, 2.6055358582233, 2.54601096513127, 2.51706504452232, 
2.65881302492679, 2.41699173389333, 2.50632526897698, 3.38331206444616, 
2.42264387175866, 2.88777884245547, 2.66556076847414, 2.57667068606312, 
2.82842712474619, 2.45392159423444, 3.26120941588761, 2.96210034629795, 
3.15029929264988, 2.45725817252266, 2.60322780509177, 2.44047901652299, 
2.62969260620419, 2.75266207632928, 2.70491803278689, 2.50538919974957, 
2.67245427169849, 2.53752436551672, 2.39363249102323, 2.66888876749965, 
2.42875772462149, 2.51128061073898, 2.6601687552902, 2.42695128500777, 
2.47524808213598, 3.02027282771754, 2.76292767227726, 3.35536212146769, 
3.02099623515762, 2.86858958869576, 3.22748612183951, 2.71029339461238, 
3.74049197592482, 3.5862911438168, 3.5862911438168, 3.62137635846906, 
2.52830160180932, 3.02499989250178, 2.75411175020533, 3.03382622483556, 
3.15545938748212, 3.12874339081841, 2.99339361416605, 2.94091650663359, 
2.96968686588444, 2.66763937246675, 3.1749324456989, 2.96611863265678, 
3.06769803788475, 3.47196832194708, 2.81593266562507, 2.91397909399762, 
3.17230019504959, 2.96611863265678, 3.06769803788475, 3.47196832194708, 
2.96463964047674, 3.06846257323317, 3.17230019504959, 2.96611863265678, 
3.06769803788475, 3.47196832194708, 2.96463964047674, 3.06846257323317
), Monitor = c("Berthold Red", "Berthold Red", "Berthold Red", 
"Berthold Red", "Berthold Red", "Berthold Red", "Berthold Red", 
"Berthold Red", "Berthold Red", "Berthold Red", "Berthold Red", 
"Berthold Red", "Berthold Red", "Berthold Red", "Berthold Red", 
"Berthold Red", "Berthold Red", "Berthold Red", "Berthold Red", 
"Berthold Red", "Berthold Red", "Berthold Red", "Berthold Red", 
"Berthold Red", "Berthold Red", "Berthold Blue", "Berthold Blue", 
"Berthold Blue", "Berthold Blue", "Berthold Blue", "Berthold Blue", 
"Berthold Blue", "Berthold Blue", "Berthold Blue", "Berthold Blue", 
"Berthold Blue", "Berthold Blue", "Berthold Blue", "Berthold Blue", 
"Berthold Blue", "Berthold Blue", "Berthold Blue", "Berthold Blue", 
"Berthold Blue", "Berthold Blue", "Berthold Blue", "Berthold Blue", 
"Berthold Blue", "Berthold Blue", "Berthold Blue", "Berthold Blue", 
"NC 61", "NC 61", "NC 61", "NC 61", "NC 61", "NC 61", "NC 61", 
"NC 61", "NC 61", "NC 61", "NC 61", "NC 61", "NC 61", "NC 61", 
"NC 61", "NC 61", "NC 61", "NC 61", "NC 61", "NC 61", "NC 61", 
"NC 61", "NC 61", "NC 61", "NC 61", "NC 61", "NC 61", "Mini Red", 
"Mini Red", "Mini Red", "Mini Red", "Mini Red", "Mini Red", "Mini Blue", 
"Mini Blue", "Mini Blue", "Mini Blue", "Mini Blue", "Mini Blue"
)), .Names = c("Date", "Sensitivity.100cm", "Sensitivity.50cm", 
"Error.100cm", "Error.50cm", "Monitor"), class = "data.frame", row.names = c(NA, 
90L))
library(reshape2)
data.melt <- melt(big_df[,c(1:3,6)],id=c("Date","Monitor"))
data.melt2 <- melt(big_df[,c(1,4:6)],id=c("Date","Monitor"))

levels(data.melt2$variable) <- levels(data.melt$variable)

data.melt <- merge(data.melt,data.melt2,by=c("Date","Monitor","variable"))

library(ggplot2)
ggplot(data.melt, aes(x=Date, y=value.x, col=variable)) + 
  geom_point() + 
  geom_errorbar(aes(ymin=value.x-value.y,ymax=value.x+value.y)) +
  facet_wrap(~Monitor)

One solution is to make two melted data frames - one for values other for errors. In second melt() function changed value.name= to value2 to have different names.

data.melt1 <- melt(big_df[,c(1:3,6)],id=c("Date","Monitor"))
data.melt2 <- melt(big_df[,c(1,4,5,6)],id=c("Date","Monitor"),value.name="value2")

Now put together first data frame with value2 column of second data frame.

data.tog<-cbind(data.melt1,data.melt2["value2"])

head(data.tog)
        Date      Monitor          variable    value   value2
1 18/10/2012 Berthold Red Sensitivity.100cm 23.93103 1.313970
2 19/10/2012 Berthold Red Sensitivity.100cm 23.67925 1.564446
3 22/10/2012 Berthold Red Sensitivity.100cm 23.07087 1.401929
4 23/10/2012 Berthold Red Sensitivity.100cm 23.75000 1.369701
5 23/10/2012 Berthold Red Sensitivity.100cm 31.33333 1.674979
6 26/10/2012 Berthold Red Sensitivity.100cm 25.13514 1.330927

In geom_errorbar() use value+value2 and value-value2 to set ymax and ymin .

ggplot(data.tog, aes(x=Date, y=value, col=variable)) + 
  geom_point() + 
  geom_errorbar(aes(ymin=value-value2,ymax=value+value2))+facet_wrap(~Monitor)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM