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.