ggplot2:scale_shape_manual

ggplot2で水準ごとに色分けすると結構わかりづらい。

library(rdatamarket)
library(reshape2)
library(ggplot2)
getDM <- function(url) {
    data <- dmseries(url)
    data <- data.frame(year = index(data), as.data.frame(data))
    data <- melt(data, id.vars = "year")
    colnames(data) <- c("year", "country", "value")
    invisible(data)
}
url <- "http://datamarket.com/data/set/15n6/health-expenditure-public-of-gdp#display=line&ds=15n6|hnt"
res <- getDM(url)
smp <- subset(res, country %in% c("Sudan", "Mexico", "Kuwait", "Germany", 
    "Finland", "France", "Canada", "Japan", "United.States"))
ggplot(smp, aes(x = year, y = value, group = country)) + geom_line(aes(color = country)) + 
    geom_point(aes(color = country))

plot of chunk unnamed-chunk-1

形で分けたくなるけど水準が多い場合デフォルトだとshapeが足りなくなる。

ggplot(smp, aes(x = year, y = value, group = country)) + geom_line(aes(color = country)) + 
    geom_point(aes(color = country, shape = country))

plot of chunk unnamed-chunk-2

したがってmanualで指定してやる必要がある。

ggplot(smp, aes(x = year, y = value, group = country)) + geom_line(aes(color = country)) + 
    geom_point(aes(color = country, shape = country)) + scale_shape_manual(values = 0:length(unique(smp$country)))

plot of chunk unnamed-chunk-3

symbolの番号対応表はこちら
http://wiki.stdout.org/rcookbook/Graphs/Shapes%20and%20line%20types/