8/6/2023 0 Comments Plot line graph r![]() Geom_line(mapping = aes(x = year, y = pop)) Start Exercise Quiz: Malformed Plot gapminder_comparison <- filter(gapminder, country %in% c("Brazil", "China", "India")) Map the year to the x-axis and the population pop to the y-axis with aes().Use the data set gapminder_comparison in your ggplot() function which contains only data for the countries in question.Start Exercise Exercise: Compare populationsĬompare the population growth over the last decades in the countries Austria, Hungary and Serbia. Map the group and the color parameter to the country variable.Create a line graph with the geom_line() function.Use the data set gapminder_comparison in your ggplot() function which contains only data for the countries Japan, Brazil and India.Exercise: Compare life expectancyĬreate a line graph to compare the life expectancy lifeExp in the countries Japan, Brazil and India. ![]() Note that ggplot also separates the lines correctly if only the color mapping is specified (the group parameter is implicitly set). Geom_line(mapping = aes(x = year, y = lifeExp, gapminder_comparison <-įilter(gapminder, country %in% c("Austria", "Hungary")) To make the lines easier to distinguish we also map color to the country so that each country line has a different color. By specifying the country variable ggplot creates a separate line for each country. ![]() The group argument tells ggplot which observations belong together and should be connected through lines. Then, we set the variable country as the group argument for the aesthetic mapping. We first filter the dataset for both countries of interest. For example, let’s answer the question: How has life expectancy changed in the countries Austria and Hungary over time? Multiple line graphs show the absolute differences between observations but also how the specific trajectories relate to each other. Line graphs are often extended and used for the comparison of two or more lines. So far we only focused on single lines, but what if we have multiple countries in the dataset and want to somehow differentiate them? Start Exercise Adding more lines ggplot(_) +
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |