r - 添加阴影效果ggplot2条形图(barplot)

标签 r ggplot2

我试图在图中展示较差的设计选择。人们使用的一种浪费墨水、可能分散注意力的效果是条形的阴影效果。我想让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()

enter image description here

此尝试给出以下警告,并且透明层被忽略(这是明智的):

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
   

最佳答案

我想也许这就是您正在寻找的......?

ggplot(dat) +
    geom_bar(aes(x = as.integer(School) + .2, y= Percent - .5),stat = "identity", alpha=.2,width = 0.6) +
    geom_bar(aes(x = as.integer(School), y=Percent, fill = School),stat = "identity",width = 0.6) +
    scale_x_continuous(breaks = 1:6,labels = as.character(dat$School)) +
    theme_bw()

enter image description here

关于r - 添加阴影效果ggplot2条形图(barplot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28997809/

相关文章:

r - R 中的 corrplot.mixed - 使对角线中的标签更小

r - "Error in colnames"合并xts集时

r - 如何在 R 中进行分类时间序列预测?

r - 通过 R 中的矩阵列表进行索引

r - ggplot2 中自动计算 sec_axis 公式的双轴图

r - ggplot2 条形图的多个子组

r - 在ggplot中调整文本在错误栏上方的位置

r - 在 R dplyr 中按计数展开列

r - 我可以在不将函数解析为单独文件的情况下运行 package.skeleton 吗?

r - R中ggplot2标签内的下标