如何更改 ggplot2 中分类变量的 x 轴大小?

How to change the size of x-axis for categorical variables in ggplot2?

我正在尝试调整绘图中 x 轴的大小,以便绘图区域中不会有太多空白 space。

这是我的剧情:

criterio.fig <- ggplot(criterio_sum, aes(x = session,
                                y = mean,
                                fill = sham_cat)) +
        geom_bar(stat = "identity", position = "dodge", width = 0.5) +
        geom_errorbar(aes(ymin = se_low, ymax = se_high),
                      width = 0.2,
                      position = position_dodge(0.5)) +
        scale_fill_manual(values = c(roxo, verde)) +
        # Add a title
        ggtitle("") +
        scale_x_discrete(breaks = unique(criterio_sum$session), 
                         labels = c("Sessão 1", "Sessão 2")) +
        scale_y_continuous(expand = c(0, 0), limits = c(0, 0.5)) +
        # Customize the x-axis
        xlab("Sessão") +
        # Customize the y-axis
        ylab(eixo_y) +
        # Get rid of title for legend
        labs(fill = "") +
        # Remove dark background
        theme_minimal() +
        theme(panel.border = element_blank(),
              panel.grid = element_blank(),
              strip.background = element_rect(colour = "white", fill = "white"),
              legend.position = "top", axis.line = element_line(colour = "black"),
              axis.text.x = element_text(colour = "black", size = 11, vjust = 0.5),
              axis.text.y = element_text(colour = "black", size = 11, hjust = 0,
                                         margin = margin(r = 5)),
              axis.ticks.y = element_line(colour = "black"), 
              axis.ticks.length = unit(-0.07, "cm"),
              axis.title.x = element_text(colour = "black", face = "bold"),
              axis.title.y = element_text(colour = "black", face = "bold"),
              text = element_text(family = "Times New Roman", size = 13))

这是用来制作图表的数据:

  sham_cat session  medida    mean    sd     n      se se_low se_high
  <fct>    <fct>    <fct>    <dbl> <dbl> <int>   <dbl>  <dbl>   <dbl>
1 Sham     session1 Critério 0.209 0.264    48 0.00549  0.203   0.214
2 Sham     session2 Critério 0.338 0.412    51 0.00808  0.330   0.346
3 Catódica session1 Critério 0.184 0.282    49 0.00575  0.178   0.190
4 Catódica session2 Critério 0.319 0.362    50 0.00724  0.312   0.326

代码中的图表如下:

这就是我想要的样子:

我已经尝试了很多东西,包括扩展或改变条的大小,调整 window 的大小等等。它改变了条的大小, window,但类别之间的 space 保持不变。

提前致谢。

尝试将此添加到您的 ggplot 调用中:

+ theme(axis.text=element_text(size=16),
        axis.title=element_text(size=16,face="bold")) 

调整文字大小看看。

您可以使用 theme(asepct.ratio = 1) 并在 geom_bargeom_errorbar 中增加 width =

ggplot(criterio_sum, aes(x = session,
                         y = mean,
                         fill = sham_cat)) +
  geom_bar(stat = "identity", position = "dodge", width = 0.9) +
  geom_errorbar(aes(ymin = se_low, ymax = se_high),
                width = 0.2,
                position = position_dodge(0.9)) +
  scale_fill_manual(values = c("purple", "green")) +
  ggtitle("") +
  scale_x_discrete(breaks = unique(criterio_sum$session), 
                   labels = c("Sessão 1", "Sessão 2")) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 0.5)) +
  labs(x = "Sessão", y = expression(bold("Viés de Resposta"~(italic("c"))))) +
  labs(fill = "") +
  theme_minimal() +
  theme(aspect.ratio = 1,
        panel.border = element_blank(),
        panel.grid = element_blank(),
        strip.background = element_rect(colour = "white", fill = "white"),
        legend.position = "top", axis.line = element_line(colour = "black"),
        axis.text.x = element_text(colour = "black", size = 11, vjust = 0.5),
        axis.text.y = element_text(colour = "black", size = 11, hjust = 0,
                                   margin = margin(r = 5)),
        axis.ticks.y = element_line(colour = "black"), 
        axis.ticks.length = unit(-0.07, "cm"),
        axis.title.x = element_text(colour = "black", face = "bold"),
        axis.title.y = element_text(colour = "black", face = "bold"),
        text = element_text(family = "Times New Roman", size = 13))

请注意,我不得不更改语言环境中的颜色并手动设置您的 x 轴标签,因为没有提供。