如何将颠倒的条形图添加到我的 ggplot 时间序列图中?

问题描述 投票:0回答:1

我试图通过为“TP”(=总降水量)变量添加一个倒置的条形图来使this plot对读者更加友好。不幸的是,我无法从this线程中获得对我来说看起来有任何好处的建议。我希望最终结果看起来有点like this,但用的是线而不是点。这个想法是让它看起来不那么混乱,但仍然可视化变量之间可能的联系(通常的时间跨度将比下面给出的片段更长)。这是原始数据的str:

> str(DFSub0716)
'data.frame':   1633 obs. of  4 variables:
 $ datetime: POSIXct, format: "2016-07-06 12:00:00" "2016-07-06 13:00:00" "2016-07-06 14:00:00" "2016-07-06 15:00:00" ...
 $ Q_m3.s  : num  0.396 0.396 0.396 0.396 0.396 0.396 0.396 0.396 0.396 0.396 ...
 $ No3_mg.l: num  1.72 1.5 1.49 1.49 1.48 ...
 $ TP      : num  0.09661 0.06568 0.1311 0.0337 0.00211 ...

编辑:这里有一些数据可以重现下面代码的输出

                datetime Q_m3.s No3_mg.l         TP
6703 2016-07-06 06:00:00 0.4430 1.566312 0.01118792
6704 2016-07-06 07:00:00 0.4195 1.609813 0.02158611
6705 2016-07-06 08:00:00 0.3960 1.628437 0.05844045
6706 2016-07-06 09:00:00 0.3960 1.683905 0.04606792
6707 2016-07-06 10:00:00 0.3960 1.617095 0.07120785
6708 2016-07-06 11:00:00 0.3960 1.634220 0.08015819
6709 2016-07-06 12:00:00 0.3960 1.724347 0.09661102
6710 2016-07-06 13:00:00 0.3960 1.495940 0.06567970
6711 2016-07-06 14:00:00 0.3960 1.488123 0.13109615
6712 2016-07-06 15:00:00 0.3960 1.494128 0.03369540
6713 2016-07-06 16:00:00 0.3960 1.482408 0.00210596
6714 2016-07-06 17:00:00 0.3960 1.509152 0.00052649
6715 2016-07-06 18:00:00 0.3960 1.483495 0.00000000

这是我到目前为止所拥有的:

#### 2016/07//06
DFSub060716 <- DFSub[DFSub$datetime >= "2016-07-06 06:00:00" & DFSub$datetime <= "2016-07-06 18:00:00", ]

# to long format
library(reshape2)
DFSubL2 <- melt(DFSub060716, id="datetime")
# x-axis breaks
library(lubridate)
desired_breaks2 <- seq.POSIXt(from = lubridate::ymd_hms("20160706 060000"), 
                              to   = lubridate::ymd_hms("20160706 180000"), by = "2 hours")
# scale and plot data
library(ggplot2)
library(dplyr)
NQ060716 <- DFSubL2 %>%
  group_by(variable) %>%
  mutate(
    value.scaled = (value - mean(value)) / sd(value),
    idx = 1:n()) %>%
  ggplot(aes(x = datetime, y = value.scaled, colour = variable, linetype = variable)) +
  theme_bw() +
  theme(
    axis.title.x = element_blank(),
    panel.grid.major.x = element_blank())+
  scale_x_datetime(date_labels = "%m.%d\n%H:%M", breaks = desired_breaks2)+
  labs(title = "HND low-flow period 2016-07-06")+
  geom_line(lwd = 0.9)+
  scale_linetype_manual(values=c("solid", "solid", "solid")) +
  scale_color_manual(values = c("Q_m3.s" = "#000000",
                                "No3_mg.l" = "#669933",
                                "TP" = "#3399FF"))
NQ060716

我希望我没有忘记任何必要的信息,但如果是这样,请告诉我。我很感激任何帮助(这里完全是初学者)。谢谢!

r ggplot2 time-series styling
1个回答
0
投票

我相信您正在寻找的是: “

scale_y_reverse() +

这样您就不需要切换到 geom_tile,您的示例将如下所示:

ggplot(aes(x = datetime, y = value.scaled, colour = variable, linetype = variable)) + 
    theme_bw() +
    theme(
        axis.title.x = element_blank(),
        panel.grid.major.x = element_blank())+
    scale_x_datetime(date_labels = "%m.%d\n%H:%M", breaks = desired_breaks2)+
    scale_y_reverse() +
    labs(title = "HND low-flow period 2016-07-06")+
    geom_line(lwd = 0.9)+
    scale_linetype_manual(values=c("solid", "solid", "solid")) +
    scale_color_manual(values = c("Q_m3.s" = "#000000",
                                "No3_mg.l" = "#669933",
                                "TP" = "#3399FF"))
© www.soinside.com 2019 - 2024. All rights reserved.