Skip to content

Instantly share code, notes, and snippets.

@aravindhebbali
Last active September 26, 2017 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aravindhebbali/0036a9cc1ca779965f45c38af260dcdc to your computer and use it in GitHub Desktop.
Save aravindhebbali/0036a9cc1ca779965f45c38af260dcdc to your computer and use it in GitHub Desktop.
Data Visualization: Legends
# data
year <- seq(2010, 2014, 1)
india <- c(10.3, 6.6, 5.6, 6.6, 7.2)
china <- c(10.6, 9.5, 7.8, 7.7, 7.3)
russia <- c(4.5, 4.3, 3.5, 1.3, 0.7)
brazil <- c(7.5, 3.9, 1.9, 3.0, 0.1)
s_africa <- c(3.2, 3.2, 2.2, 2.2, 1.5)
gdp <- data.frame(year, india, china, russia, brazil, s_africa, stringsAsFactors = FALSE)
gdp
# line graph
plot(gdp$year, gdp$india, type = 'l',
ylim = c(-5, 15), xlab = 'Year',
ylab = 'Growth Rate', col = 'blue',
main = 'BRICS: Growth Rate')
lines(gdp$year, gdp$china, col = 'red')
lines(gdp$year, gdp$russia, col = 'green')
lines(gdp$year, gdp$brazil, col = 'yellow')
lines(gdp$year, gdp$s_africa, col = 'violet')
# legend location
plot(gdp$year, gdp$india, type = 'l',
ylim = c(-5, 15), xlab = 'Year',
ylab = 'Growth Rate', col = 'blue',
main = 'BRICS: Growth Rate')
lines(gdp$year, gdp$china, col = 'red')
legend(x = 2013, y = 14, legend = c('india', 'china'))
# line type and color
plot(gdp$year, gdp$india, type = 'l',
ylim = c(-5, 15), xlab = 'Year',
ylab = 'Growth Rate', col = 'blue',
main = 'BRICS: Growth Rate')
lines(gdp$year, gdp$china, col = 'red')
legend(x = 2012, y = 14, legend = c('india', 'china'),
lty = 1, col = c('blue', 'red'))
# point size, shape and color
plot(gdp$year, gdp$india, type = 'l',
ylim = c(-5, 15), xlab = 'Year',
ylab = 'Growth Rate', col = 'blue',
main = 'BRICS: Growth Rate')
points(gdp$year, gdp$india, pch = 2, col = 'blue')
lines(gdp$year, gdp$china, col = 'red')
points(gdp$year, gdp$china, pch = 2, col = 'red')
legend(x = 2012, y = 14, legend = c('india', 'china'),
lty = 1, pch = 2, col = c('blue', 'red'))
# text placement
plot(gdp$year, gdp$india, type = 'l',
ylim = c(-5, 15), xlab = 'Year',
ylab = 'Growth Rate', col = 'blue',
main = 'BRICS: Growth Rate')
lines(gdp$year, gdp$china, col = 'red')
legend(x = 2011, y = 14, legend = c('india', 'china'),
lty = 1, col = c('blue', 'red'),
horiz = TRUE)
plot(gdp$year, gdp$india, type = 'l',
ylim = c(-5, 15), xlab = 'Year',
ylab = 'Growth Rate', col = 'blue',
main = 'BRICS: Growth Rate')
lines(gdp$year, gdp$china, col = 'red')
lines(gdp$year, gdp$russia, col = 'green')
lines(gdp$year, gdp$brazil, col = 'yellow')
legend(x = 2012, y = 0, legend = c('india', 'china', 'russia', 'brazil'),
lty = 1, col = c('blue', 'red', 'green', 'yellow'),
ncol = 2)
# legend title
plot(gdp$year, gdp$india, type = 'l',
ylim = c(-5, 15), xlab = 'Year',
ylab = 'Growth Rate', col = 'blue',
main = 'BRICS: Growth Rate')
lines(gdp$year, gdp$china, col = 'red')
legend(x = 2012, y = 14, legend = c('india', 'china'),
lty = 1, col = c('blue', 'red'),
title = 'Nations')
# title color
plot(gdp$year, gdp$india, type = 'l',
ylim = c(-5, 15), xlab = 'Year',
ylab = 'Growth Rate', col = 'blue',
main = 'BRICS: Growth Rate')
lines(gdp$year, gdp$china, col = 'red')
legend(x = 2012, y = 14, legend = c('india', 'china'),
lty = 1, col = c('blue', 'red'),
title = 'Nations', title.col = 'red')
# title color
plot(gdp$year, gdp$india, type = 'l',
ylim = c(-5, 15), xlab = 'Year',
ylab = 'Growth Rate', col = 'blue',
main = 'BRICS: Growth Rate')
lines(gdp$year, gdp$china, col = 'red')
legend(x = 2012, y = 0, legend = c('india', 'china'),
lty = 1, col = c('blue', 'red'), horiz = TRUE,
title = 'Nations', title.adj = 0.1)
# background color
plot(gdp$year, gdp$india, type = 'l',
ylim = c(-5, 15), xlab = 'Year',
ylab = 'Growth Rate', col = 'blue',
main = 'BRICS: Growth Rate')
lines(gdp$year, gdp$china, col = 'red')
legend(x = 2012, y = 0, legend = c('india', 'china'),
lty = 1, col = c('blue', 'red'), horiz = TRUE,
title = 'Nations', title.adj = 0.1)
# border
plot(gdp$year, gdp$india, type = 'l',
ylim = c(-5, 15), xlab = 'Year',
ylab = 'Growth Rate', col = 'blue',
main = 'BRICS: Growth Rate')
lines(gdp$year, gdp$china, col = 'red')
legend(x = 2012, y = 0, legend = c('india', 'china'),
lty = 1, col = c('blue', 'red'), title = 'Nations',
box.lty = 3, box.lwd = 1.5, box.col = 'green')
# text appearance
plot(gdp$year, gdp$india, type = 'l',
ylim = c(-5, 15), xlab = 'Year',
ylab = 'Growth Rate', col = 'blue',
main = 'BRICS: Growth Rate')
lines(gdp$year, gdp$china, col = 'red')
legend(x = 2012, y = 0, legend = c('india', 'china'),
lty = 1, col = c('blue', 'red'), title = 'Nations',
text.col = 'green', text.font = 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment