简体   繁体   中英

An issue with tiff and ggplot R functions inside for loop

损坏的图像[![] [1] ] 2

I was not able to get a batch of images with tiff() R function inside for loop.

I have x86_64-w64-mingw32/x64 (64-bit) platform with installed Windows 8.1 and RStudio Version 1.0.143 with R version 3.6.0 (2019-04-26) -- "Planting of a Tree" . I tried to obtaine a batch of tiff images by putting my code inside the for loop. Unfortunately that resulted in the numerous broken files (in attachment). But if i type exactly the same code in the RStudio console i got normal (valid) image.

#Fragment of the script
    for (i in 1:nrow(BH))
    {
    ...
    tiff( paste(pos$rs[1],"tiff",sep = '.'))
    ggplot(df_g_,aes(x=factor(g),fill=factor(O)))+geom_bar(stat="count")+xlab("")+labs(fill='')
    dev.off()
    }

#If i perform the following code in console after script execution i got a valid image

    tiff( paste(pos$rs[1],"tiff",sep = '.'))
    ggplot(df_g_,aes(x=factor(g),fill=factor(O)))+geom_bar(stat="count")+xlab("")+labs(fill='')
    dev.off()

The solution was to envelop ggplot function by base print function in the loop:

print(ggplot(df_g_,aes(x=factor(g),fill=factor(O)))+geom_bar(stat="count")+xlab("")+labs(fill=''))

So, i've used the solution from the other post: enter link description here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM