问题描述
我正在尝试绘制不同的图形,将 HPI 与中值指数进行比较。首先,我在这个例子中有相同的数据,但没有最后三列,我用下面的代码重新调整它,让一列包含中位数,一列包含三个 HPI
df %>%
pivot_wider(names_from = Index,values_from = Value) %>%
pivot_longer(starts_with("HPI"),names_to = "Index",values_to = "Value")
现在,我需要其他三列在图形中添加置信区间,但我似乎无法使它起作用。
这是我的数据示例
df <- tibble::tribble(
~Index,~Value,~Operation,~Month,~SD,~lower,~upper,"HPI1",0.9,"Sale","01/2020",0.00224245,0.9788445,0.9876349,1.1,"Rent","02/2020",0.00227729,1.0323418,1.0412688,"HPI2",0.89,0.00645641,1.1494260,1.1747352,1.12,"HPI3",0.85,0.00732540,1.1376065,1.1663221,1.22,"Median",0.91,1.02,"03/2020","04/2020",1.1663221)
非常感谢
解决方法
这可能是一种无需使用 pivot_wider
和 pivot_longer
的可行方法:
library(dplyr)
library(lubridate)
library(ggplot2)
# month as date format
df <-
df %>%
mutate(Month = my(Month))
# All cases of HPI index by Month and Operation to join with Median only data to create a separate data frame for the facetted Median line
df_hpi <-
df %>%
filter(Index != "Median") %>%
select(Index,Month,Operation)
# Median data frame for all facetting cases: Index and operation by month and value
df_med <-
df %>%
filter(Index == "Median") %>%
rename(dataset = Index)%>%
right_join(df_hpi,by = c("Month" = "Month","Operation" = "Operation"))
# HPI data only
df1 <-
df %>%
filter(Index != "Median")
# All together in the call to ggplot
ggplot(df1,aes(Month,Value,group = Index))+
geom_line()+
geom_line(data = df_med,aes(colour = "Median"))+
facet_grid(Operation~Index)+
labs(colour = NULL)+
theme(legend.position = "bottom")
由 reprex package (v2.0.0) 于 2021 年 4 月 13 日创建
,我知道您正在尝试做这样的事情:一条线和一条丝带(置信区间)。 但是您的“较低”列的值似乎不正确。
相反,我使用了“SD”列。
library(tidyverse)
library(lubridate)
library(ggplot2)
df <- tibble::tribble(
~Index,~Value,~Operation,~Month,~SD,~lower,~upper,"HPI1",0.9,"Sale","01/2020",0.00224245,0.9788445,0.9876349,1.1,"Rent","02/2020",0.00227729,1.0323418,1.0412688,"HPI2",0.89,0.00645641,1.1494260,1.1747352,1.12,"HPI3",0.85,0.00732540,1.1376065,1.1663221,1.22,"Median",0.91,1.02,"03/2020","04/2020",1.1663221)
df %>%
mutate(Month = my(Month),ymin = Value - 2*SD,ymax = Value + 2*SD) %>%
ggplot() +
geom_line(aes(
x = Month,y = Value,group = Operation,color = Operation
)) +
geom_ribbon(aes(
x = Month,ymin = ymin,ymax = ymax,fill = Operation
),alpha = 0.3) +
facet_wrap(~ Index) +
theme_light() +
labs(x = NULL,y = NULL)