刷新plot_grid中的png设备(使用cowplot)

时间:2018-05-20 07:46:16

标签: r ggplot2 cowplot

这是我的问题:

我做:

plot_grid(first_graph_by_mistake)
plot_grid(second_graph_on_purpuse)
ggsave("graph1.png")

vs ONLY

plot_grid(second_graph_on_purpose)
ggsave("graph2.png")

两张图看起来都一样,但是当我这样做时:

system("diff graph1.png graph2.png") it shows a difference.

也许png设备没有刷新,一些设置不同,这就是diff显示差异的原因。如何使2个图表完全相同?那是我的主要问题。

我在一段很长的代码中完成了上述操作。当我尝试制作一个代表例子时,差异并没有显示出两个图形之间的任何差异。我无法重现我在查询中提到的内容。

这是代表:

library(cowplot)
library(grid)

plot.mpg.1 <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl)))   + geom_point(size=2.5)
plot.mpg.2 <- ggplot(mpg, aes(x = cty, y = displ, colour = factor(cyl))) + geom_point(size  = 2.5)
plot.mpg.3 <- ggplot(mpg, aes(x = hwy, y = displ, colour = factor(cyl))) + geom_point(size  = 2.5)
plot.mpg.4 <- ggplot(mpg, aes(x = drv, y = displ, colour = factor(cyl))) + geom_point(size  = 2.5)

mygraphs <- list(plot.mpg.1,plot.mpg.2,plot.mpg.3,plot.mpg.4)
dummygraph <- mygraphs[[1]]    
legend = get_legend(dummygraph + theme(legend.position = "bottom",legend.justification="center") + guides(fill = guide_legend(nrow =  1 )))

toplotlist <- lapply(mygraphs,function(x){x + theme(plot.margin = unit(c(0, 0, 0,0), "in"),legend.position="none")})

pmatrix  <-  do.call("plot_grid",toplotlist)
p<-plot_grid(pmatrix,legend,nrow=2,rel_heights = c(8,.2),rel_widths = c(10,1))

# Note : Please run first, the first section. Then run the second section. 


######################################################################################################
# Without this line
#   plot_grid(pmatrix,legend,nrow=2,rel_heights = c(64,1))

title <- ggdraw() + draw_label("My title", fontface='bold',size = 20)
semifinal <-  plot_grid(title, p, ncol=1, rel_heights=c(0.1, 1))
blank <- grid.rect(gp=gpar(col="white"))
plot_grid(semifinal,blank,ncol=1,rel_heights=c(15,1))
ggsave(paste0("without_line.png"),height = 10,width = 10,dpi = 600)             
   ########################################################################### ############################
# With this line  
plot_grid(pmatrix,legend,nrow=2,rel_heights = c(64,1))

title <- ggdraw() + draw_label("My title", fontface='bold',size = 20)
semifinal <-  plot_grid(title, p, ncol=1, rel_heights=c(0.1, 1))
blank <- grid.rect(gp=gpar(col="white"))
plot_grid(semifinal,blank,ncol=1,rel_heights=c(15,1))
ggsave(paste0("with_line.png"),height = 10,width = 10,dpi = 600)             

 ###################################################################################################

# Now do
system("diff without_line.png with_line.png")

0 个答案:

没有答案