forked from jennybc/ggplot2-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-merge-color-into-data.r
More file actions
22 lines (17 loc) · 888 Bytes
/
02-merge-color-into-data.r
File metadata and controls
22 lines (17 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
library(plyr)
## data import from local file
gDat <- read.delim("gapminderDataFiveYear.tsv")
country_colors <- read.delim("gapminder-country-colors.tsv",
colClasses = list(color = "character"))
str(country_colors)
## 'data.frame': 142 obs. of 3 variables:
## $ continent: Factor w/ 5 levels "Africa","Americas",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ country : Factor w/ 142 levels "Afghanistan",..: 95 39 43 28 118 121 127 6..
## $ color : chr "#7F3B08" "#833D07" "#873F07" "#8B4107" ...
## insert color as a variable in gDat
gDat <- merge(gDat, country_colors)
## Sort by year (increasing) and population (decreasing)
## Why? So larger countries will be plotted "under" smaller ones.
gDatOrdered <- arrange(gDat, year, desc(pop))
write.table(gDatOrdered, "gapminderDataColorReadySorted.tsv",
quote = FALSE, sep = "\t", row.names = FALSE)