问题描述
我有一些实验中的蒸腾数据,我想用R表示一个线图上的时间序列。我还有一些降水数据,我想在同一个图上显示为条形图。我已经能够使用R的基本程序来做到这一点,但我想在ggplot中做到这一点。我到处搜索过,并且我知道设计师不太喜欢用这种方法制作图形,所以很困难,但是我已经看到它使用两个y轴进行多线图/散点图的绘制。它可以通过线图和条形图来完成吗?
以下是我在基本R
以下是我用来绘制图表的数据
以下是代码
dt 附加(dt)
dt $日期< - as.Date(dt $日期,格式=%d /%m /%y)
sp 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 values = c(blue,yellow,hotpink1,orange,red))
print(sp2 + ylim(100,350)+ labs (标题=气孔导度 - Tamata Maples,
y =表达式(电导率(m〜mol〜m ^ { - 2})),x =日期))
我可以在相同的ggplot折线图上绘制降雨量图吗?
正如
其他一些相关回答问题:或。
I have some transpiration data from an experiment that I would like to show as a time series on a line graph using R. I also have some precipitation data which I would like to show on the same graph as a bar plot. I've been able to do this using R's base program, but I'd like to do this in ggplot. I've searched everywhere and I know that the designers aren't too fond of making graphs this way so it's tough, but I have seen it done with multiple line graphs/scatter plots using two y axis. Can it be done with a line graph and a bar plot?
Here's what I have in basic R
Here is the data which I used to make the plot
And here is the code for the plot above.
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)
Here's the ggplot time series graph and the same data in a slightly different format.
And here's the code
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"))
Can I have the rainfall bar plot on the same ggplot line graph????
As described with nice examples here or here starting with version 2.2.0 of ggplot2
, is possible to add a secondary axis.
One could try this:
Prepare the provided data for ggplot
:
# Read data from OP's DropBox links
am_means <- read.csv("https://www.dropbox.com/s/z4dl0jfslhqccb8/am_means.csv?dl=1")
rainfall <- read.csv("https://www.dropbox.com/s/vkv9vm5o93ttk1i/Rainfall.csv?dl=1")
am_means$dates <- as.Date(am_means$dates, format = "%d/%m/%Y")
rainfall$DATE <- as.Date(rainfall$DATE,format = "%d/%m/%Y")
# join the two tables
my_data_all <- merge(x = am_means,
y = rainfall,
by.x = "dates",
by.y = "DATE",
all = TRUE)
# use data between desired date interval (rainfall had some extra dates)
require(data.table)
setDT(my_data_all)
my_data <- my_data_all[dates %between% c("2017-01-31", "2017-04-06")]
Plot with secondary OY axis:
Is important to transform the data appearing on 2nd OY (right-hand side). Since the max value is approx 2 times smaller that the one of the data from first OY axis (left-hand side), one can multiply by 2.
my_factor <- 2
my_plot <- ggplot(my_data,
aes(x = dates,
group = Treatment)) +
geom_errorbar(aes(ymin = cond - err,
ymax = cond + err,
colour = Treatment),
width = 0.5,
size = 0.5) +
geom_line(aes(y = cond, colour = Treatment)) +
geom_point(aes(y = cond, colour = Treatment)) +
# here the factor is applied
geom_bar(aes(y = daily.rainfall * my_factor),
fill = "light blue",
stat = "identity")
my_plot
Adding the 2nd OY axis with scale_y_continuous
. Note the transformation back. If above we multiplied by 2, now we divide data by 2:
my_plot <- my_plot + scale_y_continuous(sec.axis = sec_axis(trans = ~ . / my_factor,
name = "Rainfall (mm)"))
Continues with OP's code
my_plot <- my_plot + scale_x_date(date_breaks = "2 weeks",
date_minor_breaks = "1 week",
date_labels = "%b %d") +
scale_color_manual(breaks = c("Control", "T1", "T2", "T3", "T4"),
values = c("blue", "yellow", "hotpink1", "orange", "red")) +
labs(title = "Stomatal Conductance - Tamata Maples",
y = expression(Conductance (m~mol~m^{-2})),
x = "Date") +
theme_bw()
my_plot
Some other related answered questions: adding a cumulative curve or combining Bar and Line chart.
这篇关于使用ggplot绘制条形图和线图的多个y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!