2011-12-20 2 views
1

ggplot 스크립트를 다음과 같이 나열된 함수로 래핑합니다. 그러나,이 하나를 실행하는 것은 나에게 그런ggplot 및 ggdendro : dendrogram을 생성하고 pdf 파일에 저장하는 경우의 문제

xy.coords 오류와 같은 오류 메시지 제공 (X, Y, xlabel, ylabel을, 로그) :

'X'목록입니다,하지만이 없습니다 구성 요소 'x'및 'y'

이 스크립트를 함수에 랩핑하지 않고도 정상적으로 작동합니다. 따라서 ggplot을 사용하여 dendrogram을 생성하고 pdf에 저장하는 이런 종류의 함수를 작성하는 방법.

nicedendro<-function(inputdat, outputfile){ 
    library(ggdendro) 
    library(ggplot2) 
    x <- read.table(inputdat, head=TRUE) 
    y <- 1-x 
    d <- as.dist(y,diag=FALSE,upper=FALSE) 
    hc <- hclust(d,"ave") 
    dhc <- as.dendrogram(hc) 
    ddata <- dendro_data(dhc,type="rectangle") 
    ddata$labels$text <- gsub("\\."," ",ddata$labels$text) 
    pdf(outputfile, width=30,height=35) 

    plot(ggplot(segment(ddata)) + 
     geom_segment(aes(x=x0,y=y0,xend=x1,yend=y1)) + 
     xlab(NULL) + 
     ylab(NULL) + 
     scale_x_discrete(limits=ddata$labels$text) + 
     opts(panel.grid.major = theme_blank()) + 
     opts(panel.grid.minor=theme_blank()) + 
     coord_flip()) 


    dev.off() 
} 

답변

5

이것은 매우 자주 묻는 질문입니다. 그래프를 인쇄하려면 print()을 사용해야합니다.

d <- function(){ 
    g1 <- qplot(...) 
    print(g1) 
} 
+0

인쇄를 호출하지 않고 ggplot 객체에서 plot을 호출하면 약간의 비밀이 있습니다 ... OP가받는 오류를 설명 할 수 있습니다. –

+0

필자는 ggplot2-object를 사용하여'plot (g1)'이'print (g1)'에 발송된다는 인상을 받았습니다. –

+0

하지만 내가 틀렸어 위에서 언급 한 오류 메시지가 나타납니다. –

관련 문제