首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将模式添加到ggplot boxplot

将模式添加到ggplot boxplot
EN

Stack Overflow用户
提问于 2021-09-23 14:35:02
回答 1查看 168关注 0票数 0

我希望能够操纵我的每一个盒子图的填充模式。我有下面的代码为我提供了一个盒子图,我试着使用ggpattern来设置一个模式,但我不能给这个图的每一列分配一个特定的模式。

我可以使用geom_boxplot_pattern创建一些模式,但我无法更改显示的模式类型以满足我的需求。

感谢任何能帮上忙的人,贝斯特,瓦伦蒂娜

代码语言:javascript
复制
g<-ggplot(data=data, aes(Instr1,seek, color=Instr1)) +
geom_boxplot(color="black", fill=col1,alpha = 0.5,outlier.shape = NA) +
  geom_jitter( position = position_jitter(0.2)) + 
  scale_color_manual(values=c("black", "black","black", "black"))+
  stat_summary(fun=mean, geom="point", shape=23, size=4,color="black", stroke = 1)+
  theme_classic()+
  theme(axis.title.x=element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x=element_blank())+
  theme(axis.title.y=element_blank(),
        axis.text.y = element_text(size=20,color="black"),
        axis.ticks.y=element_blank())+
  theme(legend.position = "none")+
  ylim(-1.8, 1.8)``` 



g+ geom_boxplot_pattern( aes( pattern = task )) ```
EN

回答 1

Stack Overflow用户

发布于 2021-09-23 17:04:08

我希望我理解了你的问题。这是我想出来的:

首先创建一个新列,为每个变量分配一个任务。

然后,您可以使用scale_pattern_manual()和scale_pattern_fill_manual()手动为每个任务分配图案和颜色。

代码语言:javascript
复制
#remotes::install_github("coolbutuseless/ggpattern")

library("ggplot2")
library("ggpattern")

plot_data <- iris

## create new column to assign tasks to groups of variables
plot_data$task <- ifelse(plot_data$Species == "setosa", "task1", "task2")

ggplot(plot_data, aes(x=Species, y=Sepal.Length)) +
  geom_boxplot_pattern(aes(pattern = task, pattern_fill = task), pattern_density = 0.35, outlier.shape = NA) +
  scale_pattern_manual(values= c("task1" = "crosshatch", "task2" = "stripe")) + # manually assign pattern
  scale_pattern_fill_manual(values=c("task1" = "red", "task2" = "lightblue")) + # manually assign colors
  geom_jitter(position = position_jitter(0.2), aes(color=task)) +
  theme_classic()

ggpattern manually change pattern and color

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69302065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档