我有一些来自实验的蒸腾数据,我想在使用 R 的折线图上显示为时间序列。我还有一些降水数据,我想在同一张图上显示为条形图。我已经能够使用 R 的基本程序来做到这一点,但我想在 ggplot 中做到这一点。我到处搜索,我知道设计师不太喜欢以这种方式制作图表,所以这很困难,但我已经看到它使用两个 y 轴使用多个线图/散点图完成。可以用折线图和条形图来完成吗?
这是我用来制作情节的数据
这是上面情节的代码。
attach(summary)
library(Hmisc)
library(scales)
par(mar=c(6.5,4,4,5)+.1)
plot(summary$dates,summary$c_mean_am,type="n",ylim=c(100,350),
main="Stomatal Conductance during experiment",las=1,cex.main=1,
font.lab=2,font.axis=2,cex.axis=0.7,cex.lab=0.8,
ylab=expression('Stomatal conductance'~(m~mol~ m^{2})),,xlab="Date")
lines(dates,c_mean_am,pch=21,cex=0.6,bg="blue",col="blue")
lines(dates,T1_mean_am,pch=21,cex=0.6,bg="yellow",col="yellow")
lines(dates,T2_mean_am,pch=21,cex=0.6,bg="hotpink1",col="hotpink1")
lines(dates,T3_mean_am,pch=21,cex=0.6,bg="orange",col="orange")
lines(dates,T4_mean_am,pch=21,cex=0.6,bg="red",col="red")
with (data = summary , expr = errbar(dates, c_mean_am,
c_mean_am+c_se_am,
c_mean_am-c_se_am,
add=T, pch=21,col="blue",bg="blue",
cex=0.6,cap=0.01,errbar.col="blue"))
with (data = summary , expr = errbar(dates, T1_mean_am,
T1_mean_am+T1_se_am,
T1_mean_am-T1_se_am, add=T,
pch=21,col="yellow",bg="yellow",
cex=0.6,cap=0.01,errbar.col="yellow"))
with (data = summary , expr = errbar(dates, T2_mean_am,
T2_mean_am+T2_se_am,
T2_mean_am-T2_se_am,
add=T, pch=21,col="hotpink1",
bg="hotpink1",cex=0.6,cap=0.01,
errbar.col="hotpink1"))
with (data = summary , expr = errbar(dates, T3_mean_am,
T3_mean_am+T3_se_am,
T3_mean_am-T3_se_am,
add=T, pch=21,col="orange",
bg="orange",cex=0.6,cap=0.01,
errbar.col="orange"))
with (data = summary , expr = errbar(dates, T4_mean_am,
T4_mean_am+T4_se_am,
T4_mean_am-T4_se_am, add=T,
pch=21,col="red",bg="red",
cex=0.6,cap=0.01,errbar.col="red"))
data2<-Rainfall
names(data2)
par(new=TRUE)
graph<-tapply(data2$`daily rainfall`,data2$DATE,I)
barplot(graph,col="light blue",border=NA,xaxt="n",yaxt="n",xlab="",ylab="",ylim=c(0,150))
axis(4,las="1",cex.axis=0.7,font.axis=2)
mtext("Rainfall (mm)",side=4,line=3,font=2,cex=0.8)
这是 ggplot 时间序列图和格式略有不同的相同数据。
这是代码
dt<-am_means
attach(dt)
dt$dates <- as.Date(dt$dates,format = "%d/%m/%y")
sp<-ggplot(dt,aes(x=dates,y=cond,colour=Treatment,group=Treatment))+geom_errorbar
(aes(ymin=cond-err,ymax=cond+err),width=0.5,size=0.5)+geom_line()+geom_point()+scale_x_date
(date_breaks = "2 weeks",date_minor_breaks = "1 week",date_labels = "%b %d")
sp2<-sp + scale_color_manual(breaks = c("Control", "T1","T2","T3","T4"),
values=c("blue", "yellow","hotpink1","orange","red"))
print(sp2 +ylim(100, 350)+labs(title= "Stomatal Conductance - Tamata Maples",
y=expression(Conductance (m~mol~m^{-2})), x = "Date"))
我可以在同一个ggplot折线图上绘制降雨条图吗????