Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
r中不同时间分辨率的堆栈图_R_Ggplot2_Graph_Stacked - Fatal编程技术网

r中不同时间分辨率的堆栈图

r中不同时间分辨率的堆栈图,r,ggplot2,graph,stacked,R,Ggplot2,Graph,Stacked,我正在尝试使用ggplot2和grid arrange堆叠三个图形。所有三个图形都应该具有相同的x轴(但不共享),问题是它们都处于不同的时间分辨率,我很难对齐三个不同绘图的轴 这就是我所拥有的: 我想要类似的东西: 土壤温度数据集 average 3/1/2018 11:00 9.353692972 3/1/2018 12:00 10.75947564 3/1/2018 13:00 11.56223312 3/1/2018 14:00 11.59511

我正在尝试使用ggplot2和grid arrange堆叠三个图形。所有三个图形都应该具有相同的x轴(但不共享),问题是它们都处于不同的时间分辨率,我很难对齐三个不同绘图的轴

这就是我所拥有的:

我想要类似的东西:

土壤温度数据集

                average
3/1/2018 11:00  9.353692972
3/1/2018 12:00  10.75947564
3/1/2018 13:00  11.56223312
3/1/2018 14:00  11.59511989
3/1/2018 15:00  11.07712308
3/1/2018 16:00  9.762939639
3/1/2018 17:00  7.650089417
3/1/2018 18:00  6.021789611
3/1/2018 19:00  4.844122056
3/1/2018 20:00  3.946675139
3/1/2018 21:00  3.265207733
3/1/2018 22:00  2.751823167
3/1/2018 23:00  2.307551222
3/2/2018 0:00   1.977323322
3/2/2018 1:00   1.714310775
3/2/2018 2:00   1.505199708
3/2/2018 3:00   1.402693267
3/2/2018 4:00   1.384921586
3/2/2018 5:00   1.350009046
....
数据集ppt

3/1/2018    0
3/2/2018    0
3/3/2018    0
3/4/2018    0
3/5/2018    0
3/6/2018    0
3/7/2018    0
3/8/2018    0
3/9/2018    0
3/10/2018   0
3/11/2018   0
3/12/2018   0
3/13/2018   0
3/14/2018   0
数据集流量

DPF     U       N
7.08    0.00    0.00
14      0.01    0.02
22      0.16    0.25
29.21   0.00    0.00
33.88   0.05    0.00
42.08   0.00    0.00
代码

#定义工作表
每小时

安装程序 为了便于再现,我将使用
mtcars
数据集 演示如何解决这个问题。首先,让我们创建三个图:

库(ggplot2)
种子集(123)
dfs
# define sheets
hourly <- read_excel("ashland2_graphs.xlsx", sheet = 'Hourly')
daily <- read_excel("ashland2_graphs.xlsx", sheet = 'Climate')
nh3flux <-read_excel('ashland2_graphs.xlsx', sheet = 'flux')


# define soil temp
soiltemp <- as.numeric(as.character(hourly$average))

# soil temperature graph
# define x axis
x = c(1:length(soiltemp))

fig1 <- ggplot()+
  geom_line(aes(x=x,y = soiltemp), stat = 'identity') +
  scale_x_continuous(breaks = seq(1, length(soiltemp), by = 240), 
                     label = c('0', '10', '20', '30', '40'))

# precipitation graph
ppt <- as.numeric(as.character(daily$ppt))

# precip x axis
x2 = c(1:length(ppt))

fig2 <- ggplot(climate) +
  geom_bar(aes(x = x2, y = ppt), stat = 'identity', color = "grey") +
  scale_y_continuous(expand = c(0, 0))

print(fig2)

# Losses
urea <- as.numeric(as.character(nh3flux$`NH3 Flux g/m2 day-1 - urea`))
nbpt <- as.numeric(as.character(nh3flux$`NH3 Flux g/m2 day-1 - nbpt`))
x3 <- nh3flux$`Days post fertilization`

fig3 <- ggplot(nh3flux) +
  geom_point(mapping = aes(x = x3, y = urea)) +
  geom_line(mapping = aes(x = x3, y = urea)) +
  geom_point(mapping = aes(x = x3, y = nbpt)) +
  geom_line(mapping = aes(x = x3, y = nbpt), linetype = 'dashed') +
  scale_x_continuous(breaks=seq(0, 45, 10), limits = c(0, 45))

print(fig3)

# stack graphs
grid.arrange(fig3, fig2, fig1, ncol = 1)