0

I’m having a problem when trying to save a graph. I’m using a function to graph a gauge graph that I have taken from: How to draw gauge chart in R?

The function works fine. When I run it the graph is created as a pop up.

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()) 

}

When I execute this function (test.R), everything goes ok. The file image.png contains the graph and is located on the proper place.

  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()

}

The problem occurs when I run this function, run_gg.R. It returns a file with an empty image.

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()
}

I modify the function gg.gauge.R to include de png and dev.off (gg.gauge2.R), but the result is the same.

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()
}

I think that the problem may be on the way that the graph is created, but I cannot see what is happening. Any suggestion?

Thanks

PS: Sorry for the loooong message :)

Community
  • 1
  • 1
Carlos
  • 5
  • 2

1 Answers1

0

You need to print() the ggplot object.

That is, inside the run_gg() function, replace:

gg.gauge(pos)

with

print(gg.gauge(pos))

mrbrich
  • 853
  • 1
  • 8
  • 9
  • Thanks for the help, but I still don’t understand why the function gg.gauge2 doesn’t work. Obviously I need much more knowledge on R (and probably also informatics) – Carlos Jul 28 '16 at 14:05
  • The reason is that a `ggplot` object doesn't actually plot anything until it gets printed. This happens automatically if you type the object name in the interactive prompt, but not inside a function. – mrbrich Jul 29 '16 at 15:52