gpt4 book ai didi

r - 在 ggplot2 中绘制污染物超标的持续时间

转载 作者:行者123 更新时间:2023-12-02 09:10:08 25 4
gpt4 key购买 nike

我有一个数据框,其中包含日期列和参数计算浓度列。我正在尝试绘制一个时间序列图,其中包含所有浓度的散点,然后有一条水平线显示污染物的标准(即 500)。我可以做到这一点,没有问题。我遇到的问题是尝试绘制一条线,显示超过 500 的持续时间。我似乎找不到任何东西来解决我的问题。我将不胜感激任何指导。

示例数据:

df<-structure(list(Date_Time = structure(c(1480093200, 1482660000, 
1395651343, 1329823800, 1326929400, 1331233200, 1490130000, 1476138600,
1474070400, 1489393800, 1483272000, 1393515068, 1480471200, 1332680400,
1471226400, 1470853800, 1396124591, 1496250000, 1394581991, 1438177553,
1332108000, 1493051400, 1475949600, 1491024600, 1488832200, 1473697800,
1475404200, 1488511800, 1490212800, 1477040400, 1494793740, 1389346885,
1473933600, 1390611191, 1486551600, 1476475200, 1473593400, 1388854543,
1327012200, 1493611140), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Calculated_TDS = c(271.3692, 634.3604, 634.246, 219.546,
674.286, 169.21, 506.118, 452.6932, 314.8412, 4640.3052,
358.0844, 734.918, 97.71, 460.358, 385.998, 283.9532, 370.554,
309.2356, 296.766, 137.079616, 24.494, 383.996, 321.2476,
784.6248, 642.1396, 1320.7032, 213.254, 462.1884, 547.6452,
376.274, 195.1216, 595.35, 320.1608, 411.166, 882.5512, 288.5292,
533.574, 1000.326, 124.022, 256.6116)), row.names = c(NA,
-40L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("Date_Time",
"Calculated_TDS"))

代码:

library(tidyverse)

test_df<-df%>%
mutate(greater = Calculated_TDS > 500)%>%
group_by(Date_Time,Calculated_TDS)%>%
summarize(n_greater = sum(greater), duration = length(Date_Time))


plot<-ggplot() +
geom_point(data = test_df , aes(x = Date_Time, y = Calculated_TDS))+
geom_line(data= test_df,aes(x=Date_Time, y = duration),stat="identity")+
geom_hline(aes(yintercept = 500,color="red"),size=1.3)

plot

我知道我所拥有的没有意义,但我不明白如何找到超出的持续时间。

enter image description here

最佳答案

我不太确定你想要什么,但这是一个起点。这个想法很简单,为超出和低于的列制作列,用 NA 填充,然后绘图。超出的线指定的颜色为红色,下方的点指定的颜色为蓝色。请注意,color = "red" 应位于水平线的 aes 之外。仅当颜色应随值变化时才在 aes 中使用颜色。


图书馆(tidyverse)

test_df <- df %>% 
mutate(greater = Calculated_TDS > 500,
exceed_value = if_else(greater, Calculated_TDS, as.numeric(NA)),
below_value = if_else(greater, as.numeric(NA), Calculated_TDS))

plot <- ggplot(data = test_df, aes(x = Date_Time)) +
geom_point(aes(y = exceed_value), color = "red") +
geom_point(aes(y = below_value), color = "blue") +
geom_line(aes(y = exceed_value),
color = "red") + geom_hline(aes(yintercept = 500), color = "red", size = 1.3)

print(plot)
#> Warning in as.POSIXlt.POSIXct(x): unknown timezone 'zone/tz/2018g.1.0/
#> zoneinfo/America/New_York'
#> Warning: Removed 26 rows containing missing values (geom_point).
#> Warning: Removed 14 rows containing missing values (geom_point).
#> Warning: Removed 4 rows containing missing values (geom_path).

关于r - 在 ggplot2 中绘制污染物超标的持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53379823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com