Hi so I have got my 2 separated line charts with 3 lines on each chart done. Now I want to put them on a facet with one on top another. How can I do this?
Here is my code:
ggplot(chinamor, aes(x=`Year`))+
geom_line(aes(y=`China Life Expectancy`), color="red")+
geom_line(aes(y=`Russia Life Expectancy`), color="blue")+
geom_line(aes(y=`United Kingdom Life Expectancy`), color="yellow")
ggplot(chinamor, aes(x=`Year`))+
geom_line(aes(y=`China Mortality`), color="red")+
geom_line(aes(y=`Russia Mortality`), color="blue")+
geom_line(aes(y=`United Kingdom Mortality`), color="yellow")
The easiest solution, I think, is to reshape your data to have the columns Year, Country, Variable, and Value. The Variable column would have the values Life Expectancy and Mortality. The ggplot code could then be
ggplot(chinamor_reshaped, aes(x = Year, y = Value, color = Country)) + geom_line() +
facet_wrap(~ Variable, ncol = 1) +
scale_color_manual(values = c(China = "red", Russia = "blue", United Kingdom = "yellow"))
I have not tested that code since I don't have your data. If you need more help, please post the output of
dput(chinamor)
or if the data set is very large,
dput(head(chinamor, 25))
where 25 is the number of rows of data. Change that to a value that makes sense for your data.