[R] Black outlines for errorbar using geom_line

Jim Lemon drjimlemon at gmail.com
Sun May 3 13:43:17 CEST 2015


Hi Michael,
I don't know about ggplot, but it is fairly easy to create outlined or
even shadowed errorbars:

require(plotrix)
# display thick black errorbars
dispersion(...,lwd=3)
# display thin white errorbars
dispersion(...,col="white")

The above trick produces white errorbars outlined in black. I think it
will work for any routine producing error bars, not just the
"dispersion" function. You may be able to simply add two geom_errorbar
components to the call in the same way as above.

Jim


On Sat, May 2, 2015 at 8:18 AM,  <michael.eisenring at agroscope.admin.ch> wrote:
> Hi there,
>  I would like to create black outlines around my errorbars in order to get especially the white errorbar better visible.
> Is that even possible with ggplot2 and if yes how?
> I would be very grateful if anyone could help me. I added my code and a dput() of my data,
>
> Thank you very much,
> Michael
>
>
> #my code:--------------------------------------------------
>
>
> #load packages
> library(compute.es<http://compute.es/>);library(ggplot2);library(multcomp);library(pastecs);library(WRS)
> library(pastecs);library(Hmisc)
> library (car)
>
>
> # Comput CI manually and produce error plots
>
> #CI manually computed with formula summarySE
> summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
>                       conf.interval=.95, .drop=TRUE) {
>   library(plyr)
>
>   # New version of length which can handle NA's: if na.rm==T, don't count them
>   length2 <- function (x, na.rm=FALSE) {
>     if (na.rm) sum(!is.na<http://is.na/>(x))
>     else       length(x)
>   }
>
>   # This does the summary. For each group's data frame, return a vector with
>   # N, mean, and sd
>   datac <- ddply(data, groupvars, .drop=.drop,
>                  .fun = function(xx, col) {
>                    c(N    = length2(xx[[col]], na.rm=na.rm),
>                      mean = mean   (xx[[col]], na.rm=na.rm),
>                      sd   = sd     (xx[[col]], na.rm=na.rm)
>                    )
>                  },
>                  measurevar
>   )
>
>   # Rename the "mean" column
>   datac <- rename(datac, c("mean" = measurevar))
>
>   datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean
>
>   # Confidence interval multiplier for standard error
>   # Calculate t-statistic for confidence interval:
>   # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
>   ciMult <- qt(conf.interval/2 + .5, datac$N-1)
>   datac$ci <- datac$se * ciMult
>
>   return(datac)
> }
>
> #summarySE provides the standard deviation, standard error of the mean, and a (default 95%) confidence interval
> CI <- summarySE(data, measurevar="cor_average", groupvars=c("damage","leaf"))
> CI
>
>
> #plotting in ggplot
> # To prevent that errorbars overlap use position_dodge to move them horizontally
> pd <- position_dodge(0.4) # move them .05 to the left and right
> #Create errorbar using 95% CI
> ggplot(CI, aes(x=leaf, y=cor_average, colour=damage)) +
>   geom_errorbar(aes(ymin=cor_average-ci, ymax=cor_average+ci), width=.3, size=1,position=pd) +
>   geom_line(position=pd) +
>   scale_colour_manual(values=c("white","black","gray65"))+
>   geom_point(position=pd,size=3)
>
>
>
> #my data set: ---------------------------------------------------------------------------------------------------------------
>
>> dput(data<-read.csv("Exp1.csv",sep=",",header=T))
> structure(list(damage = 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,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 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,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 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666667, 2880, 2760, 2210,
> 2142, 2508, 1551.666667, 1856, 498.7777778, 2634.666667, 2380,
> 2338.333333, 2006.666667, 3296.666667, 2945.333333, 2720.666667,
> 2436, 2033, 2579.333333, 455.1111111, 2123.333333, 2033.333333,
> 3660.333333, 2765, 3752, 3150, 3747.333333, 3572, 3536, 3593.333333,
> 1178.777778, 3476, 3291.666667, 3927.333333, 3705, 4636.666667,
> 3720, 4080, 4828, 5280, 4495, 711.1111111, 4340, 4173, 4785,
> 5070, 6324, 3360, 5150, 6262.666667, 5624, 5595.333333, 1626.777778,
> 5869.333333, 5080, 6888, 7561.333333, 7280, 3610, 5926.666667,
> 6600, 6375.333333, 7831.666667, 4181.777778, 8023, 6077.333333,
> 14640, 13501.33333, 15383.33333, 6193, 10030, 8756.333333, 10291.66667,
> 16450, 33611.11111, 15138.66667, 10948, 6600, 7785, 10944, 11712,
> 17490, 14370, 10353, 8814.333333, 742.5, 605, 520, 728.5, 418,
> 650, 468.6666667, 506.6666667, 512, 518, 646, 1974, 2408, 2494,
> 1931.666667, 1243.666667, 2024, 1805, 2166.666667, 2408, 1613.333333,
> 2573, 2102.333333, 2470, 2701, 2566.666667, 2960, 2300, 2579.666667,
> 1785, 2223.333333, 2046, 3336.666667, 3737, 4529.666667, 3718,
> 3192, 4110, 3198, 4116, 2645, 3288, 3315, 4850, 4293.333333,
> 4972, 4641, 4370, 4933.333333, 3853.333333, 4658, 3720, 4340,
> 3882.666667, 5577, 7800, 7646.333333, 4200, 5418, 5324.666667,
> 4564, 5085, 5060, 7093.333333, 6923.333333, 7700, 14575, 14725,
> 6650, 12154.66667, 8254.666667, 5800, 7663.333333, 6640, 12661.66667,
> 12982.33333, 14025, 17148, 21960, 10418.33333, 25850, 12636,
> 8470, 17152, 20603.33333, 22078, 20086.66667, 24219, 18544, 13475,
> 23389, 22374, 10246.5, 13695, 12220, 487.5, 645, 623.3333333,
> 493.3333333, 555, 532, 658.6666667, 522, 492, 528, 604.3333333,
> 528, 1998.333333, 2016.666667, 1971.666667, 2376, 1780, 1729,
> 2026.666667, 2186.666667, 1860, 2112, 2300, 2054.666667, 2520,
> 2532.333333, 2181.666667, 2960.333333, 2220, 2472, 2555, 1392,
> 2826.666667, 1457, 2600, 2026.666667, 3720, 4275, 4389, 3014,
> 2875, 3920, 4094, 2712, 3853.333333, 2926, 4766.666667, 3948.333333,
> 5625, 6303.333333, 4783.666667, 4770, 3530.333333, 4840, 3760,
> 4656.666667, 3600, 3923.333333, 5458.333333, 3600, 5115, 5950,
> 5662.666667, 6075, 4307.333333, 5960, 5425, 4978, 6292, 4477.333333,
> 5568, 5364.333333, 8824.666667, 8520, 8000, 11293.33333, 5133.333333,
> 7973.333333, 5786.666667, 6006, 6766.666667, 7000, 7566, 15555,
> 14299.33333, 19716.66667, 24408, 11349, 17400, 13362, 19525,
> 23902, 15364, 16875, 10450, 17605, 22950, 19920, 12412, 21187.33333,
> 20425, 20710, 10280.5, 11063, 21498.66667, 22333.33333)), .Names = c("damage",
> "leaf", "cor_average"), class = "data.frame", row.names = c(NA,
> -297L))
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list