英文:
How to plot error bars in a plot with two y axis in diffrent scales?
问题
我正在尝试使用两个y轴绘制plt图,但是,它们的刻度差异很大,而且我在其中一个变量的误差条上遇到了问题。以下是代码:
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
使用这段代码,我得到了这个图像。
我在这里看到应该更改次要轴的刻度,但是,似乎标准误差非常小,在图中很难看到。
英文:
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
得分: 1
这与次轴无关。在第二个geom_linerange
中,你只是没有从evi.total
中加减标准差。
你设置了ymin=evi.total*1000, ymax=evi.total*1000
。最小值和最大值是相同的,所以没有什么可以绘制的。应该是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 * 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"))
多年来我学到的一课是,当你的代码难以阅读时,犯这种错误会更容易。值得花一点时间使你的代码更易读,通过在名称和运算符之间添加空格,遵循一致的缩进并避免不必要的重复。这可以节省数小时的寻找明显错误的时间。
英文:
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"))
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"))
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论