我在尝试保存图表时遇到问题。 我正在使用一个函数来绘制我从中获取的量规图: How to draw gauge chart in 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)时,一切正常。 file 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以包含de 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:对于loooong消息抱歉:)
答案 0 :(得分:0)
您需要run_gg()
ggplot对象。
也就是说,在
gg.gauge(pos)
函数内,替换:
print(gg.gauge(pos))
与
{{1}}