我在尝试保存图形时遇到问题。 我正在使用函数来绘制我已拍摄的规格图: How to draw gauge chart in R?在R上保存图表
该函数可以正常工作。当我运行它时,图形被创建为弹出式窗口。
gg.gauge <- function(pos) {
library("ggplot2")
library("utils")
breaks =c(0,33,66,100)
colors = c('red', 'gold', 'forestgreen')
get.poly <- function(a,b,r1=0.5,r2=1.0) {
th.start <- pi*(1-a/100)
th.end <- pi*(1-b/100)
th <- seq(th.start,th.end,length=100)
x <- c(r1*cos(th),rev(r2*cos(th)))
y <- c(r1*sin(th),rev(r2*sin(th)))
return(data.frame(x,y))
}
ggplot()+
geom_polygon(data=get.poly(breaks[1],breaks[2]),aes(x,y),fill=colors[1])+
geom_polygon(data=get.poly(breaks[2],breaks[3]),aes(x,y),fill=colors[2])+
geom_polygon(data=get.poly(breaks[3],breaks[4]),aes(x,y),fill=colors[3])+
geom_polygon(data=get.poly(pos-1,pos+1,0.2),aes(x,y))+
geom_text(data=as.data.frame(breaks), size=5, fontface="bold", vjust=0,
aes(x=1.1*cos(pi*(1-breaks/100)),y=1.1*sin(pi*(1-breaks/100)),label=paste0(breaks,"%")))+
annotate("text",x=0,y=0,label=pos,vjust=0,size=8,fontface="bold")+
coord_fixed()+
theme_bw()+
theme(axis.text=element_blank(),
axis.title=element_blank(),
axis.ticks=element_blank(),
panel.grid=element_blank(),
panel.border=element_blank())
}
当我执行这个函数(test.R)时,一切正常。文件image.png包含图形并位于正确的位置。
test <- function(){
png(filename = 'C:/shared/image.png',
width = 480, height = 480, units = "px", pointsize = 12,
bg = "transparent", res = NA, family = "", restoreConsole = TRUE,
type = "windows")
hist(1:5)
dev.off()
}
当我运行这个函数run_gg.R时会发生问题。它返回一个空白图像的文件。
run_gg <- function(pos) {
png(filename = 'C:/shared/image.png',
width = 480, height = 480, units = "px", pointsize = 12,
bg = "transparent", res = NA, family = "", restoreConsole = TRUE,
type = "windows")
gg.gauge(pos)
dev.off()
}
我修改函数gg.gauge.R包括德PNG和dev.off(gg.gauge2.R),但结果是相同的。
gg.gauge2 <- function(pos) {
library("ggplot2")
library("utils")
breaks =c(0,33,66,100)
colors = c('red', 'gold', 'forestgreen')
get.poly <- function(a,b,r1=0.5,r2=1.0) {
th.start <- pi*(1-a/100)
th.end <- pi*(1-b/100)
th <- seq(th.start,th.end,length=100)
x <- c(r1*cos(th),rev(r2*cos(th)))
y <- c(r1*sin(th),rev(r2*sin(th)))
return(data.frame(x,y))
}
png(filename = 'C:/shared/image.png',
width = 480, height = 480, units = "px", pointsize = 12,
bg = "transparent", res = NA, family = "", restoreConsole = TRUE,
type = "windows")
ggplot()+
geom_polygon(data=get.poly(breaks[1],breaks[2]),aes(x,y),fill=colors[1])+
geom_polygon(data=get.poly(breaks[2],breaks[3]),aes(x,y),fill=colors[2])+
geom_polygon(data=get.poly(breaks[3],breaks[4]),aes(x,y),fill=colors[3])+
geom_polygon(data=get.poly(pos-1,pos+1,0.2),aes(x,y))+
geom_text(data=as.data.frame(breaks), size=5, fontface="bold", vjust=0,
aes(x=1.1*cos(pi*(1-breaks/100)),y=1.1*sin(pi*(1-breaks/100)),label=paste0(breaks,"%")))+
annotate("text",x=0,y=0,label=pos,vjust=0,size=8,fontface="bold")+
coord_fixed()+
theme_bw()+
theme(axis.text=element_blank(),
axis.title=element_blank(),
axis.ticks=element_blank(),
panel.grid=element_blank(),
panel.border=element_blank())
dev.off()
}
我认为问题可能在创建图表的方式上,但我看不到发生了什么。 有什么建议吗?
感谢
PS:很抱歉的过长...消息:)
您是否尝试过使用'ggsave()'? –
您需要在您的函数中放置'print(ggplot())'(保留所有内容,只需将print()放在完整的ggplot函数中即可。 –