【发布时间】:2015-01-21 22:51:33
【问题描述】:
我正在尝试做一些 ggplot hack。我想用一个变量(变量= Open.Burning)为我的条形图和“传奇化”着色,但堆栈周围的条形图标签和框由另一个变量。我已经完成了大部分工作,但我仍在努力根据变量 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()
)
我目前得到的:
我想得到什么:
我真的不知道如何做到这一点。有什么想法吗?
谢谢!
【问题讨论】:
-
您提供的内容不可重现
-
@RStudent 谢谢你,我编辑了帖子
-
仍然无法重现。始终在干净的会话中进行测试。
-
为什么不使用
fill = Open.Burning.Label? -
@joran 谢谢,在干净的会话中测试,已修复。