gpt4 book ai didi

r - 将 geom_area 的区域调整为 geom_line 的一条线

转载 作者:行者123 更新时间:2023-12-01 00:12:00 25 4
gpt4 key购买 nike

我正在尝试使用发电量和能源消耗数据制作每小时调度曲线,其特征是当我们进行功率平衡(发电量减去消耗量)时,我们得到的值几乎为零。
在发电数据中还有净交换值,当电力系统输出能量时为负值,当系统输入能量以完成消耗时为正值。
enter image description here
因此,对于使用 geom_area 创建的图和 geom_line好的,黑线(消耗)需要根据生成区域进行调整,使区域和黑线之间没有间隙。但是,在我的尝试中,我做不到。你怎么看,同样的能量平衡导致零,在 19 和 20 小时之间存在差距。我不知道出了什么问题。有人知道怎么做吗?
提前致谢。

绘图数据:

generation <-
data.frame('dayHour' = c('18/11/2018 18:00','18/11/2018 19:00','18/11/2018 20:00','18/11/2018 21:00','18/11/2018 18:00','18/11/2018 19:00','18/11/2018 20:00','18/11/2018 21:00','18/11/2018 18:00','18/11/2018 19:00','18/11/2018 20:00','18/11/2018 21:00','18/11/2018 18:00','18/11/2018 19:00','18/11/2018 20:00','18/11/2018 21:00'),
'power' = c(-1364.290, -433.110, 1132.39, 749.48, 463.75, 467.8, 469.35, 436.51, 2025.5, 2133.07, 2306.85, 2304.91, 211.52, 213.16, 214.33, 214.59),
'label' = c('net interchange', 'net interchange', 'net interchange', 'net interchange', 'gas', 'gas', 'gas', 'gas', 'hydro', 'hydro', 'hydro', 'hydro', 'biomass', 'biomass', 'biomass', 'biomass'))


generation$label <- factor(generation$label, levels = c('net interchange', 'gas', 'hydro', 'biomass'))

net.load <-
data.frame('dayHour' = c('18/11/2018 18:00', '18/11/2018 19:00', '18/11/2018 20:00', '18/11/2018 21:00'), 'power' = c(1336.48, 2380.91, 4122.91, 3705.49), 'label' = c('net load', 'net load', 'net load', 'net load'))


generation$dayHour <-
as.POSIXct(strptime(generation$dayHour,format='%d/%m/%Y %H:%M'))

net.load$dayHour <-
as.POSIXct(strptime(net.load$dayHour,format='%d/%m/%Y %H:%M'))

功率平衡

pb <- 
filter(generation, label == "biomass")$power +
filter(generation, label == "hydro")$power +
filter(generation, label == "gas")$power +
filter(generation, label == "net interchange")$power -
net.load$power

summary(pb)

调度曲线

ggplot() + 

geom_area(data = generation,
aes(y = power,
x = dayHour,
fill = label)) +

geom_line(data = net.load,
aes(y = power,
x = dayHour,
colour = label),
size = 1.2,
colour = "black") +

labs(fill = "generation",
colour = 'net load')

最佳答案

它看起来像 position_stack当插值穿过 x 轴时会感到困惑。

要修复它,您可以在绘图之前手动插入(例如使用 approx ):

library(tidyverse)

generation <- data.frame(
dayHour = structure(c(1542585600, 1542589200, 1542592800, 1542596400, 1542585600, 1542589200, 1542592800, 1542596400, 1542585600, 1542589200, 1542592800, 1542596400, 1542585600, 1542589200, 1542592800, 1542596400), class = c("POSIXct", "POSIXt"), tzone = ""),
power = c(-1364.29, -433.11, 1132.39, 749.48, 463.75, 467.8, 469.35, 436.51, 2025.5, 2133.07, 2306.85, 2304.91, 211.52, 213.16, 214.33, 214.59),
label = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("net interchange", "gas", "hydro", "biomass"), class = "factor")
)

generation_interpolated <- generation %>%
group_by(label) %>%
summarise(data = list(as_tibble(approx(dayHour, power, n = 501)))) %>%
unnest() %>%
mutate(x = as.POSIXct(x, origin = '1970-01-01', tz = 'UTC'))

net_power_interpolated <- generation_interpolated %>%
group_by(x) %>%
summarise(y = sum(y))

ggplot(generation_interpolated, aes(x, y)) +
geom_area(aes(fill = label)) +
geom_line(data = net_power_interpolated)

line that fits areas

看如何 approx作品,一个更简单的,未分组的例子:

df <- data.frame(x = c(0, 5, 10), y = c(0, 20, 10))
interpolated <- approx(df$x, df$y, n = 11)
str(interpolated)
#> List of 2
#> $ x: int [1:11] 0 1 2 3 4 5 6 7 8 9 ...
#> $ y: num [1:11] 0 4 8 12 16 20 18 16 14 12 ...

ggplot(as.data.frame(interpolated), aes(x, y)) +
geom_line() +
geom_point() +
geom_point(data = df, color = 'dodgerblue', size = 4)

关于r - 将 geom_area 的区域调整为 geom_line 的一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57368067/

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