2012-10-01 5 views
1

데이터 프레임의 경우 strip.text.x에 맞게하는 방법 : X제목 문자열이 너무 긴

Date   Duration Test 
1/1/2012  10  This is the first that took place on1/1/2012 
2/1/2012  3   This test peformed the best result 

내가 그래프 피팅 strip.text.x 향하고 주위 과제를 데. 아무도 권고 사항이 있습니까 어떻게 해결할 수 있습니까? 당신이 facet_grid보다는 facet_wrap를 사용할 수있는 경우

ggplot(x, aes(Date, Duration, group=Test, colour=Test)) 
+ geom_line() 
+ geom_smooth(method="lm", se=T, size=1) 
+ facet_wrap(~Test, scale="free") 
+ opts(strip.text.x = theme_text(size=8, colour="navyblue")) 

답변

2

, 당신은 임의의 텍스트를 포장하기 위해 labeller 인수의 기능을 사용할 수 있습니다. (facet_wraplabeller 인수가 (yet)하지 않습니다.)

레이블을 래핑하는 x 재현

x <- 
structure(list(Date = structure(c(-719143, -718778), class = "Date"), 
    Duration = c(10L, 3L), Test = c("This is the first that took place on1/1/2012", 
    "This test peformed the best result")), .Names = c("Date", 
"Duration", "Test"), row.names = c(NA, -2L), class = "data.frame") 

도우미 기능을 확인

library("plyr") 
label_wrap_gen <- function(width = 25) { 
    function(variable, value) { 
     laply(strwrap(as.character(value), width=width, simplify=FALSE), 
      paste, collapse="\n") 
    } 
} 

플로팅 코드 (자세한 내용 on the ggplot2 wiki에서 논의). 단지 두 개의 값이 주어지기 때문에 점으로 변경되었으므로 선과 다듬기가 의미가 없습니다. 그러나 그것들은 어쨌든 질문의 초점이 아니 었습니다.

ggplot(x, aes(Date, Duration, group=Test, colour=Test)) + 
    geom_point() + 
    facet_grid(~Test, scale="free", labeller=label_wrap_gen(width=10)) + 
    opts(strip.text.x = theme_text(size=8, colour="navyblue")) 

enter image description here

편집 : 당신을 위해 작동하지 않습니다

facet_grid 때문에, 직접 Test 변수로 줄 바꿈을 포함하고 facet_wrap을 사용할 수 있습니다.

x$Test <- laply(strwrap(as.character(x$Test), width=10, simplify=FALSE), 
       paste, collapse="\n") 
ggplot(x, aes(Date, Duration, group=Test, colour=Test)) + 
    geom_point() + 
    facet_wrap(~Test, scale="free") + 
    opts(strip.text.x = theme_text(size=8, colour="navyblue")) 

enter image description here

내가 코멘트에서 만든 헤더에 대한 다른 질문을 이해하지 않습니다. 아마도 새롭고 명확한 질문을 던지십시오.

+0

그리드가 더 많으므로이 점이 저에게 효과적입니다. 이것은 잘 알고 있습니다. – user1471980

+0

헤더 표시와 관련하여 다른 질문이 있습니다. y-axix의 오른쪽 위와 오른쪽에 텍스트를 넣으려면 어떻게해야합니까? 이제 y 축에 날짜가 있고 그래프 상단에 테스트가 있습니다. 나는 90도 천사의 y 축척 오른쪽에 다른 표제를 넣고 싶다. – user1471980

관련 문제