gpt4 book ai didi

减少轴和几何体之间的空间

转载 作者:行者123 更新时间:2023-12-01 01:07:44 24 4
gpt4 key购买 nike

我有一个看起来像这样的情节:

enter image description here

如何在不切断直接标签的情况下删除线的左边缘和 y 轴之间的空间?

编码:

library(ggplot2)
library(scales)
labs <- c("Special Ed., Charter School",
"Gen. Ed., Charter School",
"Special Ed., Public School",
"Gen. Ed., Public School")

pot <- data.frame(Engagement = c(643, 793, 590, 724, 735, 928, 863, 662),
Classroom = rep(rep(c("Special Ed.", "Gen. Ed."), each = 2), 2),
Gender = factor(rep(c("Male", "Female"), 4), levels = c("Male", "Female")),
School = rep(c("Charter", "Public"), each = 4),
ID = factor(rep(1:4, each = 2)),
labs = factor(rep(labs, each=2), levels=labs)
)

library(directlabels)

xout <- ggplot(pot, aes(y = Engagement, x = Gender, group = ID)) +
geom_line(aes(color=labs), size=2) + theme_classic() +
scale_x_discrete(expand = c(.1, .3)) +
scale_color_hue(l=40) +
theme(text = element_text(size=22))

direct.label(xout, list('last.qp', cex=1.35))

最佳答案

解决方法是向 Gender 添加空级别。

pot$Gender<-factor(pot$Gender,levels=c("Male","Female",""))

然后在 scale_x_discrete()使用 drop=FALSE来展现这个水平。现在您也可以更改 expand=值。
 + scale_x_discrete(expand = c(0.01, 0.01),drop=FALSE)

enter image description here

另一种可能性是使用您的 Gender作为数字,然后用 scale_x_continuous()套装 limits=然后提供 breaks=labels=再次获得正确的标签。
xout <- ggplot(pot, aes(y = Engagement, x = as.numeric(Gender), group = ID)) + 
geom_line(aes(color=labs), size=2) + theme_classic() +
scale_x_continuous(expand=c(0,0),"Gender",breaks=c(1,2),
labels=c("Male","Female"),limits=c(0.95,4)) +
scale_color_hue(l=40) +
theme(text = element_text(size=22))

关于减少轴和几何体之间的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17376753/

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