MYaseen208
MYaseen208

Reputation: 23898

Extracting and plotting facet_wrap elements along with same legend key with ggplot2

I want to plot each element of facet_wrap separately along with the same legend key. I know this can be achieved with subset on class but in that case the legend key can be different for different levels of class variable. Here is my MWE.

library(ggplot2)
ggplot(mpg, aes(displ, hwy, color=factor(cyl))) +
  geom_point() +
  facet_wrap(~class)

enter image description here

Actually this is required for administrative plots to plot different states with the same legend key but at different latitude and longitude.

Edited

library(XML)
library(ggplot2)
library(plyr)
library(maps)

unemp <-
  readHTMLTable('http://www.bls.gov/web/laus/laumstrk.htm',
                colClasses = c('character', 'character', 'numeric'))[[2]]

names(unemp) <- c('rank', 'region', 'rate')
unemp$region <- tolower(unemp$region)

us_state_map <- map_data('state')
map_data <- merge(unemp, us_state_map, by = 'region')

map_data <- arrange(map_data, order)
map_data$subregion <- rep(c("A", "B"), c(8000, 7537))

states <- data.frame(state.center, state.abb)

p1 <- ggplot(data = map_data, aes(x = long, y = lat, group = group))
p1 <- p1 + geom_polygon(aes(fill = cut_number(rate, 5)))
p1 <- p1 + geom_path(colour = 'gray', linestyle = 2)
p1 <- p1 + scale_fill_brewer('Unemployment Rate (Jan 2011)', palette  = 'PuRd')
p1 <- p1 + coord_map()
p1 <- p1 + geom_text(data = states, aes(x = x, y = y, label = state.abb, group = NULL), size = 2)
p1 <- p1 + theme_bw()
p1

enter image description here

p1 + facet_wrap(~subregion, nrow=1)

enter image description here

p2 <- ggplot(data = subset(map_data, subregion=="A"), aes(x = long, y = lat, group = group))
p2 <- p2 + geom_polygon(aes(fill = cut_number(rate, 5)))
p2 <- p2 + geom_path(colour = 'gray', linestyle = 2)
p2 <- p2 + scale_fill_brewer('Unemployment Rate (Jan 2011)', palette  = 'PuRd')
p2 <- p2 + coord_map()
p2 <- p2 + geom_text(data = states, aes(x = x, y = y, label = state.abb, group = NULL), size = 2)
p2 <- p2 + theme_bw()
p2

enter image description here

p3 <- ggplot(data = subset(map_data, subregion=="B"), aes(x = long, y = lat, group = group))
p3 <- p3 + geom_polygon(aes(fill = cut_number(rate, 5)))
p3 <- p3 + geom_path(colour = 'gray', linestyle = 2)
p3 <- p3 + scale_fill_brewer('Unemployment Rate (Jan 2011)', palette  = 'PuRd')
p3 <- p3 + coord_map()
p3 <- p3 + geom_text(data = states, aes(x = x, y = y, label = state.abb, group = NULL), size = 2)
p3 <- p3 + theme_bw()
p3

enter image description here

Please see last three plots have different scales and legend keys.

Upvotes: 1

Views: 471

Answers (1)

EDi
EDi

Reputation: 13280

Subset and use scale_color_discrete(drop = FALSE) to avoid dropping the unused factor levels.

Here's an example:

require(ggplot2)
mtcars$f_gear <- factor(mtcars$gear)
ggplot(mtcars, aes(x = disp, y = hp, col = f_gear)) +
  geom_point()

ggplot(mtcars[1:2, ], aes(x = disp, y = hp, col = f_gear)) +
  geom_point() +
  scale_color_discrete(drop = FALSE)

Update

You could specify the fill before plotting:

map_data$cuts <- cut_number(map_data$rate, 5)
p2 <- ggplot(data = subset(map_data, subregion=="A"), aes(x = long, y = lat, group = group))
p2 <- p2 + geom_polygon(aes(fill = cuts))
p2 <- p2 + geom_path(colour = 'gray', linestyle = 2)
p2 <- p2 + scale_fill_brewer('Unemployment Rate (Jan 2011)', palette  = 'PuRd')
p2 <- p2 + coord_map()
p2 <- p2 + geom_text(data = states, aes(x = x, y = y, label = state.abb, group = NULL), size = 2)
p2 <- p2 + theme_bw()
p2

p3 <- ggplot(data = subset(map_data, subregion=="B"), aes(x = long, y = lat, group = group))
p3 <- p3 + geom_polygon(aes(fill = cuts))
p3 <- p3 + geom_path(colour = 'gray', linestyle = 2)
p3 <- p3 + scale_fill_brewer('Unemployment Rate (Jan 2011)', palette  = 'PuRd')
p3 <- p3 + coord_map()
p3 <- p3 + geom_text(data = states, aes(x = x, y = y, label = state.abb, group = NULL), size = 2)
p3 <- p3 + theme_bw()
p3

Upvotes: 1

Related Questions