【问题标题】:How can I plot a line for one country throughout time via ggplot?如何通过 ggplot 在整个时间段内为一个国家绘制一条线?
【发布时间】:2022-10-04 22:54:08
【问题描述】:

我的代码是

ggplot(classen, aes(x=Year, y=, colour= class)) + geom_line()

我的df是:

Country Name Year SupDem
Turkiye 1990 5
Turkiye 1991 45
Turkiye 1993 50
US 1990 80
US 1991 85
US 1993 84

我需要选择国家的名称,然后想绘制它,但我不确定是否可以在 ggplot() 函数中选择国家。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    是的,color 应该是 Country Name 而不是 classy 应该是 SupDem

    library(ggplot2)
    
    ggplot(df, aes(Year, SupDem, colour = `Country Name`)) +
      geom_line()
    

    或者,如果您想要更现代的样式,例如:

    library(ggplot2)
    
    ggplot(df, aes(Year, SupDem, colour = `Country Name`)) +
      geom_line(size = 1) +
      geom_point(size = 3) +
      scale_color_manual(values = c('orange', 'deepskyblue4')) +
      theme_minimal(base_size = 16) 
    

    创建于 2022 年 10 月 4 日,reprex v2.0.2


    以可重复格式从问题中获取的数据

    df <- structure(list(`Country Name` = c("Turkiye", "Turkiye", "Turkiye", 
     "US", "US", "US"), Year = c(1990L, 1991L, 1993L, 1990L, 1991L, 
    1993L), SupDem = c(5L, 45L, 50L, 80L, 85L, 84L)), class = "data.frame", 
    row.names = c(NA, -6L))
    

    【讨论】:

      猜你喜欢
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      相关资源
      最近更新 更多