R语言 平滑多线图上数据缺失的线中的断点(ggplot2)

yk9xbfzb  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(125)

我在追踪一段时间内的税收趋势,尽管在原始数据集中有缺失数据点的地方,线条看起来是断开的。我正在寻找一种方法来平滑线条,以便线条看起来不间断,即使在有缺失数据的地方。
有人能帮忙吗?谢谢你:}
我的代码是:

ggplot() +
  geom_line(data=df_Sweden, aes(x=YEAR, y=df_Sweden$CENTAXINCOMESH, color="Income Taxes"), group=1) +
  geom_line(data=df_Sweden, aes(x=YEAR, y=df_Sweden$CENTAXPROPERTYSH, color="Property Taxes"), group=1) +
  geom_line(data=df_Sweden, aes(x=YEAR, y=df_Sweden$CENTAXCUSTOMSSH, color="Customs and International Trade"), group=1) +
  geom_line(data=df_Sweden, aes(x=YEAR, y=df_Sweden$CENTAXEXCISESH, color="Excise Taxes"), group=1) +
  geom_line(data=df_Sweden, aes(x=YEAR, y=df_Sweden$CENTAXCONSSH, color="Consumption Taxes"), group=1) +
  scale_y_continuous(limits=c(0,100),
                     labels=scales::label_percent(scale=1),
                     expand = expansion(0,0)) +
   scale_x_continuous(n.breaks = 30,
     limits = c(1890,2012), expand = c(0, 0)) + #yes shorted timeframe
  scale_color_manual(values=c("Income Taxes" = "#991f00", "Property Taxes" = "#ff3300", "Customs and International Trade" = "#00ffff", "Excise Taxes" = "#0099ff", "Consumption Taxes" = "#0000cc")) +
  theme(axis.text.x = element_text(angle=45, vjust=1, hjust=1)) +
  xlab("Year") +
  ylab("% of Government Revenue") +
  labs(color="Tax Type") +
  theme(plot.title=element_text(hjust=0.5))

字符串

我粗略地尝试了插值(虽然我认为我对它的理解还不够好,不能让它工作)。我遇到了一个叫做样条(?)的东西,但无法下载所需的软件包

qlvxas9a

qlvxas9a1#

在ggplot2中,函数geom_line只能按照x轴上变量的顺序连接观测值。为了可视化趋势,而不将线条限制在观测值上(用你的话说,“平滑线条”),geom_smooth是一个更合适的函数。
所以正确的代码应该是:

ggplot() +
geom_smooth(data=df_Sweden, aes(x=YEAR, y=df_Sweden$CENTAXINCOMESH, 
color="Income Taxes"), group=1) +
geom_smooth(data=df_Sweden, aes(x=YEAR, y=df_Sweden$CENTAXPROPERTYSH, 
color="Property Taxes"), group=1) +
geom_smooth(data=df_Sweden, aes(x=YEAR, y=df_Sweden$CENTAXCUSTOMSSH, 
color="Customs and International Trade"), group=1) +
geom_smooth(data=df_Sweden, aes(x=YEAR, y=df_Sweden$CENTAXEXCISESH, 
color="Excise Taxes"), group=1) +
geom_smooth(data=df_Sweden, aes(x=YEAR, y=df_Sweden$CENTAXCONSSH, 
color="Consumption Taxes"), group=1) +
scale_y_continuous(limits=c(0,100),
                 labels=scales::label_percent(scale=1),
                 expand = expansion(0,0)) +
scale_x_continuous(n.breaks = 30,
 limits = c(1890,2012), expand = c(0, 0)) + #yes shorted timeframe
scale_color_manual(values=c("Income Taxes" = "#991f00", "Property Taxes" = 
"#ff3300", "Customs and International Trade" = "#00ffff", "Excise Taxes" = 
"#0099ff", "Consumption Taxes" = "#0000cc")) +
theme(axis.text.x = element_text(angle=45, vjust=1, hjust=1)) +
xlab("Year") +
ylab("% of Government Revenue") +
labs(color="Tax Type") +
theme(plot.title=element_text(hjust=0.5))

字符串

相关问题