[英]R: plot multiple lines in different colours from subset of database
I've created a database with six different countries and multiple GDP and inequality measures. 我创建了一个包含六个不同国家和多个GDP和不平等衡量指标的数据库。
For starters, I want to plot the GDP growth of the countries in one plot. 首先,我想在一个图中绘制这些国家的GDP增长。 This works out perfectly fine: 这工作得很好:
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)
However, I want the lines for the different countries to be displayed in different colours and not just 600. I virtually spend the whole day on this super nooby problem and I've tried all sort of things from creating a colour vector over subsetting manually to playing with ggplot - but I'm really stuck. 但是,我希望不同国家/地区的线条以不同的颜色显示,而不仅仅是600。我几乎整天都在处理超级nooby问题,并且尝试了各种方法,从手动创建子集创建颜色矢量到玩ggplot-但我真的很困。
Any idea how the lines could be displayed in different colours? 知道如何用不同的颜色显示线条吗?
Thank you so much! 非常感谢!
I just wanted to say that I ended up using a way less elegant method - but it worked. 我只是想说,我最终使用了一种不太优雅的方法-但却奏效了。
Firstly, I subsetted my countries. 首先,我将我的国家归为一类。
c1 <- subset(countries,countries$Country=="c1")
c2 <- subset(countries,countries$Country=="c2")
c3 <- subset(countries,countries$Country=="c3")
Secondly, I plotted the lines one by one. 其次,我一条一条地画线。
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.