我将ggplot脚本包装到一个函数中,如下所示。但是,运行此命令将给出错误消息,如
xy.coords中的错误(x,y,xlabel,ylabel,log):
'x‘是一个列表,但没有组件'x’和'y‘
如果不将这些脚本包装成函数,它们就能正常工作。因此,如何编写这样的函数,它生成了一个树状图使用to图,并将它保存到一个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()
}发布于 2011-12-20 15:41:06
这是一个非常常见的问题:您必须使用print()来打印图形。
d <- function(){
g1 <- qplot(...)
print(g1)
}https://stackoverflow.com/questions/8577811
复制相似问题