首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在R中使用ggpattern编辑图形

在R中使用ggpattern编辑图形
EN

Stack Overflow用户
提问于 2020-07-28 22:11:07
回答 1查看 602关注 0票数 0

我写了一些代码来制作图表(都在下面)

代码语言:javascript
复制
p <- ggplot(for_plots, aes(x = factor(condition), y = conflict, fill = smoking_status)) + 
  stat_summary(fun = "mean", geom = "bar", position = "dodge") +
  theme_classic() +
  scale_fill_manual(labels = c("Smokers", "Ex"),
                    values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
  scale_color_manual(labels = c("Smokers", "Ex"),
                     values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
  labs(x = 'Condition', y = 'Conflict (AUC)') + 
  scale_x_discrete(labels = c('Animal','Smoking')) +
  coord_cartesian(ylim=c(0,1.5)) +
  scale_y_continuous(expand = c(0,0)) 

p + 
  stat_summary(fun.data = mean_se, geom = "errorbar", width = .08, position = position_dodge(0.9))

然而,我最近读到了“ggpattern”,想知道是否有人可以帮我在我的图中的黄色条形图中添加一些对角线黑线(例如,前吸烟者冲突)。我尝试了多种方法,但将'geom_col_pattern‘添加到代码中似乎搞乱了Y轴,并且为每个条件(动物、吸烟)提供了总体冲突,而不是为吸烟者和前吸烟者提供单独的冲突。我认为'geom_col_pattern‘可能与我代码中的'stat_summary’不兼容。有人有什么建议吗?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-31 17:57:48

只需更新stat_summarygeom参数,而不是在绘图顶部添加geom_col_pattern

代码语言:javascript
复制
#replicate of your dataframe
for_plots <- data.frame(matrix(nrow = 100, ncol=0))
for_plots$condition <- sample(rep(c("Animal", "Smoking"), 100), 100)
for_plots$smoking_status <- sample(rep(c("Smokers", "Ex"), 100), 100)
n_smoking <- length(which(for_plots$condition == "Smoking"))
for_plots$conflict[for_plots$condition=="Smoking"] <- sample(seq(0.8, 1.3, length.out = n_smoking), n_smoking)
n_animal <- length(which(for_plots$condition == "Animal"))
for_plots$conflict[for_plots$condition=="Animal"] <- sample(seq(0.5, 1, length.out = n_animal), n_animal)
代码语言:javascript
复制
p <- ggplot(for_plots, aes(x = factor(condition), y = conflict, fill = smoking_status)) + 
  stat_summary(aes(pattern=smoking_status),
               fun = "mean", position = "dodge", 
               geom = "bar_pattern", pattern_fill="black", colour="black") + #edited part
  theme_classic() +
  scale_fill_manual(labels = c("Smokers", "Ex"),
                    values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
  scale_color_manual(labels = c("Smokers", "Ex"),
                     values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
  labs(x = 'Condition', y = 'Conflict (AUC)') + 
  scale_pattern_manual(values=c("none", "stripe"))+ #edited part
  scale_x_discrete(labels = c('Animal','Smoking')) +
  coord_cartesian(ylim=c(0,1.5)) +
  scale_y_continuous(expand = c(0,0)) 

p + 
  stat_summary(fun.data = mean_se, geom = "errorbar", width = .08, position = position_dodge(0.9))

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

https://stackoverflow.com/questions/63136113

复制
相关文章

相似问题

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