gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:28:30 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com