【发布时间】:2015-05-13 21:40:34
【问题描述】:
我试图在情节中展示较差的设计选择。人们使用的一种浪费墨水的、可能会分散注意力的效果,如果是条的阴影效果。我想让 ggplot2 这样做。我的基本想法是使第一层半透明的条稍微高一点并向右移动。我可以得到稍高的,但不能得到稍向右的:
dat <- data_frame(
School =c("Franklin", "Washington", "Jefferson", "Adams", "Madison", "Monroe"),
sch = seq_along(School),
count = sort(c(13, 17, 12, 14, 3, 22), TRUE),
Percent = 100*round(count/sum(count), 2)
)
dat[["School"]] <- factor(dat[["School"]], levels = c("Franklin",
"Washington", "Jefferson", "Adams", "Madison", "Monroe"))
ggplot(dat) +
geom_bar(aes(x = School, weight=Percent + .5), alpha=.1, width = .6) +
geom_bar(aes(x = School, weight=Percent, fill = School), width = .6) +
theme_bw()
此尝试给出以下警告,并且透明层被忽略(这是明智的):
ggplot(dat) +
geom_bar(aes(x = School + .2, weight=Percent + .5), alpha=.1, width = .6) +
geom_bar(aes(x = School, weight=Percent, fill = School), width = .6) +
theme_bw()
## Warning messages:
## 1: In Ops.factor(School, 0.2) : ‘+’ not meaningful for factors
## 2: In Ops.factor(School, 0.2) : ‘+’ not meaningful for factors
【问题讨论】:
-
我认为这不是一个完整的答案,但请尝试颠倒
geom_bar层的顺序并将School显式转换为第二层的整数(在数据框中或@987654327 @调用)。 -
...使用
y和stat = "identity"而不是weight似乎更接近你想要的。 -
...如果我对两层都使用连续比例,我可以在后面(即首先)做灰色矩形,但当然这会破坏 x 轴标签,你会解决这个问题手动。 (很抱歉在 cmets 中对此进行了草拟……)
-
@joran 你的想法也帮助我实现了目标。这比你给我一个解决方案要好得多。