0

I am trying to do a plt with two y axis but, the scales are really different and I am having trouble with the errors bars of one of the variables. Here is the code:

date<-as.Date(c("2008-10-01","2009-09-01","2010-11-01","2011-09-01",
              "2012-10-01","2013-09-01","2014-09-01","2015-09-01",
              "2016-09-01","2017-09-01"))
year2<-c(2008,2009,2010,2011,2012,2013,2014,2015,2016,2017)
evi.total<-c(0.074,0.064,0.066,0.060,0.073,NA,0.064,0.073,0.076,0.076)
densidad.total<-c(22.78,17.19,19.39,16.46,32.34,NA,13.62,26.41,26.05,34.77)
se.dens.tot<-c(4.50, 4.76,4.13,3.93,7.75,NA,3.51,5.63,4.86,7.02)
sd.evi.general<-c(0.011,0.010,0.011,0.010,0.011,NA,0.012,0.011,0.012,0.014)
n<-c(5018,5018,5018,5018,5018,NA,5018,5018,5018,5018)
se.evi<-sd.evi.general/(sqrt(n))

data<-data.frame(date,year2,evi.total,densidad.total,se.dens.tot,sd.evi.general,
                 se.evi)

library(ggplot2)

p <- ggplot(data=data,aes(x = date))
p<-p + scale_x_date(date_breaks="3 month", date_labels = "%b-%y", 
                  )                            
p <- p + geom_line(aes(y =densidad.total, colour = "Guanaco's density"),size=.5)+
         geom_linerange(aes(y=densidad.total,ymin=densidad.total-se.dens.tot, 
                            ymax=densidad.total+se.dens.tot), 
                            color="tomato1", size=1,lwd=.07)

p <- p + geom_line (aes(y =evi.total*1000, colour = "EVI"),size=.5) +  
  labs(color=NULL) + geom_linerange(aes(y=evi.total*1000,
                                    ymin=evi.total*1000-se.evi*1000, 
                                    ymax=evi.total*1000+se.evi*1000),
                                     color="aquamarine4",size=1)
                             

p <- p + scale_y_continuous(sec.axis = sec_axis(~./1000, name = " EVI"))

p <- p + scale_colour_manual(values = c("aquamarine4", "tomato1"))
p <- p +theme_classic()

p <- p + labs(y = expression ("Guanaco's density "(guanaco/~km^2), x = "",
              colour = "")) 
p <- p + theme(legend.position = "bottom")+
  theme(text = element_text(size = 20,hjust = 1),  
        axis.line.x = element_line(color="black", size = 1),
        axis.line.y = element_line(color="black", size = 1),
        axis.text.x=element_text(colour="black",angle=60, size=10),
        axis.text.y=element_text(colour="black"))


p

With this code I get this

I have seen here that I should change the secondary axis scale, but, It seems that the standar error is really small and is difficult to seee in the plot.

1 Answers1

1

This has nothing to do with a secondary axis. You simply haven't added or subtracted the standard deviation from evi.total in your second geom_linerange.

You have set ymin=evi.total*1000, ymax=evi.total*1000. The minimum and maximum values are the same, so there is nothing to draw. It should be ymin = (evi.total - sd.evi.general) * 1000, ymax = (evi.total + sd.evi.general) * 1000

ggplot(data, aes(x = date)) +
  geom_line(aes(y = densidad.total, colour = "Guanaco's density"), size = 0.5) +
  geom_linerange(aes(ymin = densidad.total - se.dens.tot, 
                     ymax = densidad.total + se.dens.tot), 
                 color = "tomato1", lwd = 0.7) +
  geom_line (aes(y = evi.total * 1000, colour = "EVI"), size = 0.5) + 
  geom_linerange(aes(ymin = (evi.total - sd.evi.general) * 1000, 
                     ymax = (evi.total + sd.evi.general) * 1000),
                 color = "aquamarine4", lwd = 0.7) +
  scale_y_continuous(sec.axis = sec_axis(~./1000, name = " EVI")) +
  scale_x_date(NULL, date_breaks="3 month", date_labels = "%b-%y") +
  labs(y = expression ("Guanaco's density "(guanaco/~km^2))) +
  scale_colour_manual(NULL, values = c("aquamarine4", "tomato1")) +
  theme_classic() +
  theme(legend.position = "bottom",
        text            = element_text(size = 20, hjust = 1),  
        axis.line.x     = element_line(color = "black", size = 1),
        axis.line.y     = element_line(color = "black", size = 1),
        axis.text.x     = element_text(color = "black", angle = 60, size = 10),
        axis.text.y     = element_text(color = "black"))

enter image description here

If you need to use standard errors instead of standard deviation, these are so small that they are difficult to see. You could stretch them out maximally by doing:

ggplot(data, aes(x = date)) +
  geom_line(aes(y = densidad.total, colour = "Guanaco's density"), size = 0.5) +
  geom_linerange(aes(ymin = densidad.total - se.dens.tot, 
                     ymax = densidad.total + se.dens.tot), 
                 color = "tomato1", lwd = 0.7) +
  geom_line (aes(y = evi.total * 4000 - 240, colour = "EVI"), size = 0.5) + 
  geom_linerange(aes(ymin = (evi.total - se.evi) * 4000 - 240, 
                     ymax = (evi.total + se.evi) * 4000 - 240),
                 color = "aquamarine4", lwd = 0.7) +
  scale_y_continuous(sec.axis = sec_axis(~(. + 240)/4000, name = " EVI")) +
  scale_x_date(NULL, date_breaks="3 month", date_labels = "%b-%y") +
  labs(y = expression ("Guanaco's density "(guanaco/~km^2))) +
  scale_colour_manual(NULL, values = c("aquamarine4", "tomato1")) +
  theme_classic() +
  theme(legend.position = "bottom",
        text            = element_text(size = 20, hjust = 1),  
        axis.line.x     = element_line(color = "black", size = 1),
        axis.line.y     = element_line(color = "black", size = 1),
        axis.text.x     = element_text(color = "black", angle = 60, size = 10),
        axis.text.y     = element_text(color = "black"))

enter image description here

A lesson I have learned over the years is that it is easier to make this kind of mistake when your code is hard to read. It's worth investing a little time to make your code easier to read by adding spaces between names and operators, following consistent indentation and avoiding unnecessary repetition. It can save hours of looking for a bug hiding in plain sight.

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Apologies, I 've been dealing with this plot so long that I have deleted the standard error in the plot. I have added it now in the code. I can't work with standard deviation, I need to work with standard error. – user228095 Aug 08 '23 at 19:32
  • @user228095 then what is the problem? The calculated standard error is tiny in comparison to the range of evi, so you can't see the error bars unless you zoom in - if those are really the standard deviations and the sample size, then that is indeed the standard error. – Allan Cameron Aug 08 '23 at 19:49
  • I see that, I was wondering if there is any way to change the second y-axis starting point to make it easier to visualize. – user228095 Aug 08 '23 at 20:04
  • @user228095 yes, see my update. This is really as large as you can make the standard errors. – Allan Cameron Aug 08 '23 at 20:12