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

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

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

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

字符串

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

qlvxas9a

qlvxas9a1#

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

  1. ggplot() +
  2. geom_smooth(data=df_Sweden, aes(x=YEAR, y=df_Sweden$CENTAXINCOMESH,
  3. color="Income Taxes"), group=1) +
  4. geom_smooth(data=df_Sweden, aes(x=YEAR, y=df_Sweden$CENTAXPROPERTYSH,
  5. color="Property Taxes"), group=1) +
  6. geom_smooth(data=df_Sweden, aes(x=YEAR, y=df_Sweden$CENTAXCUSTOMSSH,
  7. color="Customs and International Trade"), group=1) +
  8. geom_smooth(data=df_Sweden, aes(x=YEAR, y=df_Sweden$CENTAXEXCISESH,
  9. color="Excise Taxes"), group=1) +
  10. geom_smooth(data=df_Sweden, aes(x=YEAR, y=df_Sweden$CENTAXCONSSH,
  11. color="Consumption Taxes"), group=1) +
  12. scale_y_continuous(limits=c(0,100),
  13. labels=scales::label_percent(scale=1),
  14. expand = expansion(0,0)) +
  15. scale_x_continuous(n.breaks = 30,
  16. limits = c(1890,2012), expand = c(0, 0)) + #yes shorted timeframe
  17. scale_color_manual(values=c("Income Taxes" = "#991f00", "Property Taxes" =
  18. "#ff3300", "Customs and International Trade" = "#00ffff", "Excise Taxes" =
  19. "#0099ff", "Consumption Taxes" = "#0000cc")) +
  20. theme(axis.text.x = element_text(angle=45, vjust=1, hjust=1)) +
  21. xlab("Year") +
  22. ylab("% of Government Revenue") +
  23. labs(color="Tax Type") +
  24. theme(plot.title=element_text(hjust=0.5))

字符串

展开查看全部

相关问题