我想将facet_wrap
的每个元素与相同的图例键分开绘制。我知道这可以通过类的子集来实现,但在这种情况下,对于类变量的不同级别,图例键可以是不同的。这是我的MWE。
library(ggplot2)
ggplot(mpg, aes(displ, hwy, color=factor(cyl))) +
geom_point() +
facet_wrap(~class)
实际上,行政图需要使用相同的图例键绘制不同的状态,但纬度和经度不同。
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
p1 + facet_wrap(~subregion, nrow=1)
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
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
请看最后三个地块有不同的比例和图例键。
答案 0 :(得分:1)
子集并使用scale_color_discrete(drop = FALSE)
以避免丢弃未使用的因子级别。
以下是一个例子:
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)
<强>更新强>
您可以在绘图前指定填充:
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