Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.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 - Fatal编程技术网

R 降雨径流绘图

R 降雨径流绘图,r,ggplot2,R,Ggplot2,我的数据框架如下: Date,Precipitation,Observed,Simulated 1/1/1988,21.90,3.06,15.27 1/2/1988,12.34,6.70,17.46 1/3/1988,19.76,18.32,32.45 1/4/1988,5.54,98.20,46.67 1/5/1988,6.50,91.92,37.43 1/6/1988,11.04,38.12,20.94 1/7/1988,17.90,44.14,26.64 1/8/1988,23.76,41

我的数据框架如下:

Date,Precipitation,Observed,Simulated
1/1/1988,21.90,3.06,15.27
1/2/1988,12.34,6.70,17.46
1/3/1988,19.76,18.32,32.45
1/4/1988,5.54,98.20,46.67
1/5/1988,6.50,91.92,37.43
1/6/1988,11.04,38.12,20.94
1/7/1988,17.90,44.14,26.64
1/8/1988,23.76,41.07,118.2
1/9/1988,59.74,169.06,225.4
1/10/1988,51.58,371.91,325.3
1/11/1988,18.08,447.05,387.1
1/12/1988,2.92,295.40,329.6
1/13/1988,2.72,133.90,218.7
1/14/1988,0.00,95.82,128.6
1/15/1988,7.38,70.32,70.17
1/16/1988,3.50,36.21,38.42
1/17/1988,4.58,9.43,21.97
1/18/1988,7.84,35.74,12.7
1/19/1988,16.86,24.32,12.96
1/20/1988,5.00,33.90,18.56
1/21/1988,0.96,27.06,20.9
1/22/1988,15.54,23.20,42.36
1/23/1988,11.36,16.41,54.19
1/24/1988,1.06,23.94,48.3
1/25/1988,6.42,17.35,42.32
1/26/1988,0.00,14.91,33.91
1/27/1988,2.44,10.13,25.46
1/28/1988,2.00,14.33,16.39
1/29/1988,2.36,10.62,6.423
1/30/1988,4.20,18.65,6.175
1/31/1988,15.80,12.67,38.42
2/1/1988,0.46,15.50,52.92
2/2/1988,2.50,14.91,35.03
2/3/1988,1.24,11.36,22.25
2/4/1988,0.04,16.72,15.55
2/5/1988,3.02,22.83,11.84
2/6/1988,0.00,10.86,10.33
2/7/1988,2.72,5.43,9.352
2/8/1988,0.36,3.32,9.41
2/9/1988,4.92,2.25,8.246
2/10/1988,1.94,21.38,8.059
2/11/1988,1.68,66.37,8.936
2/12/1988,0.00,66.37,8.247
2/13/1988,14.04,14.91,8.071
2/14/1988,2.08,9.90,9.941
2/15/1988,5.64,7.29,10.41
2/16/1988,0.00,21.38,10.08
2/17/1988,0.00,15.21,9.821
2/18/1988,1.44,13.49,10.43
2/19/1988,5.66,8.32,10.66
2/20/1988,0.32,4.77,8.877
2/21/1988,0.96,3.32,7.622
2/22/1988,1.36,2.46,6.069
2/23/1988,0.00,1.94,6.852
2/24/1988,0.36,1.48,7.538
2/25/1988,1.96,1.23,7.785
2/26/1988,4.08,1.08,9.163
2/27/1988,0.00,0.94,10.55
2/28/1988,0.56,0.81,10.5
2/29/1988,0.00,0.81,10.34
3/1/1988,0.00,0.69,10.77
3/2/1988,0.08,0.58,11.25
3/3/1988,0.82,0.53,10.86
3/4/1988,7.48,0.49,7.274
3/5/1988,6.26,0.40,5.618
3/6/1988,17.26,0.49,6.707
3/7/1988,3.42,0.35,8.519
3/8/1988,0.64,0.28,9.339
3/9/1988,3.64,0.32,7.926
3/10/1988,2.00,0.64,6.804
3/11/1988,19.60,0.75,4.982
3/12/1988,0.72,0.69,1.149
3/13/1988,12.52,0.64,1.99
3/14/1988,12.58,0.69,4.057
3/15/1988,22.34,0.81,13.46
3/16/1988,23.44,18.65,20.58
3/17/1988,35.20,65.08,41.81
3/18/1988,10.24,78.59,54.7
3/19/1988,47.32,146.45,88.96
3/20/1988,10.50,172.28,111.9
3/21/1988,0.12,215.85,88.96
3/22/1988,0.00,86.61,62.25
3/23/1988,5.88,49.53,42.3
3/24/1988,4.34,32.56,27.56
3/25/1988,0.50,121.94,18.82
3/26/1988,2.00,88.11,12.78
3/27/1988,14.30,35.28,7.07
3/28/1988,4.76,55.83,6.714
3/29/1988,2.60,17.67,6.184
3/30/1988,2.80,52.92,6.394
3/31/1988,2.80,63.80,7.282
4/1/1988,4.12,34.36,6.681
4/2/1988,23.88,14.62,18.06
4/3/1988,40.14,21.74,63.34
4/4/1988,20.74,31.24,128
4/5/1988,14.72,196.87,148.2
4/6/1988,3.08,121.94,115.5
4/7/1988,8.58,52.92,76.39
4/8/1988,4.32,99.81,48.48
4/9/1988,14.54,31.68,29.99
4/10/1988,3.62,53.49,20.66
4/11/1988,2.20,112.29,17.43
4/12/1988,2.06,58.83,14.83
4/13/1988,1.84,33.00,14.34
4/14/1988,0.00,50.08,13.21
4/15/1988,4.32,25.87,11.88
4/16/1988,6.84,21.03,11.32
4/17/1988,1.74,130.16,10.49
4/18/1988,19.20,55.83,10.87
4/19/1988,8.32,42.08,13.14
4/20/1988,4.30,21.38,14.37
4/21/1988,7.96,116.62,14.34
4/22/1988,11.52,60.67,14.35
4/23/1988,33.82,59.44,107.3
4/24/1988,5.66,198.03,152.3
4/25/1988,18.32,247.39,131.8
4/26/1988,17.80,130.16,121
4/27/1988,2.68,219.52,85.55
4/28/1988,0.00,148.43,52.51
4/29/1988,5.42,98.20,33.55
4/30/1988,1.12,54.65,24.19
5/1/1988,2.90,30.81,19.78
5/2/1988,0.00,16.72,17.36
5/3/1988,5.28,10.62,15.42
5/4/1988,0.00,7.69,10.36
5/5/1988,0.00,9.43,8.719
5/6/1988,0.00,5.60,8.94
5/7/1988,0.16,4.46,7.924
5/8/1988,0.00,3.72,6.791
5/9/1988,0.32,3.19,4.776
5/10/1988,3.24,3.32,6.717
5/11/1988,0.08,6.89,8.701
5/12/1988,0.00,4.61,9.747
5/13/1988,0.00,4.01,8.976
5/14/1988,0.00,3.19,8.588
5/15/1988,0.00,2.04,10.46
5/16/1988,0.00,1.75,12.28
5/17/1988,0.00,1.84,12.8
5/18/1988,3.26,1.56,12.48
5/19/1988,0.00,1.39,11.55
5/20/1988,2.84,1.39,11.39
5/21/1988,14.68,1.48,11.79
5/22/1988,8.00,1.39,12.92
5/23/1988,0.24,1.31,13.17
5/24/1988,0.00,1.08,12.73
5/25/1988,0.12,1.23,12.26
5/26/1988,3.12,1.01,12.14
5/27/1988,0.00,0.94,12.18
5/28/1988,0.00,1.23,12.56
5/29/1988,0.48,1.16,12.86
5/30/1988,0.00,0.94,11.86
5/31/1988,0.00,0.94,11.16
6/1/1988,2.72,0.94,11.3
6/2/1988,0.00,0.88,12.22
6/3/1988,0.92,0.81,13.16
6/4/1988,0.00,0.81,13.31
6/5/1988,0.00,0.69,13.42
6/6/1988,0.00,0.69,13.69
6/7/1988,0.00,0.64,13.95
6/8/1988,0.00,0.64,14.15
6/9/1988,0.00,0.58,14.18
6/10/1988,0.00,0.58,13.17
6/11/1988,0.00,0.58,12.07
6/12/1988,0.00,0.53,11.68
6/13/1988,0.00,0.49,10.58
6/14/1988,0.00,0.40,9.865
6/15/1988,0.00,0.40,9.251
6/16/1988,0.00,0.32,9.095
6/17/1988,0.00,0.32,9.796
6/18/1988,0.00,0.28,11.09
6/19/1988,0.00,0.25,12.24
6/20/1988,0.00,0.25,12.51
6/21/1988,0.00,0.25,12.83
6/22/1988,0.00,0.25,12.24
6/23/1988,0.00,0.25,11.73
6/24/1988,0.00,0.18,11.34
6/25/1988,0.00,0.18,11.88
6/26/1988,0.00,0.18,12.57
6/27/1988,0.00,0.16,13.2
6/28/1988,5.50,0.16,12.72
6/29/1988,0.00,0.13,11.74
6/30/1988,0.00,0.13,11.08
7/1/1988,0.00,0.32,11.06
7/2/1988,0.00,0.28,11.23
7/3/1988,0.00,0.28,11.97
7/4/1988,0.00,0.25,12.37
7/5/1988,0.00,0.25,13
7/6/1988,0.00,0.21,13.21
7/7/1988,0.00,0.18,13.29
7/8/1988,0.00,0.13,13.41
7/9/1988,0.00,0.13,13.49
7/10/1988,0.00,0.13,13.33
7/11/1988,0.00,0.13,13.13
7/12/1988,0.00,0.13,11.59
7/13/1988,0.00,0.11,10.34
7/14/1988,0.00,0.11,9.258
7/15/1988,0.00,0.09,9.584
7/16/1988,0.00,0.09,10.67
7/17/1988,0.00,0.09,11.51
7/18/1988,0.00,0.09,11.22
7/19/1988,0.00,0.13,10.27
7/20/1988,0.00,0.18,6.438
7/21/1988,0.00,0.28,2.296
7/22/1988,0.00,0.25,3.29
7/23/1988,0.00,0.32,5.341
7/24/1988,0.00,0.40,7.088
7/25/1988,0.00,0.35,7.95
7/26/1988,0.00,0.32,9.277
7/27/1988,0.00,0.32,10.6
7/28/1988,0.00,0.25,10.83
7/29/1988,0.00,0.25,10.31
7/30/1988,0.00,0.25,8.075
7/31/1988,0.00,0.18,7.727
8/1/1988,0.00,0.21,7.823
8/2/1988,0.00,0.21,8.826
8/3/1988,0.00,0.18,9.881
8/4/1988,0.00,0.18,9.957
8/5/1988,0.00,0.18,10.3
8/6/1988,0.00,0.18,9.91
8/7/1988,2.00,0.18,10.55
8/8/1988,2.64,0.18,10.41
8/9/1988,2.60,0.18,10.79
8/10/1988,0.00,0.18,9.982
8/11/1988,0.00,0.58,9.223
8/12/1988,0.00,0.64,7.946
8/13/1988,0.00,0.40,2.514
8/14/1988,0.00,0.32,4.496
8/15/1988,0.00,0.69,6.865
8/16/1988,0.00,0.81,9.129
8/17/1988,0.00,0.58,10.58
8/18/1988,0.00,0.32,11.36
8/19/1988,0.00,0.53,11.24
8/20/1988,0.00,0.32,11.25
8/21/1988,3.00,0.32,10.33
8/22/1988,0.00,0.25,9.991
8/23/1988,0.00,0.25,10.26
8/24/1988,0.00,0.21,9.69
8/25/1988,0.00,0.18,10.23
8/26/1988,1.20,0.18,9.806
8/27/1988,0.40,0.18,10.48
8/28/1988,18.32,0.16,11.04
8/29/1988,0.00,0.13,10.73
8/30/1988,0.00,0.13,10.2
8/31/1988,0.00,0.44,8.738
9/1/1988,0.00,0.58,7.971
9/2/1988,0.00,0.49,6.974
9/3/1988,0.00,0.32,7.245
9/4/1988,0.00,0.25,6.757
9/5/1988,0.00,0.18,5.911
9/6/1988,0.00,0.16,7.438
9/7/1988,0.00,0.13,7.892
9/8/1988,1.74,0.13,8.176
9/9/1988,0.00,0.09,7.239
9/10/1988,0.32,0.09,8.05
9/11/1988,0.00,0.09,8.973
9/12/1988,0.00,0.06,9.029
9/13/1988,0.00,0.04,7.839
9/14/1988,0.00,0.03,8.298
9/15/1988,0.00,0.03,9.274
9/16/1988,0.00,0.03,9.598
9/17/1988,0.20,0.01,9.834
9/18/1988,0.00,0.01,8.965
9/19/1988,0.00,0.01,7.586
9/20/1988,0.00,0.00,6.39
9/21/1988,0.00,0.00,6.086
9/22/1988,0.00,0.01,5.242
9/23/1988,0.00,0.01,5.468
9/24/1988,0.00,0.00,7.112
9/25/1988,0.00,0.00,8.564
9/26/1988,0.00,0.01,8.098
9/27/1988,0.00,0.01,7.438
9/28/1988,0.00,0.01,5.027
9/29/1988,0.00,0.01,3.847
9/30/1988,0.00,0.00,3.736
10/1/1988,19.98,0.00,4.882
10/2/1988,1.44,0.00,5.67
10/3/1988,4.76,0.00,5.89
10/4/1988,0.00,0.00,5.984
10/5/1988,0.00,0.01,4.795
10/6/1988,0.46,0.09,1.18
10/7/1988,0.16,0.16,1.71
10/8/1988,2.92,0.09,2.805
10/9/1988,0.00,0.06,3.646
10/10/1988,0.00,0.13,3.978
10/11/1988,3.02,0.11,3.825
10/12/1988,12.22,0.09,4.28
10/13/1988,5.72,0.13,5.557
10/14/1988,0.00,0.13,5.941
10/15/1988,0.00,0.09,6.157
10/16/1988,0.00,0.09,6.48
10/17/1988,2.12,0.16,7.368
10/18/1988,0.00,0.40,7.508
10/19/1988,0.00,0.28,7.449
10/20/1988,0.00,0.21,6.893
10/21/1988,1.74,0.18,7.525
10/22/1988,0.00,0.09,8.059
10/23/1988,0.00,0.09,6.449
10/24/1988,5.74,0.09,2.135
10/25/1988,0.08,0.06,2.628
10/26/1988,8.02,0.04,3.698
10/27/1988,1.08,0.03,3.943
10/28/1988,0.00,0.02,3.831
10/29/1988,2.66,0.01,4.499
10/30/1988,1.66,0.01,5.826
10/31/1988,2.88,0.09,5.149
11/1/1988,4.52,0.03,4.765
11/2/1988,2.92,0.01,4.237
11/3/1988,1.66,0.00,5.34
11/4/1988,1.40,0.00,5.443
11/5/1988,2.44,0.00,2.925
11/6/1988,2.76,0.00,2.2
11/7/1988,4.86,0.00,0.872
11/8/1988,6.00,0.03,1.637
11/9/1988,10.34,0.06,0
11/10/1988,4.36,0.06,0.4189
11/11/1988,2.00,0.81,0.7399
11/12/1988,0.58,4.46,0.0379
11/13/1988,3.72,3.59,0
11/14/1988,0.00,2.04,0.7838
11/15/1988,0.00,1.39,2.604
11/16/1988,0.00,1.94,3.007
11/17/1988,2.56,1.39,3.594
11/18/1988,6.48,0.69,3.388
11/19/1988,0.48,0.69,3.61
11/20/1988,6.92,0.40,4.088
11/21/1988,9.48,0.58,4.012
11/22/1988,2.50,0.49,2.636
11/23/1988,1.92,1.94,4.444
11/24/1988,3.78,3.45,5.406
11/25/1988,0.04,1.56,6.103
11/26/1988,1.52,1.56,6.55
11/27/1988,2.04,1.39,6.061
11/28/1988,0.00,2.46,6.712
11/29/1988,2.32,1.56,7.358
11/30/1988,0.00,1.08,7.037
12/1/1988,0.00,0.88,7.034
12/2/1988,1.04,0.64,5.991
12/3/1988,9.80,0.49,6.684
12/4/1988,0.00,0.40,7.438
12/5/1988,1.26,0.32,7.088
12/6/1988,1.00,0.28,6.931
12/7/1988,2.70,0.25,5.799
12/8/1988,0.76,0.21,3.556
12/9/1988,3.12,0.09,2.857
12/10/1988,0.04,0.09,2.951
12/11/1988,4.00,0.13,3.475
12/12/1988,0.40,0.35,4.755
12/13/1988,0.00,0.35,6.497
12/14/1988,2.36,0.64,6.792
12/15/1988,0.00,0.69,5.049
12/16/1988,1.88,0.49,3.227
12/17/1988,0.64,0.40,3.096
12/18/1988,1.56,0.28,3.878
12/19/1988,2.32,0.25,5.608
12/20/1988,3.18,0.25,6.537
12/21/1988,3.84,0.32,7.767
12/22/1988,1.86,0.32,8.419
12/23/1988,3.28,0.28,8.728
12/24/1988,14.22,1.48,8.467
12/25/1988,4.64,1.48,5.57
12/26/1988,3.20,1.39,3.291
12/27/1988,0.00,23.57,0.897
12/28/1988,4.92,22.46,1.262
12/29/1988,2.02,5.26,1.976
12/30/1988,0.00,43.62,2.227
12/31/1988,0.00,55.83,3.271
我想用ggplot2生成如下图

