3

) and dev.off() to write plots into files. The code is like this

allWeeks = data.frame(weekInYear = 1:53,
                  user2013 = sample(1:100, 53),
                  user2014 = sample(1:100, 53),
                  user2015 = sample(1:100, 53),
                  job2013 = sample(1:100, 53),
                  job2014 = sample(1:100, 53),
                  job2015 = sample(1:100, 53))

allWeeks[30:53, c('user2015', 'job2015')] = NA


melted = melt(allWeeks[ , c('weekInYear', 'user2015', 'job2015')], id = 'weekInYear')
pic1 = 'output/allWeek2015.png'
png(width = 600, height = 300, file = pic1)
ggplot(melted, aes(weekInYear, value, colour = variable)) +
        geom_line()
dev.off()

melted = melt(allWeeks[ , c('weekInYear', 'user2013', 'user2014', 'user2015')], id = 'weekInYear')
pic2 = 'output/allWeekUser.png'
png(width = 600, height = 300, file = pic2)
ggplot(melted, aes(weekInYear, value, colour = variable)) +
        geom_line()
dev.off()

melted = melt(allWeeks[ , c('weekInYear', 'job2013', 'job2014', 'job2015')], id = 'weekInYear')
pic3 = 'output/allWeekJob.png'
png(width = 600, height = 300, file = pic3)
ggplot(melted, aes(weekInYear, value, colour = variable)) +
        geom_line()
dev.off()

When I select all of the code and click run, it works well, with some warning messages about NAs in plotted data.

When I click source, there's no message, and the pictures are blank. Any help's appreciated please...

UPDATE: replaced with ggsave(pic1, width = 3, height = 1.5) and there's no blank issue now. But ggsave() seems to use an unreasonable scale and fontsize... See pictures: LEFT saved with png() and dev.off() vs. RIGHT saved with ggsave(). Did I miss some parameter? Any help's appreciated please

enter image description here

YJZ
  • 3,934
  • 11
  • 43
  • 67
  • that same code and following your steps output 3 files each time for both ways (and the plots are never blank) – hrbrmstr Oct 16 '15 at 01:07
  • 1
    You should use `ggsave()` to save `ggplot2` plots. If you prefer `png`, you need to `print` the output. This might be achieved by storing the plot, i.e. `g <- ggplot(...); print(g)`. –  Oct 16 '15 at 01:09
  • Possible duplicate of [Using png not working when called within a function](http://stackoverflow.com/questions/9206110/using-png-not-working-when-called-within-a-function) –  Oct 16 '15 at 01:11
  • thanks guys I'll check out `ggsave()`. Should work better since it's designed for ggplots – YJZ Oct 16 '15 at 03:49
  • hi @Pascal thanks for help. `ggsave()` seems to use an unreasonable scale and fontsize... Do you know how to fix this? thanks a lot! – YJZ Oct 16 '15 at 04:01
  • Yes, I know. Read the documentation. –  Oct 16 '15 at 04:26
  • hi @Pascal thanks for your help. Sorry I didn't get a chance to fix this earlier - was doing some interviews. yeah I figured it out - should use `dpi` – YJZ Oct 24 '15 at 04:13
  • I also had similar problem. Putting the ggplot object inside print() fixed the issue for me. – Kumar Manglam Feb 12 '16 at 08:37

1 Answers1

0

I just had the same problem. You can try putting the plot as an object first, and then using print() to this new dummy plot-object before dev.off().

    allWeeks = data.frame(weekInYear = 1:53,
                      user2013 = sample(1:100, 53),
                      user2014 = sample(1:100, 53),
                      user2015 = sample(1:100, 53),
                      job2013 = sample(1:100, 53),
                      job2014 = sample(1:100, 53),
                      job2015 = sample(1:100, 53))
    
    allWeeks[30:53, c('user2015', 'job2015')] = NA
    
    
    melted = melt(allWeeks[ , c('weekInYear', 'user2015', 'job2015')], id = 'weekInYear')
    pic1 = 'output/allWeek2015.png'
    png(width = 600, height = 300, file = pic1)
    dummy_plot <- ggplot(melted, aes(weekInYear, value, colour = variable)) +
            geom_line()
    print(dummy_plot)
    dev.off()