使用ggplot

时间:2017-07-19 14:51:23

标签: r ggplot2 bar-chart yaxis

我有一些来自实验的蒸腾数据,我希望在使用R的线图上显示时间序列。我也有一些降水数据,我想在条形图上显示在同一图表上。我已经能够使用R的基本程序做到这一点,但我想在ggplot中这样做。我到处搜索,我知道设计师不太喜欢用这种方式制作图形所以它很难,但我已经看到它使用两个y轴完成了多个线图/散点图。可以用折线图和条形图完成吗?

这是我在基本R中所拥有的 Transpiration time series

以下是我用来制作情节的数据

Time series data here

Rainfall data here

以下是上图的代码。

 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时间序列图和the same data格式略有不同。

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线图上使用降雨条图吗????

2 个答案:

答案 0 :(得分:4)

正如从ggplot2版本2.2.0开始的好示例herehere所述,可以添加secondary axis

有人可以试试这个:

准备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")]

使用辅助OY轴绘图:

转换出现在第二个OY (右侧)的数据非常重要。由于最大值比第一个OY轴(左侧)的数据小约2倍,因此可以乘以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

enter image description here

使用scale_y_continuous添加第二个OY轴。 请注意转换回来。如果超过2,我们现在将数据除以2:

my_plot <- my_plot + scale_y_continuous(sec.axis = sec_axis(trans = ~ . / my_factor, 
                                                            name = "Rainfall (mm)"))

继续使用OP的代码

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

enter image description here

其他一些相关的已回答问题:adding a cumulative curvecombining Bar and Line chart

答案 1 :(得分:0)

IF @lvFunction = 6 BEGIN SET @lvSQL = 'UPDATE [dbo].[Backup_Paths] SET [dbo].[Backup_Paths].[Last Executed] = @lvLastExecuted WHERE [dbo].[Backup_Paths].[AAA Client ID] = @lvAAAID AND [dbo].[Backup_Paths].[Process Name] = @lvName AND [dbo].[Backup_Paths].[UNC Path] = @lvPath' END SET @lvSQL = @lvSQL + ' WHERE [dbo].[Backup_Paths].SomeProp = 7' execute sp_executesql @lvSQL, '@lvFunction int, @lvECMID varchar(12), @lvECMID_new varchar(12), @lvName varchar(35), @lvName_new varchar(35), @lvPath varchar(255), @lvPath_new varchar(255), @lvLastExecuted datetime', @lvFunction, @lvECMID, @lvECMID_new, @lvName, @lvName_new, @lvPath, @lvPath_new, @lvLastExecuted; 中没有多轴,它是该套装的创建者Hadley Wickham的选择。你不能。

来源:https://stackoverflow.com/a/3101876/8031980