我是用excel做的,但我想用ggplot2来绘图,这样我就可以自定义大多数东西了

绘图的整个数据集在以下链接中可用


请提供帮助。

以下链接提供了一些解决方案:

我个人用我的

library(ggplot2)
library(data.table)
library(lubridate)
library(tidyverse)

# store the data in csv
data <- fread('data.csv')

data <- melt(data, measure.vars = c('Observed', 'Simulated'), value.name = 'Streamflow') %>%
  .[,Date := fast_strptime(Date, '%m/%d/%Y') %>% as_date()]


summary(data)

# Calculate the range needed to avoid having your hyetograph and hydrograph overlap 
maxRange <- 600 # set how wide of the first axis (streamflow)
coeff <- 0.4 # set the shrink coeffcient of Precipitation


# Plot the data
ggplot(data = data, aes(x = Date)) +
  
  # Use geom_tile to create the inverted hyetograph
  # y = the center point of each bar
  # maxRange - Precipitation/coeff/2
  geom_tile(aes(y = maxRange - Precipitation/coeff/2, 
                height = Precipitation/coeff, 
                fill = 'PColor')
            )+
  # Plot your discharge data
  geom_line(aes(y = Streamflow,
                color = variable), 
            alpha = 0.8,
            size = 0.7) +
  # Create a second axis with sec_axis() and format the labels to display the original precipitation units.
  scale_y_continuous(name = "Streamflow ()",
                     limit = c(0, maxRange),
                     expand = c(0, 0),
                     sec.axis = sec_axis(trans = ~(.-maxRange)*coeff,
                                         name = "Precipitation (mm/d)"))+
  scale_fill_manual(values = c('PColor' = "#386cb0"),
                    labels = c('PColor' = 'Precipitation'),
                    name = NULL
  )+
  scale_color_manual(values = c('black', '#e41a1c'), 
                     name = NULL)+
  theme_bw()+
  guides(color = guide_legend(nrow = 1)) +
  theme(
    # legend.position = c(0.75, 0.5),
    legend.position = 'top',
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank())

