Graph with errobar using percentile function

ggplot2

#1

Hello,
I am new to R-studio, I attended a course in which I discovered this program and now I would like to use it for an other project of mine. I would like to create a line plot with erro bars, but I have some issue with my following code :

Percentile <- function(perc) function(x)

perc is the percentile which should be computed for the numeric vector x

quantile(x, perc*1e-2, na.rm=TRUE)

ggp <- ggplot(data=TESTOVER,
mapping=aes(x=wavelength, y=value)) +
geom_line(stat="summary", fun.y="median")+
geom_errorbar(stat="summary",
fun.ymin=Percentile(25),
fun.ymax=Percentile(75))+
print(ggp)

This message appears, even though I have no zero in my datasheet : Erreur : Don't know how to add o to a plot
De plus : Warning messages:
1: Removed 146 rows containing non-finite values (stat_summary).
2: Removed 146 rows containing non-finite values (stat_summary).

It may come from the structure of my datasheet, I don't really know ...

I would be very happy with any help you could provide

Jessica


#2

One obvious error is the + print(ggp) part. Put this on a separate line without the +.


#3

Welcome! Your code should run with the correction suggested by @martin.R, but I’d suggest one other modification to make it clearer: it’s a good idea to use curly braces whenever you have a function definition that extends over more than one line. Like so:

Percentile <- function(perc) {
  # perc is the percentile which should be computed for the numeric vector x
  function(x) quantile(x, perc*1e-2, na.rm=TRUE)
}

Also, for future reference (because I hope you’ll ask more questions! :grin: ), here’s how to make code you post here appear with nice formatting :sparkles: (and not trigger other automatic formatting, like how your comment was turned into a section heading):


#4

Hello,

Thank you to you both for you answers it was indeed a problem with the + :wink: And thank you to you too @jcblum for your suggestion and the link for the formatting, I was wondering why it had done the heading ^^ One last question that is maybe more a statistical one, is it correct to calculate the mean and not the average as the string of the plot ?


#5

If I’m understanding this right, you’re wondering about whether the median or the mean is a more appropriate measure of central tendency? That’s really a domain- and data-specific question, so it’s hard to say without seeing your data — and even then the answer depends on what your analysis goals are.

The common advice is to favor the median when the data are skewed, since the mean is sensitive to outliers and extreme values, however as in most things statistical it’s important to exercise judgement rather than applying blanket rules. Lots of useful perspectives in this discussion from Cross Validated:

(If I’ve misunderstood and that’s not what you’re asking at all, I apologize!)


#6

Thank you @jcblum for you answer, you understood my question very clearly even though I wrote it in a confuse way. Thanks for the link to the cross validated discussion :slight_smile: See you around


#7

If your question's been answered, would you mind choosing a solution? (see FAQ below for how) It makes it a bit easier to visually navigate the site and see which questions still need help.

Thanks