提问人:PhobixPhobix 提问时间:11/9/2023 最后编辑:PhilPhobixPhobix 更新时间:11/9/2023 访问量:27
在多线图中插值 ggplot2
Interpolating missing values in multi-line graph ggplot2
问:
尽管不完全了解其含义,但我还是使用了插值这个词。我有一个多线图,如果数据集中缺少值,图上的线就会断裂(参见 20 世纪初的法国,有一个缺口)。我想知道如何“填充”该行,以便不会显示数据中的中断。我试图“平滑”,但无法下载 ggalt 包。现在我想我需要“插值”。有人能帮忙吗?非常感谢:')
ggplot() +
geom_line(data=df_France, aes(x=YEAR, y=CENTAXGDP, color="France"), group=1) + #note: putting color=within the bracket makes it group by variable, not change it's color
geom_line(data=df_Australia, aes(x=YEAR, y=CENTAXGDP, color="Australia"), group=1) +
geom_line(data=df_Norway, aes(x=YEAR, y=CENTAXGDP, color="Norway"), group=1) +
geom_line(data=df_Sweden, aes(x=YEAR, y=CENTAXGDP, color="Sweden"), group=1) +
geom_line(data=df_USA, aes(x=YEAR, y=CENTAXGDP, color="USA"), group=1) +
geom_line(data=df_Denmark, aes(x=YEAR, y=CENTAXGDP, color="Denmark", group=1)) +
geom_smooth() +
scale_y_continuous(limits=c(0,40),
labels=scales::label_percent(scale=1),
expand = expansion(0,0)) +
scale_x_continuous(n.breaks=35) +
scale_color_manual(values=c("France" = "red", "Australia" = "blue", "Norway" = "green", "Sweden" = "purple", "USA" = "black", "Denmark" = "pink")) +
theme(axis.text.x = element_text(angle=45, vjust=1, hjust=1)) +
xlab("Year") +
ylab("% of GDP") +
labs(color="Country") +
ggtitle("Total central government tax revenue as a share of GDP: 1800-2012") +
theme(plot.title=element_text(hjust=0.5))
有人在 StackOverflow 上遇到了类似的问题,对我帮助很大,所以我试图复制他们所做的:
df %>%
mutate(is_real = !is.na(value)) %>%
group_by(color) %>%
mutate(value = pracma::interp1(df$YEAR, value, df$YEAR, "linear"))
出现错误消息:
Error: Problem with `mutate()` column `is_real`.
ℹ `is_real = !is.na(value)`.
x object 'value' not found
答: 暂无答案
评论
mutate(CENTAXGDP = pracma::interp1(YEAR, CENTAXGDP, YEAR, "linear"))