在R中,如何消除position_dodge2后小节之间的多余空格?

问题描述 投票:0回答:1

我想从我的数据中生成一个分组条形图,其中包含七个不同的条件(两个因素),每个处理有三个重复。参考组("水")是未分组的,因为它只包含一个条件,并显示为一个单一的条形图,其他的处理是分组的。我使用position_dodge2 with preserve = "single "来保持所有条形图的宽度相同,但是单条形图周围有很多空间,我想在保持与其他条形图相同宽度的同时,把这些空间去掉。这怎么可能呢?

我试着在position_dodge2中添加宽度和padding,但没有成功。 我想避免分面,保持其他条形图的宽度,而 "孤独 "的那个条形图则以其X轴标签为中心。

我的数据(data2)。

structure(list(glue = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 3L, 
3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Methylcellulose", 
"Water", "Xanthan"), class = "factor"), sug = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 2L, 
2L, 3L, 3L, 3L), .Label = c("Control", "Mannitol", "Sorbitol"
), class = "factor"), sg = c(85, 91.76470588, 89.79591837, 69.76744186, 
93.82716049, 92.59259259, 52.94117647, 77.21518987, 84.81012658, 
87.5, 87.05882353, 83.13253012, 68.6746988, 55.55555556, 73, 
68.6746988, 82.71604938, 93.75, 34.52380952, 83.52941176, 86.9047619
)), class = "data.frame", row.names = c(NA, -21L))

这是我按照所需的顺序排序后的图。

ggplot(data = data2, aes(x =glue, y = sg, fill = sug)) + 
  stat_summary(geom = "bar", position = position_dodge2(width = 2, preserve = "single"), color = "black") +
  stat_summary(fun.data = mean_se, geom = "errorbar", position = position_dodge2( preserve = "single", padding = 0.7)) +
  theme_classic() +
  ylab("Germination rate [%]") +
  theme(legend.position = "bottom",
        text = element_text(size = 12, color = "black"),
        axis.text = element_text(size = 12, color = "black"),
        axis.title = element_text(face = "bold", vjust = 3),
        legend.title = element_text(face = "bold"),
        axis.line = element_line(size=1),
        axis.ticks.x = element_line (size = 1)) + 
  scale_x_discrete("Seed coat") +
  scale_fill_manual(name = "Sugar",values = c("White", "LightGrey", "#838B8B")) +
  scale_y_continuous(limits = c(0,100)) 

谢谢!

ggplot2 position bar-chart padding
1个回答
0
投票

你可以用 geom_bar(position = position_dodge(width = 0.9))像这样。

ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) +
  geom_bar(position = position_dodge(width = 0.9))

这是否是一个选择。

library(ggplot2)
ggplot(mtcars, aes(x=as.factor(vs),fill = as.factor(cyl))) +
  geom_bar(position = 'dodge')+
  facet_grid(~cyl,scales = "free_x", space = "free_x")

创建于2020-06-11 重读包 (v0.3.0)


这里还有一种方法。将圆柱和vs的表格转换为data.frame。在相应地重命名列后,创建一个很低的圆柱体==8和vs==1的虚数,小到看不到条形图,然后创建ggplot。

结果是在正确的位置有大小相等的条形图。

library(tidyverse)
df <- data.frame(table(mtcars$cyl, mtcars$vs))

df %>% 
  rename('cyl' = 'Var1', 'vs' = 'Var2') %>% 
  mutate(Freq = ifelse(cyl == 8 & vs == 1, 0.001, Freq)) %>% #glimpse()
  ggplot() + geom_col(aes(cyl, Freq, fill =vs), position = 'dodge')

创建于2020-06-11,由 重读包 (v0.3.0)


0
投票

对我来说,这有点难以理解。这个问题随着时间的推移发生了很大的变化,以至于后来登陆这个页面的人都会有困难。这个问题开始时没有你的样本数据,但有MTCars的数据。所以我认为你应该修改你的问题,删除开头的MTCars。我可能会删除我的第一个答案,稍后。

你的解释我也不是很清楚。但我的理解是在下面考虑的。我仍然不确定,如果这是你要找的,但希望永远不会结束。进一步的解释可以在reprex中找到。

library(ggplot2)
# Your data
df <- structure(list(glue = structure(c(
  2L, 2L, 2L, 1L, 1L, 1L, 3L,
  3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L
), .Label = c(
  "Methylcellulose",
  "Water", "Xanthan"
), class = "factor"), sug = structure(c(
  1L,
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 2L,
  2L, 3L, 3L, 3L
), .Label = c("Control", "Mannitol", "Sorbitol"), class = "factor"), sg = c(
  85, 91.76470588, 89.79591837, 69.76744186,
  93.82716049, 92.59259259, 52.94117647, 77.21518987, 84.81012658,
  87.5, 87.05882353, 83.13253012, 68.6746988, 55.55555556, 73,
  68.6746988, 82.71604938, 93.75, 34.52380952, 83.52941176, 86.9047619
)), class = "data.frame", row.names = c(NA, -21L))


# you said you wanted the lonely bar in first position. You can therefore change the 
# order of factor levels like this
df$glue <- factor(df$glue, levels = c('Water', 'Methylcellulose', 'Xanthan'))

# you also want to reduce space between the reference bar (the lonely bar?) and the
# grouped bar. You also said you wanted the plot to look like the second or last plot
# on the webpage you are referencing to. But those plots look different. The difference
# is the width of the reference bar. If you want the bar to use the same width as 
# the grouped bar, which I believe, you need to change preserve to total.


ggplot(data = df, aes(x = glue, y = sg, fill = sug)) +
  stat_summary(geom = "col", position = position_dodge2(width = 2, preserve = "total"), color = "black") +
  stat_summary(fun.data = mean_se, geom = "errorbar", position = position_dodge2(preserve = "single", padding = 0.7)) +
  theme_classic() +
  ylab("Germination rate [%]") +
  theme(
    legend.position = "bottom",
    text = element_text(size = 12, color = "black"),
    axis.text = element_text(size = 12, color = "black"),
    axis.title = element_text(face = "bold", vjust = 3),
    legend.title = element_text(face = "bold"),
    axis.line = element_line(size = 1),
    axis.ticks.x = element_line(size = 1)
  ) +
  scale_x_discrete("Seed coat") +
  scale_fill_manual(name = "Sugar", values = c("White", "LightGrey", "#838B8B")) +
  scale_y_continuous(limits = c(0, 100))
#> No summary function supplied, defaulting to `mean_se()`

创建于2020-06-13,由 重读包 (v0.3.0)

© www.soinside.com 2019 - 2024. All rights reserved.