ggplot图例用于不同的变量而不是填充

时间:2015-01-21 22:51:33

标签: r ggplot2 legend

我正在尝试做一些ggplot hack。我想通过一个变量(变量= Open.Burning)为条形图和'legendize'着色,但条形图标签和框周围的另一个变量。我的大部分时间都在下降,但我仍在努力根据变量Open.Burning来攻击传奇。

library(ggplot2)
library(scales)
library(grid)
muns = data.frame(cbind(
  'Open.Burning'=c(0,0,0,0,1), 
  'Population.2010'=c(486,4130,843,2648,3950), 
  'Municipality'=c('Alert Bay', 'Mount Waddington RD-uninc', 'Port Alice', 'Port McNeill', 'Port Hardy'), 
  'Regional.District'=rep('Mount Waddington', 5), 
  'mp'=c(243.0, 2551.0, 5037.5, 6783.0, 10082.0),
  'Open.Burning.Label'=c('Bylaw', 'No Bylaw', 'No Bylaw', 'No Bylaw', 'No Bylaw')), stringsAsFactors=FALSE)
muns$Population.2010 <- as.numeric(muns$Population.2010)
muns$mp <- as.numeric(muns$mp)

fp = factor(muns[,'Open.Burning'], 
            labels=c('white', 'lightblue1'), 
            levels=c(0,1))
fill.vals = as.character(fp)
names(fill.vals) = muns$Municipality

ggplot(muns, aes(x=Regional.District, y=Population.2010, fill=Municipality)) +
  geom_bar(stat='identity', width=0.6, colour = "gray32") +
  xlab('Open Burning') + ylab("Population 2010") +
  scale_y_continuous(labels = comma) +
  geom_text(aes(y = muns$mp, label=muns$Municipality), colour = "gray32", size = 3) +
  scale_fill_manual(values=fill.vals, breaks=muns$Municipality, labels=muns$Open.Burning.Label) +
  ## to add a border the legend (but not on the chart)
  guides(fill = guide_legend(reverse=TRUE, override.aes = list(colour = "black"))) +
  theme(text=element_text(size=12),
        axis.text.x  = element_blank(),
        legend.title=element_blank()
  )

我目前得到的是什么:

current plot

我想得到什么: wanted plot

我真的不知道如何做到这一点。任何想法?
谢谢!

1 个答案:

答案 0 :(得分:1)

只需要通过Open.Bylaw.Label填写并修正颜色因子。

library(ggplot2)
library(scales)
library(grid)
muns = data.frame(cbind(
  'Open.Burning'=c(0,0,0,0,1), 
  'Population.2010'=c(486,4130,843,2648,3950), 
  'Municipality'=c('Alert Bay', 'Mount Waddington RD-uninc', 'Port Alice', 'Port McNeill', 'Port Hardy'), 
  'Regional.District'=rep('Mount Waddington', 5), 
  'mp'=c(243.0, 2551.0, 5037.5, 6783.0, 10082.0),
  'Open.Burning.Label'=c('Bylaw', 'No Bylaw', 'No Bylaw', 'No Bylaw', 'No Bylaw')), stringsAsFactors=FALSE)
muns$Population.2010 <- as.numeric(muns$Population.2010)
muns$mp <- as.numeric(muns$mp)

fp = factor(muns[,'Open.Burning.Label'], 
            labels=c('white', 'lightblue1'), 
            levels=c('No Bylaw','Bylaw'))
fill.vals = as.character(fp)
names(fill.vals) = muns$Open.Burning.Label

ggplot(muns, aes(x=Regional.District, y=Population.2010, fill=Open.Burning.Label)) +
  geom_bar(stat='identity', width=0.6, colour = "gray32") +
  xlab('Open Burning') + ylab("Population 2010") +
  scale_y_continuous(labels = comma) +
  geom_text(aes(y = muns$mp, label=muns$Municipality), colour = "gray32", size = 3) +
  scale_fill_manual(values=fill.vals, breaks=muns$Open.Burning.Label, labels=muns$Open.Burning.Label) +
  ## to add a border the legend (but not on the chart)
  guides(fill = guide_legend(reverse=TRUE, override.aes = list(colour = "black"))) +
  theme(text=element_text(size=12),
        axis.text.x  = element_blank(),
        legend.title=element_blank()
  )