调整多个X轴标签的分组条形图中簇之间的间距。

huangapple go评论88阅读模式
英文:

Adjusting space between clusters on clustered bar chart with multiple x-axis labels

问题

我遇到了以下绘图问题:

我似乎无法改变两个簇之间的间距。我希望每个簇中的条形图互相接触,就像它们现在一样,但是在簇之间添加额外的间距。我尝试了许多解决方案,包括position_dodge()等,但它们对绘图的外观没有产生影响。是因为x轴上有两个级别吗?

这是我的数据:

results <- dput(results)
structure(list(tdcs = c("active", "active", "active", "active", 
"active", "active", "sham", "sham", "sham", "sham", "sham", "sham"
), type = structure(c(2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 
1L, 1L), .Label = c("Comprehension", "Trained Words", "Untrained Words"
), class = "factor"), chgacc = c(0.693669355, 0.512145161, 0.189221837, 
0.19727383, 0.488085538, 0.080855379, 0.365701923, 0.331008065, 
-0.405006036, -0.210909879, -0.135833333, 0.006145503), grouping = c("Active: 1 Month Post", 
"Active: 3 Months Post", "Active: 1 Month Post", "Active: 3 Months Post", 
"Active: 1 Month Post", "Active: 3 Months Post", "Sham: 1 Month Post", 
"Sham: 3 Months Post", "Sham: 1 Month Post", "Sham: 3 Months Post", 
"Sham: 1 Month Post", "Sham: 3 Months Post"), time = c("1 Month Post", 
"3 Months Post", "1 Month Post", "3 Months Post", "1 Month Post", 
"3 Months Post", "1 Month Post", "3 Months Post", "1 Month Post", 
"3 Months Post", "1 Month Post", "3 Months Post")), row.names = c(NA, 
-12L), spec = structure(list(cols = list(tdcs = structure(list(), class = c("collector_character", 
"collector")), type = structure(list(), class = c("collector_character", 
"collector")), chgacc = structure(list(), class = c("collector_double", 
"collector")), grouping = structure(list(), class = c("collector_character", 
"collector")), time = structure(list(), class = c("collector_character", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1L), class = "col_spec"), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

和我的代码:

library(tidyverse)
library(cowplot)

results$type <- as.factor(results$type)

colorpicks <- c("darkslategray", "azure3", "blue3", "cyan")

plot <- results %>%
  dplyr::filter(type == "Trained Words") %>%
  ggplot(aes(x = time, y = chgacc, fill = grouping)) +
  theme_minimal_hgrid() +
  geom_col(position = position_dodge(0.9), width = 1) + 
  facet_wrap(~tdcs, strip.position = "bottom", scales = "free_x" ) +
  scale_fill_manual(values = colorpicks, labels = c('Active - 1 Month Post', 'Active - 3 Months Post',
                                                    'Sham - 1 Month Post', 'Sham - 3 Months Post')) +
  theme(panel.spacing = unit(0, "lines"),
        strip.background = element_blank(),
        strip.placement = "outside") +
  scale_y_continuous(labels = scales::label_percent(accuracy = 1), limits = (c(-0.2, 0.7))) +
  labs(x = "Stimulation Type", y = "Mean Accuracy \nChange from Baseline", fill = 'Stimulation Type & \nTime Point') +
  theme(plot.title = element_text(size = 16),
        strip.text = element_text(size = 16), 
        axis.text = element_text(size = 12),
        axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        legend.text = element_text(size = 11))
plot
英文:

I am having issues with the following plot:

调整多个X轴标签的分组条形图中簇之间的间距。

I cannot seem to change the space between the two clusters. I would like the bars in each cluster to touch each other as they are now, but to add additional space between the clusters. I have tried numerous solutions, including position_dodge() etc, but they don't make a difference to the appearance of the plot. Is it because there are two levels on the x-axis?

Here is my data:

results &lt;- dput(results)
structure(list(tdcs = c(&quot;active&quot;, &quot;active&quot;, &quot;active&quot;, &quot;active&quot;, 
&quot;active&quot;, &quot;active&quot;, &quot;sham&quot;, &quot;sham&quot;, &quot;sham&quot;, &quot;sham&quot;, &quot;sham&quot;, &quot;sham&quot;
), type = structure(c(2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 
1L, 1L), .Label = c(&quot;Comprehension&quot;, &quot;Trained Words&quot;, &quot;Untrained Words&quot;
), class = &quot;factor&quot;), chgacc = c(0.693669355, 0.512145161, 0.189221837, 
0.19727383, 0.488085538, 0.080855379, 0.365701923, 0.331008065, 
-0.405006036, -0.210909879, -0.135833333, 0.006145503), grouping = c(&quot;Active: 1 Month Post&quot;, 
&quot;Active: 3 Months Post&quot;, &quot;Active: 1 Month Post&quot;, &quot;Active: 3 Months Post&quot;, 
&quot;Active: 1 Month Post&quot;, &quot;Active: 3 Months Post&quot;, &quot;Sham: 1 Month Post&quot;, 
&quot;Sham: 3 Months Post&quot;, &quot;Sham: 1 Month Post&quot;, &quot;Sham: 3 Months Post&quot;, 
&quot;Sham: 1 Month Post&quot;, &quot;Sham: 3 Months Post&quot;), time = c(&quot;1 Month Post&quot;, 
&quot;3 Months Post&quot;, &quot;1 Month Post&quot;, &quot;3 Months Post&quot;, &quot;1 Month Post&quot;, 
&quot;3 Months Post&quot;, &quot;1 Month Post&quot;, &quot;3 Months Post&quot;, &quot;1 Month Post&quot;, 
&quot;3 Months Post&quot;, &quot;1 Month Post&quot;, &quot;3 Months Post&quot;)), row.names = c(NA, 
-12L), spec = structure(list(cols = list(tdcs = structure(list(), class = c(&quot;collector_character&quot;, 
&quot;collector&quot;)), type = structure(list(), class = c(&quot;collector_character&quot;, 
&quot;collector&quot;)), chgacc = structure(list(), class = c(&quot;collector_double&quot;, 
&quot;collector&quot;)), grouping = structure(list(), class = c(&quot;collector_character&quot;, 
&quot;collector&quot;)), time = structure(list(), class = c(&quot;collector_character&quot;, 
&quot;collector&quot;))), default = structure(list(), class = c(&quot;collector_guess&quot;, 
&quot;collector&quot;)), skip = 1L), class = &quot;col_spec&quot;), class = c(&quot;spec_tbl_df&quot;, 
&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))

And my code:

library(tidyverse)
library(cowplot)

results$type &lt;- as.factor(results$type)


colorpicks &lt;- c(&quot;darkslategray&quot;, &quot;azure3&quot;,&quot;blue3&quot;, &quot;cyan&quot;)


plot &lt;- results %&gt;%
  dplyr::filter(type == &quot;Trained Words&quot;) %&gt;%
  ggplot(aes(x = time, y = chgacc, fill = grouping)) +
  theme_minimal_hgrid()+
  geom_col(position = position_dodge(0.9), width = 1) + 
  facet_wrap(~tdcs, strip.position = &quot;bottom&quot;, scales = &quot;free_x&quot; ) +
  scale_fill_manual(values = colorpicks, labels = c(&#39;Active - 1 Month Post&#39;,&#39;Active - 3 Months Post&#39;,
                                                    &#39;Sham - 1 Month Post&#39;, &#39;Sham - 3 Months Post&#39;))+
  theme(panel.spacing = unit(0,&quot;lines&quot;),
      strip.background = element_blank(),
      strip.placement = &quot;outside&quot;)+

  scale_y_continuous(labels = scales::label_percent(accuracy = 1),limits = (c(-.2,.7))) +
  labs(x = &quot;Stimulation Type&quot;, y = &quot;Mean Accuracy \nChange from Baseline&quot;, fill = &#39;Stimulation Type &amp; \nTime Point&#39;)+
  theme(plot.title = element_text(size = 16),
        strip.text = element_text(size = 16), 
        axis.text = element_text(size = 12),
        axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        legend.text=element_text(size=11))
plot

答案1

得分: 0

为了增加聚类之间的间距,您可以通过增加 x 比例的扩展,例如使用 scale_x_discrete(expand = c(0, 1.2)),它会将默认的扩展翻倍:

library(tidyverse)
library(cowplot)

results %>%
  dplyr::filter(type == "Trained Words") %>%
  ggplot(aes(x = time, y = chgacc, fill = grouping)) +
  theme_minimal_hgrid() +
  geom_col(position = position_dodge(0.9), width = 1) +
  facet_wrap(~tdcs, strip.position = "bottom", scales = "free_x") +
  scale_fill_manual(values = colorpicks, labels = c(
    "Active - 1 Month Post", "Active - 3 Months Post",
    "Sham - 1 Month Post", "Sham - 3 Months Post"
  )) +
  theme(
    panel.spacing = unit(0, "lines"),
    strip.background = element_blank(),
    strip.placement = "outside"
  ) +
  scale_y_continuous(labels = scales::label_percent(accuracy = 1), limits = (c(-.2, .7))) +
  scale_x_discrete(expand = c(0, 1.2)) +
  labs(x = "Stimulation Type", y = "Mean Accuracy \nChange from Baseline", fill = "Stimulation Type & \nTime Point") +
  theme(
    plot.title = element_text(size = 16),
    strip.text = element_text(size = 16),
    axis.text = element_text(size = 12),
    axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1),
    axis.title.y = element_blank(),
    axis.title.x = element_blank(),
    legend.text = element_text(size = 11)
  )

调整多个X轴标签的分组条形图中簇之间的间距。

英文:

To add more space between your clusters you could increase the expansion of the x scale using e.g. scale_x_discrete(expand = c(0, 1.2)) which doubles the default expansion:

library(tidyverse)
library(cowplot)

results %&gt;%
  dplyr::filter(type == &quot;Trained Words&quot;) %&gt;%
  ggplot(aes(x = time, y = chgacc, fill = grouping)) +
  theme_minimal_hgrid() +
  geom_col(position = position_dodge(0.9), width = 1) +
  facet_wrap(~tdcs, strip.position = &quot;bottom&quot;, scales = &quot;free_x&quot;) +
  scale_fill_manual(values = colorpicks, labels = c(
    &quot;Active - 1 Month Post&quot;, &quot;Active - 3 Months Post&quot;,
    &quot;Sham - 1 Month Post&quot;, &quot;Sham - 3 Months Post&quot;
  )) +
  theme(
    panel.spacing = unit(0, &quot;lines&quot;),
    strip.background = element_blank(),
    strip.placement = &quot;outside&quot;
  ) +
  scale_y_continuous(labels = scales::label_percent(accuracy = 1), limits = (c(-.2, .7))) +
  scale_x_discrete(expand = c(0, 1.2)) +
  labs(x = &quot;Stimulation Type&quot;, y = &quot;Mean Accuracy \nChange from Baseline&quot;, fill = &quot;Stimulation Type &amp; \nTime Point&quot;) +
  theme(
    plot.title = element_text(size = 16),
    strip.text = element_text(size = 16),
    axis.text = element_text(size = 12),
    axis.text.x = element_text(angle = 90, vjust = 0, hjust = 1),
    axis.title.y = element_blank(),
    axis.title.x = element_blank(),
    legend.text = element_text(size = 11)
  )

调整多个X轴标签的分组条形图中簇之间的间距。

huangapple
  • 本文由 发表于 2023年4月7日 02:49:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定