0
votes

I have an issue with geom_ribbon, and with its limits. i have a datatable like that (Length and Perf being means, and Length.sd and Perf.sd, the standard deviation)

Ex <- data.frame(Temperature = as.factor(c("16", "16", "16", "16", "16")),
                Level = Level = as.factor(c(0, 1, 2, 3, 4)), Length = c(1.623333, 2.161143, 2.924545, 3.895429, 5.068788),
                Length.sd = c(0.1578897, 0.1161331, 0.1850691, 0.1691039, 0.1937743),Perf = c(0.6693438, 0.5292980, 0.1979450, 0.1034701, 0.0827987),
                Perf.sd = c(0.04479068, 0.07299800, 0.02513500, 0.00876010, 0.00679870))

Then, I built my graph, I use geom point for the mean, geom_errorbar for the vertical error bars, geom_errorbarh for the vertical error bars, and stat_function to draw the linear regression of my dataset. Until now, everything is fine

ggplot(data=Ex, aes(x=log10(Length), y=log10(Perf), color=Temperature))+ 
  geom_point(aes(shape=Level), size = 2)+
  geom_errorbarh(aes(xmax = log10(Ex$Length + Ex$Length.sd), xmin = log10(Ex$Length - Ex$Length.sd), height = 0.01))+
  geom_errorbar(aes(ymax = log10(Ex$Perf + Ex$Perf.sd), ymin = log10(Ex$Perf - Ex$Perf.sd), width = 0.01))+
  scale_color_manual(values=c("blue"))+
  scale_shape_manual(values=c(7, 15, 17, 19, 6))+
  stat_function(fun=function(x){0.2746- 1.9945*x}, colour="red", size=1) +
  theme_bw() + 
  scale_x_continuous("Length") + 
  scale_y_continuous("Perf")

enter image description here

Then, the geom_ribbon.

ggplot(data=Ex, aes(x=log10(Length), y=log10(Perf), color=Temperature))+ 
  geom_point(aes(shape=Level), size = 2)+
  geom_errorbarh(aes(xmax = log10(Ex$Length + Ex$Length.sd), xmin = log10(Ex$Length - Ex$Length.sd), height = 0.01))+
  geom_errorbar(aes(ymax = log10(Ex$Perf + Ex$Perf.sd), ymin = log10(Ex$Perf - Ex$Perf.sd), width = 0.01))+
  scale_color_manual(values=c("blue"))+
  scale_shape_manual(values=c(7, 15, 17, 19, 6))+
  stat_function(fun=function(x){0.2746- 1.9945*x}, colour="red", size=1) +
  geom_ribbon(aes(ymax= 0.3186 -2.1155*x+0.1197*x^2 , 
                  ymin= 0.2338  -1.8895*x-0.1033*x^2 ), fill="red", colour= NA, alpha=.2) + #CI 16
  theme_bw() + 
  scale_x_continuous("Length") + 
  scale_y_continuous("Perf")

First, I have a error message, like if it doesn't see the x=log10(Length) in aes

Error: Aesthetics must be either length 1 or the same as the data (5): ymax, ymin, x, y

Then, if I write before the ggplot x=log10(Ex$Length), it works. But the ribbon stops at the means of the values, and doesn't extend until the extremes enter image description here

But if I try to change the x, like x=seq(0.1, 1, by=1), I have the error message

Error: Aesthetics must be either length 1 or the same as the data (5): ymax, ymin, x, y

like if geom_ribbon doesn't see I use a function to draw the upper and lower limits of the ribbon. it works perfectly if I remove geom_errorbarh, but I need it.

Do somebody would have a solution to force the ribbon to extend until determined limits? Thank you very much!

1
I'm trying to figure out how geom_ribbon worked at all. I would have thought you'd get an object x not found error. In addition, all instances of Ex$ should be removed. It's unnecessary and undesirable to restate the data frame name inside aes.eipi10
Are the points in this plot summaries of a larger raw data set? If so, you can just use the raw dataset to generate the regression line and confidence ribbon using geom_smootheipi10

