Last active
September 25, 2017 09:40
-
-
Save aravindhebbali/84b3204eee81d6e804f10a73900809b5 to your computer and use it in GitHub Desktop.
ggplot2: Line Charts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install | |
install.packages('ggplot2') | |
install.packages('readr') | |
# library | |
library(ggplot2) | |
library(readr) | |
# import data | |
gdp <- readr::read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/gdp.csv') | |
gdp | |
# Line Chart | |
ggplot(gdp, aes(year, india)) + | |
geom_line() | |
# Line Color | |
ggplot(gdp, aes(year, india)) + | |
geom_line(color = 'blue') | |
# Line Type | |
ggplot(gdp, aes(year, india)) + | |
geom_line(linetype = 2) | |
# Line Type (Dashed) | |
ggplot(gdp, aes(year, india)) + | |
geom_line(linetype = 'dashed') | |
# Line Size | |
ggplot(gdp, aes(year, india)) + | |
geom_line(size = 2) | |
# modify data | |
gdp2 <- gdp %>% | |
select(year, growth, india, china) %>% | |
gather(key = country, value = gdp, -year) | |
gdp2 | |
# Grouped Line Chart | |
ggplot(gdp2, aes(year, gdp, group = country)) + | |
geom_line() | |
# Map Color to Country | |
ggplot(gdp2, aes(year, gdp, group = country)) + | |
geom_line(aes(color = country)) | |
# Map Line Type to Country | |
ggplot(gdp2, aes(year, gdp, group = country)) + | |
geom_line(aes(linetype = country)) | |
# Map Line Width to Country | |
ggplot(gdp2, aes(year, gdp, group = country)) + | |
geom_line(aes(size = country)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment