如何使用ggplot绘制R中的降雨径流图?

Yu *_*Guo 3 r ggplot2

对于水文学家来说,通常使用降雨水文仪和水流水文仪。如下图所示。 水文仪和水文仪

X轴表示日期,左侧的Y轴表示降雨,右侧的Y轴表示流量。

我有一个降雨表和一个排水表。

  ####Rain Table####                   ####Discharge Table####
   Date         Value                     Date         Value
2000-01-01       13.2                   2000-01-01      150
2000-01-02       9.5                    2000-01-01      135
2000-01-03       7.3                    2000-01-01      58
2000-01-04       0.2                    2000-01-01      38
Run Code Online (Sandbox Code Playgroud)

这是我的代码。

  ggplot(rain,aes(x=DATE,y=value)) + 
    geom_bar(stat = 'identity')+
    scale_y_reverse()+
    geom_line(data =discharge,aes(x=DATE,y=value))
Run Code Online (Sandbox Code Playgroud)

但是我不知道如何在两个不同的Y轴上表示这些值。

nei*_*fws 5

我认为这些评论为不使用ggplot2这个问题提供了一个强有力的理由:它不会优雅或直接。所以这是一个使用highcharter包的答案。

我使用提供的数据作为示例,只是将排放日期更改为与下雨日期相同。

这是以HTML 格式发布的交互式结果

这是一个屏幕截图。 在此处输入图片说明 我会回应上面的评论:虽然这可能是水文学的标准,但反向双轴非常具有误导性。我认为您可以通过ggplot2一些实验获得更多信息和吸引力。

library(highcharter)
library(dplyr)

rainfall <- data.frame(date = as.Date(rep(c("2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04"), 2), "%Y-%m-%d"), 
                       value = c(13.2, 9.5, 7.3, 0.2, 150, 135, 58, 38), 
                       variable = c(rep("rain", 4), rep("discharge", 4)))

hc <- highchart() %>% 
  hc_yAxis_multiples(list(title = list(text = "rainfall depth (mm)"), reversed = TRUE), 
                     list(title = list(text = "flow (m3/s)"), 
                      opposite = TRUE)) %>% 
  hc_add_series(data = filter(rainfall, variable == "rain") %>% mutate(value = -value) %>% .$value, type = "column") %>% 
  hc_add_series(data = filter(rainfall, variable == "discharge") %>% .$value, type = "spline", yAxis = 1) %>% 
  hc_xAxis(categories = dataset$rain.date, title = list(text = "date"))

hc
Run Code Online (Sandbox Code Playgroud)


jps*_*nno 5

在ggplot中,可以使用sec_axis()函数显示第二个轴,该轴是第一个轴的变换。由于可以使用分水岭区域将降水量转换为一定体积(或将排放量转换为深度),因此可以使用sec_axis制作hytograph。

library(ggplot2)
library(readr)

df <- read_csv("date, precip_in, discharge_cfs
                2000-01-01, 13.2, 150
                2000-01-02, 9.5, 135
                2000-01-03, 7.3, 58
                2000-01-04, 0.2, 38")

watershedArea_sqft <- 100

# Convert the precipitation to the same units as discharge. These steps will based on your units
df$precip_ft <- df$precip_in/12
df$precip_cuft <- df$precip_ft * watershedArea_sqft

# Calculate the range needed to avoid having your hyetograph and hydrograph overlap 
maxRange <- 1.1*(max(df$precip_cuft) + max(df$discharge_cfs))

# Create a function to backtransform the axis labels for precipitation
precip_labels <- function(x) {(x / watershedArea_sqft) * 12}

# Plot the data
ggplot(data = df,
       aes(x = date)) +
  # Use geom_tile to create the inverted hyetograph. geom_tile has a bug that displays a warning message for height and width, you can ignore it.
  geom_tile(aes(y = -1*(precip_cuft/2-maxRange), # y = the center point of each bar
                height = precip_cuft,
                width = 1),
            fill = "gray50",
            color = "white") +
  # Plot your discharge data
  geom_line(aes(y = discharge_cfs),
            color = "blue") +
  # Create a second axis with sec_axis() and format the labels to display the original precipitation units.
  scale_y_continuous(name = "Discharge (cfs)",
                     sec.axis = sec_axis(trans = ~-1*(.-maxRange),
                                         name = "Precipitation (in)",
                                         labels = precip_labels))
Run Code Online (Sandbox Code Playgroud)

水文水位计示例