0

这是我的问题:

我愿意 :

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

与仅

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

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

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

也许 png 设备没有刷新并且某些设置不同,这就是 diff 显示不同的原因。我怎样才能使两张图完全相同?那是我的主要查询。

我在很长的一段代码中完成了上述操作。当我尝试制作一个 reprex 示例时,差异没有显示 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")
4

0 回答 0