Reduce the distance between the axis and the geometry

I have a plot that looks like this:

enter image description here

How can I remove the gap between the lava edge of the line and the y axis without cutting my labels?

Code:

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)) 
+4
source share
1 answer

A workaround would be to add an empty level to Paul.

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

Then in scale_x_discrete() use drop=FALSE to show this level. Now you can also change the expand= values.

  + scale_x_discrete(expand = c(0.01, 0.01),drop=FALSE) 

enter image description here

Another possibility is to use Gender as a number, and then scale_x_continuous() limits= with scale_x_continuous() , and then provide breaks= and labels= to get the 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)) 
+4
source

Source: https://habr.com/ru/post/1488808/


All Articles