[英]How to change the size of x-axis for categorical variables in ggplot2?
I am tryig to adjust the size of my x-axis in a plot so that it won't have too much blank space in the plot area.我正在尝试在 plot 中调整 x 轴的大小,以便在 plot 区域中不会有太多空白空间。
This is my plot:这是我的 plot:
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))
This is the data being used to make the graph:这是用于制作图表的数据:
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
The graph from the code is this one below:代码中的图表如下:
This is how I want it to be:这就是我想要的样子:
I have already tried a lot of things, including expand or change the size of the bars, adjusting the size of the window and etc. It changes the size of the bar, the size of the window, but the space between the categories stays the same.我已经尝试了很多东西,包括扩展或更改条的大小,调整 window 的大小等。它改变了条的大小,window 的大小,但类别之间的空间保持不变相同的。
Thanks in adavance.提前致谢。
You can get pretty close by using theme(asepct.ratio = 1)
and increasing the width =
in geom_bar
and geom_errorbar
.您可以通过使用
theme(asepct.ratio = 1)
并增加geom_bar
和geom_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))
Please note that I had to change the colors in my locale and manually set up your x-axis label because it wasn't provided.请注意,我必须在我的语言环境中更改 colors 并手动设置您的 x 轴 label,因为它没有提供。
Try adding this to your ggplot
call:尝试将此添加到您的
ggplot
调用中:
+ theme(axis.text=element_text(size=16),
axis.title=element_text(size=16,face="bold"))
Play around with the text size and see.玩弄文本大小并查看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.