【问题标题】:R: plot multiple lines in different colours from subset of databaseR:从数据库子集中绘制多条不同颜色的线
【发布时间】:2016-01-18 00:55:51
【问题描述】:

我创建了一个包含六个不同国家和多个 GDP 和不平等指标的数据库。

首先,我想在一张图中绘制各国的 GDP 增长情况。效果很好:

plot(my_six_countries$Year, my_six_countries$GDP.growth.rate, main = "Development of GDP growth", xlab = "Year", ylab = "GDP growth", type = "l", col = 600)

但是,我希望不同国家/地区的线条以不同的颜色显示,而不仅仅是 600 条。我几乎整天都在解决这个超级笨拙的问题,并且我尝试了各种方法,从创建颜色矢量到手动设置子集以使用 ggplot - 但我真的被卡住了。

知道线条如何以不同的颜色显示吗?

非常感谢!

【问题讨论】:

  • 你应该包括一个可重现的例子
  • 你可以使用ggplot(my_six_countries, aes(x = Year, y = GDP.growth.rate, col = whatever_country_name_variable_is) + geom_line()

标签: r plot colors


【解决方案1】:

我只是想说我最终使用了一种不太优雅的方法 - 但它奏效了。

首先,我对我的国家/地区进行了子集化。

c1 <- subset(countries,countries$Country=="c1")
c2 <- subset(countries,countries$Country=="c2")
c3 <- subset(countries,countries$Country=="c3")

其次,我一一绘制线条。

plot(c1$Year, c1$GDP, type = "l", bty="l", col="brown")
lines(c2$Year, c2$GDP, col="cornflowerblue")
lines(c3$Year, c3$GDP, col="darkblue")

【讨论】:

    猜你喜欢
    • 2017-06-09
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2019-10-09
    • 2011-01-02
    • 1970-01-01
    相关资源
    最近更新 更多