1 Answers

1
votes

I think this is what you want

x=log10(Ex$Length)
x_new = c(0,log10(Ex$Length), 1)
z_new = data.frame(x = x_new, ymin = 0.2338  -1.8895*x_new-0.1033*x_new^2, ymax = 0.3186 -2.1155*x_new+0.1197*x_new^2)
ggplot(data=Ex)+ 
   geom_point(aes(x=log10(Length), y=log10(Perf), color=Temperature,shape=Level), size = 2)+
   geom_errorbarh(aes(x=log10(Length),y=log10(Perf), xmax = log10(Ex$Length + Ex$Length.sd), xmin = log10(Ex$Length - Ex$Length.sd), height = 0.01, color = Temperature))+
   geom_errorbar(aes(x=log10(Length),ymax = log10(Ex$Perf + Ex$Perf.sd), ymin = log10(Ex$Perf - Ex$Perf.sd), width = 0.01,  color = Temperature))+
   scale_color_manual(values=c("blue"))+
   scale_shape_manual(values=c(7, 15, 17, 19, 6))+
   stat_function(fun=function(z){0.2746- 1.9945*z}, colour="red", size=1) +
   geom_ribbon(data = z_new, aes(x = x, ymin= ymin, ymax = ymax), fill="red", colour= NA, alpha=.2) + #CI 1
   theme_bw() + 
   scale_x_continuous(limits = c(0,1), "Length") + 
   scale_y_continuous("Perf")

For whatever reason stat_function is evaluating your function over the limits of the x-axis of the graph and not the limits of the variable Length in your data set. However, geom_ribbon is only being plotted within the limits of the variable Length which is why they do not match up. I'm not sure why this happens but this is how I would go about getting around it. First decide on a limit for the x-axis using scale_x_continuous(limits = c(0,1), "Length"). Next, create a new x variable (x_new) that spans the range of the limits of the x axis. Then, create a data frame that evaluates the ymin and ymax for each value of x_new according to your equation (z_new). Use this data set in the geom_ribbon function. BUT to do this you need to remove the aesthetics from the first line of the ggplot function and specify them individually for each element in the plot(geom_point, geomerrorbarh, geom_errorbar). This is annoying but necessary to incorporate variables from outside of the main data set in other aspects of the plot.

An easier way to do this would be to use stat_smooth to run and plot the linear model. This function automatically plots a 95% confidence interval (which can be changed by adding level = some number between 0 and 1). To suppress the automatically generated CI use se = FALSE. It will only plot the line to the extent of the variable Length in your data. You can then use geom_ribbon as you've already specified. If there is no reason for the line to extend beyond the data, this is much easier.

ggplot(data=Ex, aes(x=log10(Length), y=log10(Perf)))+ 
 geom_point(aes(shape=Level), size = 2, color = "blue")+
 geom_errorbarh(aes(xmax = log10(Ex$Length + Ex$Length.sd), xmin = 
 log10(Ex$Length - Ex$Length.sd), height = 0.01), color = "blue")+
 geom_errorbar(aes(ymax = log10(Ex$Perf + Ex$Perf.sd), ymin = log10(Ex$Perf - Ex$Perf.sd), width = 0.01), color = "blue")+
 scale_shape_manual(values=c(7, 15, 17, 19, 6))+
 stat_smooth(method = "lm", color = "red", alpha = .2, se = FALSE) +
 geom_ribbon(aes(ymax= 0.3186 -2.1155*x+0.1197*x^2 , 
              ymin= 0.2338  -1.8895*x-0.1033*x^2 ), fill="red", colour= NA, alpha=.2) + #CI 16
 theme_bw() + 
 scale_x_continuous("Length") + 
 scale_y_continuous("Perf")