Skip to content

Instantly share code, notes, and snippets.

@aravindhebbali
Last active September 25, 2017 09:40
Show Gist options
  • Save aravindhebbali/84b3204eee81d6e804f10a73900809b5 to your computer and use it in GitHub Desktop.
Save aravindhebbali/84b3204eee81d6e804f10a73900809b5 to your computer and use it in GitHub Desktop.
ggplot2: Line Charts
# 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