2013-06-29 2 views
4

사이의 공간을 감소?내가 다음과 같습니다 플롯을 가지고 축과 기하 구조

코드 :

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

해결 방법은 성별에 빈 수준을 추가하는 것입니다.

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)) 
+0

+1 매력처럼 작동했습니다. –

관련 문제