2012-11-04 11 views
36

축 레이블을 expression() 문으로 두 줄에 쓰고 싶습니다. 그러나 plotmathexpression은 허용하지 않습니다 (예 : 아래 첨자 텍스트가 맨 오른쪽에 나타남). 비슷한 문제가 2005 년경 this discussion 인 것으로 나타 났지만 제공하는 해결 방법은 ggplot2에서 내 응용 프로그램으로 변환되지 않습니다. A recent question은 여러 줄 표현식 문의 다른 순열을 다루었지만 여기에 제공된 해결 방법은 여기에 적용되지 않습니다.ggplot2 표현식이있는 두 줄 레이블

예 :

p <- ggplot(mtcars,aes(x=wt,y=mpg))+ 
    geom_point()+ 
    xlab(expression(paste("A long string of text goes here just for the purpose \n of illustrating my point Weight "[reported]))) 
try(ggsave(plot=p,filename=<some file>,height=4,width=6)) 

내가 이전 단어 옆에 앉아 그것을 싶습니다 때 첨자를 오른쪽으로 쫓겨된다 "보고했다"이미지를 얻을 수 있습니다. ggplot2 two line label with expression

+0

왜 여기에 식을 필요합니까 (즉 plotmath)? 캐릭터가 강한 경우, 캐릭터 벡터에'\ n'을 띄우십시오. –

+0

아마도 귀하의 제안을 이해할 수 없지만 레이블에 \ n을 포함시킵니다. 신청서에 특정 기호를 사용하려면 표현식이 필요합니다 (예 : 첨자 및 학위). – metasequoia

+0

맞아, 당신의 예제는 expression()을 필요로하지 않는다. 간단한 paste()가 할 것이다. ? plotmath –

답변

46

나는 이것이 버그라고 생각합니다. (또는 연결된 대화에서 언급 한 것처럼 "여러 줄 표현식이 지원되지 않습니다"라는 사실의 결과). 개빈 심슨에 언급

해결 방법은 다음과 같습니다

#For convenience redefine p as the unlabeled plot 
p <- ggplot(mtcars,aes(x=wt,y=mpg))+geom_point() 

#Use atop to fake a line break 
p + xlab(expression(atop("A long string of text for the purpose", paste("of illustrating my point" [reported])))) 

enter image description here

그건 사실이 라인 아래 첨자와 함께 나누기 사용할 수 있습니다. 귀하의 예제와 같은 형태를 가지고 아래의 간단한 예제에서, 첨자 올바르게 텍스트의 나머지 부분에 인접 배치되어 있지만 텍스트의 두 줄이 제대로 중앙에 있지 :

p + xlab(expression(paste("line1 \n line2 a" [b]))) 

enter image description here

두 경우 모두 텍스트의 위쪽 줄이 텍스트의 아래쪽 줄보다 길면 아래 첨자가 잘못 배치 된 것 같습니다. 첨자는 항상 바로 위 라인의 오른쪽 끝의 오른쪽 정렬을 끝

p + xlab(expression(paste("abc \n abcd" [reported]))) 

enter image description here

p + xlab(expression(paste("abc \n ab" [reported]))) 

enter image description here

비교.

p + xlab(expression(paste("abcdefghijklmnop \n ab" [reported]))) 

enter image description here

+1

에있는 atop() 연산자를 참조하십시오. 위 첨자에 대해서는 무엇을해야할지 모르지만 두 번째 줄의 * start *가 약간 오른쪽으로 이동하는 이유는 개행 문자 뒤에 여분의 공백이 있기 때문입니다. 적어도 p + xlab (expression (paste ("abcdefghijklmnop \ nab"[reported])))) 대신 대신 p + xlab (expression (paste ("abcdefghijklmnop \ nab"[보고 된])) * 같은 위치에 * 시작한다. ... 그러나 이것은 아래 첨자 앞에보기 흉한 갭을 피하기는 어렵다. – user3482899

9

당신이 트릭을 사용할 수,

library(gridExtra) 
library(grid) 

element_custom <- function() { 
    structure(list(), class = c("element_custom", "element_text")) 
} 

element_grob.element_custom <- function(element, label="", ...) { 

    mytheme <- ttheme_minimal(core = list(fg_params = list(parse=TRUE, 
                 hjust=0, x=0.1))) 
    disect <- strsplit(label, "\\n")[[1]] 
    tableGrob(as.matrix(disect), theme=mytheme) 
} 

# default method is unreliable 
heightDetails.gtable <- function(x) sum(x$heights) 

ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 
    geom_line() + 
    labs(x= "First~line \n italic('and a second') \n integral(f(x)*dx, a, b)")+ 
    (theme_grey() %+replace% theme(axis.title.x = element_custom())) 

enter image description here

+0

은 나에게 매우 유용 할 것입니다 ... 하지만 R 3.4.0 및 ggplot2_2.2.1에서는 가치가 없습니다. ( – julou