2014-04-16 1 views
0

내가 여기에 게시 된 이전 같은 질문이 있습니다R-스타일 ggplot로 축 - 다시

library(ggplot2) 

d <- data.frame(x=1:10, y=rnorm(10)) 

base_breaks_x <- function(x){ 
    b <- pretty(x) 
    d <- data.frame(y=-Inf, yend=-Inf, x=min(b), xend=max(b)) 
    list(geom_segment(data=d, aes(x=x, y=y, xend=xend, yend=yend)), 
     scale_x_continuous(breaks=b)) 
} 
base_breaks_y <- function(x){ 
    b <- pretty(x) 
    d <- data.frame(x=-Inf, xend=-Inf, y=min(b), yend=max(b)) 
    list(geom_segment(data=d, aes(x=x, y=y, xend=xend, yend=yend)), 
     scale_y_continuous(breaks=b)) 
} 

ggplot(d, aes(x,y)) + 
    geom_point() + 
    theme_bw() + 
    opts(panel.border = theme_blank(), 
     panel.grid.major = theme_blank(), 
     panel.grid.minor = theme_blank()) + 
    base_breaks_x(d$x) + 
    base_breaks_y(d$y) 

이 만 작동하고 있음을 발견 R-style axes with ggplot 내가 밥 티스트에 의해 제안 된 솔루션을 시도를 줄거리 미학 은 aes (x, y)로 구성됩니다. 프레임에서 데이터 그리기 한 열에는 색상이 변경되는 요인이 포함됩니다.

d <- data.frame(x=1:10, y=rnorm(10),name=rep(c("blue","red"),5)) 
ggplot(d, aes(x,y,colour=name))+ ... 

는이 문제가 해결 될 수있는 방법 오류 메시지

"Error in eval(expr, envir, enclos) : object 'name' not found" 

을 준다?

도움 주셔서 감사합니다.

+0

ggtheme 패키지 열려있는 기능 요청으로 IMO 청소기 솔루션이있다면 : [여기 토론] (https://github.com/jrnold/ggthemes/pull/18) – baptiste

답변

0

당신이 ggplot() 전화 내부 colour=namebase_breaks_x()을 설정 때문에이 오류가 발생했습니다 및 base_breaks_y() 또한 이러한 기능의 dataframes 내부 변수 name를 찾습니다 geom_segment() 호출이 포함되어 있습니다. 문제를 해결하는 방법에는 두 가지가 있습니다.

먼저 colour=nameggplot()에서 aes()까지 geom_point()으로 이동하십시오.

ggplot(d, aes(x,y)) + 
    geom_point(aes(colour=name)) 

둘째는 geom_segment() 전화 내부 inherit.aes=FALSE을 추가하여 기능 base_breaks_x()base_breaks_y()을 수정합니다.

geom_segment(data=d, aes(x=x, y=y, xend=xend, yend=yend),inherit.aes=FALSE) 
+0

예를 참조하십시오! 이제는 ggplot의 내부 동작을 더 잘 이해합니다! 감사! – catfranz