提问人:confusedindividual 提问时间:11/16/2023 最后编辑:stefanconfusedindividual 更新时间:11/16/2023 访问量:29
向条形图添加图案
adding pattern to a barplot
问:
我在条形图中只使一个分组条形具有模式时遇到问题。我只想让中间的栏(白色的)有一个图案。我也希望它们都有黑色的轮廓。现在,代码使它们三个都有模式。
#load 库
library(ggplot2)
library(ggpattern)
#create 数据集
specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
value <- abs(rnorm(12 , 0 , 15))
data <- data.frame(specie,condition,value)
data
#create 情节
#right 现在,这使得所有柱线都有图案。我只想让中间的条形有一个图案,我希望所有的条形都有黑色的轮廓
ggplot(data, aes(fill=condition, y=value, x=specie)) +
geom_bar(position="dodge", stat="identity")+ theme_classic() +
theme(text=element_text(size=16, family="Sans"))+ labs(x='Team', y='Points', title='Avg. Points Scored by Position & Team') +
theme(plot.title = element_text(hjust=0.5, size=20, face='bold')) +
scale_fill_manual('Position', values=c('black', 'white', 'lightgrey')) +
geom_col_pattern(position = "dodge",
pattern = "stripe",
pattern_angle = 45,
pattern_density = .1,
pattern_spacing = .04,
pattern_fill = 'black') +
guides(fill = guide_legend(override.aes =
list(
pattern = c("none", "stripe", "none"),
pattern_spacing = .01,
pattern_angle = c(0, 45, 0)
)
))
答:
2赞
stefan
11/16/2023
#1
实现所需结果的一种选择是在 aes 上映射,然后通过 设置所需的模式,即将其设置为您不需要模式的类别。对于你的大纲集.另请注意,我删除了 .condition
pattern
scale_pattern_manual
"none"
color="black"
geom_bar
library(ggplot2)
library(ggpattern)
set.seed(123)
ggplot(data, aes(fill = condition, y = value, x = specie)) +
scale_fill_manual(
"Position",
values = c("black", "white", "lightgrey")
) +
scale_pattern_manual(
"Position",
values = c("none", "stripe", "none")
) +
geom_col_pattern(
aes(pattern = condition),
position = "dodge",
pattern_angle = 45,
pattern_density = .1,
pattern_spacing = .04,
pattern_fill = "black",
color = "black"
) +
theme_classic() +
theme(
text = element_text(size = 16, family = "sans"),
plot.title = element_text(hjust = 0.5, size = 20, face = "bold")
) +
labs(
x = "Team", y = "Points",
title = "Avg. Points Scored by Position & Team"
)
评论