本文介绍了减少轴和几何之间的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似这样的情节:
I have a plot that looks like this:
如何在不切断我的直接标签的情况下删除线条的上边缘和y轴之间的空间?
How can I remove the space between the laft edge of the line and the y axis without cutting off my directlabels?
代码:
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))
推荐答案
解决方法是向性别"添加空级别.
Workaround would be to add empty level to Gender.
pot$Gender<-factor(pot$Gender,levels=c("Male","Female",""))
然后在scale_x_discrete()
中使用drop=FALSE
显示此级别.现在,您还可以更改expand=
值.
Then in scale_x_discrete()
use drop=FALSE
to show this level. Now you can change also expand=
values.
+ scale_x_discrete(expand = c(0.01, 0.01),drop=FALSE)
其他可能性是使用您的Gender
作为数字,然后将scale_x_continuous()
设置为limits=
,然后提供breaks=
和labels=
以再次获得正确的标签.
Other possibility is to use your Gender
as numeric and then with scale_x_continuous()
set limits=
and then provide breaks=
and labels=
to get correct labeling again.
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))
这篇关于减少轴和几何之间的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!