gpt4 book ai didi

ggplot2 - scale_x_discrete 中 x 轴刻度线之间的间距太远

转载 作者:行者123 更新时间:2023-12-02 12:04:00 25 4
gpt4 key购买 nike

我正在使用 ggplot2 制作一个简单的图表,但它有太多空白。

Comparison graph

我希望这两个点在 x 轴上靠得更近,这样整体图像就更小。我使用scale_x_discrete 和以下代码:

ggplot(box, aes(x=Garden, y=Fitness)) + 
geom_errorbar(aes(ymin=(Fitness-Error), ymax=(Fitness+Error)),
colour="black", width=.05) +
geom_line() +
geom_point(size=6)+
theme(panel.background = element_rect(fill =
'white'),axis.text=element_text(size=22),
axis.title=element_text(size=28,face="bold"),legend.key = element_rect(fill = "white"), plot.title = element_text(size=30,face="bold", hjust=0.5),axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 0)), axis.ticks.length = unit(0, "lines"),legend.text=element_text(size=24),legend.title=element_text(size=26), legend.key.size = unit(1.5, 'lines'))+
ggtitle("Fitness Comparison") + labs(y="Ranked Fitness", x = "Common Garden") +
scale_x_discrete(labels=c("Warm" = "Warm Limit", "Cool" = "Cool Limit"), expand=c(0.2, 0))

最佳答案

减少两个误差线之间空白的一种方法是使用带有 scale_x_continuous 的连续 x 变量。

box <- data.frame(Garden=c("Cool","Warm"),
Fitness=c(2980,2050),
Error=c(50,50))

# Convert Garden from factor to numeric
box$Garden <- as.numeric(box$Garden)

ggplot(box, aes(x=Garden, y=Fitness)) +
geom_errorbar(aes(ymin=(Fitness-Error), ymax=(Fitness+Error)), colour="black", width=.05, lwd=1) +
geom_point(size=6)+
theme(panel.background = element_rect(fill='white'),
axis.text=element_text(size=22),
axis.title=element_text(size=28,face="bold"),
legend.key=element_rect(fill = "white"),
plot.title=element_text(size=30,face="bold", hjust=0.5),
axis.title.y=element_text(margin = margin(t = 0, r = 20, b = 0, l = 0)),
axis.ticks.length=unit(0, "lines"),
legend.text=element_text(size=24),
legend.title=element_text(size=26),
legend.key.size = unit(1.5, 'lines')) +
ggtitle("Fitness Comparison") +
labs(y="Ranked Fitness", x = "Common Garden") +
scale_x_continuous(labels=c("Cool Limit","Warm Limit"),
breaks=c(1,2), limits=c(0,3), expand=c(0,0))

enter image description here

关于ggplot2 - scale_x_discrete 中 x 轴刻度线之间的间距太远,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46006563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com