库(ggplot2)
库(数据表)
图书馆(lubridate)
图书馆(tidyverse)
#将数据存储在csv中
数据%as_date()]
摘要(数据)
#计算所需的范围,以避免水文曲线和过程线重叠

maxRange以下链接中有一些解决方案:

我个人用我的

library(ggplot2)
library(data.table)
library(lubridate)
library(tidyverse)

# store the data in csv
data <- fread('data.csv')

data <- melt(data, measure.vars = c('Observed', 'Simulated'), value.name = 'Streamflow') %>%
  .[,Date := fast_strptime(Date, '%m/%d/%Y') %>% as_date()]


summary(data)

# Calculate the range needed to avoid having your hyetograph and hydrograph overlap 
maxRange <- 600 # set how wide of the first axis (streamflow)
coeff <- 0.4 # set the shrink coeffcient of Precipitation


# Plot the data
ggplot(data = data, aes(x = Date)) +
  
  # Use geom_tile to create the inverted hyetograph
  # y = the center point of each bar
  # maxRange - Precipitation/coeff/2
  geom_tile(aes(y = maxRange - Precipitation/coeff/2, 
                height = Precipitation/coeff, 
                fill = 'PColor')
            )+
  # Plot your discharge data
  geom_line(aes(y = Streamflow,
                color = variable), 
            alpha = 0.8,
            size = 0.7) +
  # Create a second axis with sec_axis() and format the labels to display the original precipitation units.
  scale_y_continuous(name = "Streamflow ()",
                     limit = c(0, maxRange),
                     expand = c(0, 0),
                     sec.axis = sec_axis(trans = ~(.-maxRange)*coeff,
                                         name = "Precipitation (mm/d)"))+
  scale_fill_manual(values = c('PColor' = "#386cb0"),
                    labels = c('PColor' = 'Precipitation'),
                    name = NULL
  )+
  scale_color_manual(values = c('black', '#e41a1c'), 
                     name = NULL)+
  theme_bw()+
  guides(color = guide_legend(nrow = 1)) +
  theme(
    # legend.position = c(0.75, 0.5),
    legend.position = 'top',
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank())

库(ggplot2)
库(数据表)
图书馆(lubridate)
图书馆(tidyverse)
#将数据存储在csv中
数据%as_date()]
摘要(数据)
#计算所需的范围,以避免水文曲线和过程线重叠
最大范围