首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何分离条形图?

如何分离条形图?
EN

Stack Overflow用户
提问于 2019-10-09 21:36:34
回答 1查看 71关注 0票数 0

我正在尝试绘制一个表示双向方差的条形图,但是条形图是重叠的,有人知道一种简单的方法来绘制数据集吗?

代码语言:javascript
复制
data <- structure(list(nozzle = c("XR", "XR", "XR", "XR", "XR", "XR", "XR", "XR", 
                                  "XR", "XR", "XR", "XR", "XR", "XR", "XR", "XR", 
                                  "AIXR", "AIXR", "AIXR", "AIXR", "AIXR", "AIXR", 
                                  "AIXR", "AIXR", "AIXR", "AIXR", "AIXR", "AIXR", 
                                  "AIXR", "AIXR", "AIXR", "AIXR"), 
                       trat = c("Cle 12.8", "Cle 12.8", "Cle 12.8", "Cle 12.8", 
                                "Cle 34", "Cle 34", "Cle 34", "Cle 34", "Cle 12.8", 
                                "Cle 12.8", "Cle 12.8", "Cle 12.8", "Cle 34", "Cle 34", 
                                "Cle 34", "Cle 34", "Cle 12.8", "Cle 12.8", "Cle 12.8", 
                                "Cle 12.8", "Cle 34", "Cle 34", "Cle 34", "Cle 34", 
                                "Cle 12.8", "Cle 12.8", "Cle 12.8", "Cle 12.8", "Cle 34", 
                                "Cle 34", "Cle 34", "Cle 34"), 
                       adj = c("Without", "Without", "Without", "Without", "Without", 
                               "Without", "Without", "Without", "With", "With", "With", 
                               "With", "With", "With", "With", "With", "Without", "Without", 
                               "Without", "Without", "Without", "Without", "Without", "Without", 
                               "With", "With", "With", "With", "With", "With", "With", "With"), 
                       dw1 = c(3.71, 5.87, 6.74, 1.65, 0.27, 0.4, 0.37, 0.34, 0.24, 0.28, 0.32, 
                               0.38, 0.39, 0.36, 0.32, 0.28, 8.24, 10.18, 11.59, 6.18, 0.2, 0.23, 
                               0.2, 0.31, 0.28, 0.25, 0.36, 0.27, 0.36, 0.37, 0.34, 0.19)), 
                  row.names = c(NA, -32L), 
                  class = c("tbl_df", "tbl", "data.frame"))

data_sum <- summarySE(data,
                          measurevar="dw1",
                          groupvars=c("nozzle", "trat","adj"))

ggplot(data_sum,
       aes(x = as.factor(trat), y = dw1,group = as.factor(nozzle), fill = as.factor(adj),
           ymax=dw1+se, ymin=dw1-se))  +
  geom_bar(stat="identity", colour = "black", width = 0.2, show_guide = FALSE, position="dodge")  +
  scale_fill_manual(name = "Presence of adjuvants" ,
                    values = c('grey80', 'grey30'),
                    labels = c("Without",
                               "With"))  +
  geom_errorbar(position=position_dodge(width=0.7),
                width=0.0, size=0.5, color="black")  +
  labs(x = "Treatment g.i.a. ha-¹",
       y = "Dry Mass (g)")  +
  theme_classic()+ facet_grid(nozzle ~ ., scales = "free_y")

我预计会是这样的:

但在同一图中有两个因素和一个共同的Y轴。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-10 00:35:04

代码的问题在于,您需要使用函数dodge2而不是dodge。请参阅此reference。这是可以帮助你的代码。

代码语言:javascript
复制
ggplot(data_sum ,aes( x = trat, y = dw1,  group = nozzle, fill = adj)) + 
geom_bar( colour = "black", width = 0.5, stat="identity",
         # Position dodge2 solve the overlapping problem
         # (padding) Add distance between bar in the same group
         position = position_dodge2(padding = 0.5)) + 
scale_fill_manual(name = "Presence of adjuvants",
                 values = c('grey80', 'grey30'),
                 labels = c("Without",
                            "With")) +
geom_errorbar(aes(ymax = dw1 + se, ymin = dw1 - se),
             # Need to be consistent with bar position
             # (padding) Here it will control the width of
             # top and botton horizontal line. If you want
             # you can choose padding = 1 to not have those lines
             position = position_dodge2(padding = 1.3),
             # (width) Control the width of error_bar and
             # in this case has a side effect of shift the 
             # position of this error bar.
             # Then this width need to be the same of geom_bar
             width = 0.5, 
             size = 0.5,
             color="black")  +
labs(x = "Treatment g.i.a. ha-¹",
    y = "Dry Mass (g)") +
facet_grid(.  ~ nozzle, scales = "free_y")

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

https://stackoverflow.com/questions/58305460

复制
相关文章

相似问题

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