Cowplot plot_grid:导出地块时会裁剪掉一部分子图

问题描述

保存时裁剪出部分图形

我试图合并两个单独的图:第一个是具有两个不相关的y轴的折线图,并且是使用基本R plot()命令构建的(我读到ggplot2不支持不相关的辅助y轴:我对么?)。第二张图再次是一个线图,这次只有一个y轴。为了内部一致性,此图也是使用基本R plot()命令构建的。

为了合并两个图,我使用recordplot()将它们保存为两个单独的对象,并存储它们以使用plot_grid()将其与参数``ncol = 1英寸。分别取的两个图看起来(几乎)如我所愿。当我放大绘图设备或尝试保存组合图形时,将裁剪出底部轴和上部图形的一部分。

帖子末尾使用dput()报告可复制示例的数据。

代码和系统信息

Sys.info()
          sysname           release           version          nodename           machine             login 
        "Windows"          "10 x64"     "build 18362" "DESKTOP-7D7TTKL"          "x86-64"         "Filippo" 
             user    effective_user 
        "Filippo"         "Filippo" 


# Load Libraries

library(tidyverse)
library(ggnewscale)
library(janitor)
library(delabj)  
library(extrafont)
library(extrafontdb)
library(cowplot)


# DATA loaded from dput() output

一个情节看起来不错

Upper Plot

# ----------
# First Plot
# ----------

plot.new() ## clean up device

par(mar = c(2,4,2,4),mfrow=c(1,1))
plot(x = range(data$date[data$reg == 'ECA']),y = range(data$sector_any_food_l[data$sub_reg == 'ECA: W. Eur. and Balcans']),type ="n",ylab = "Number of Measures",#main = "Western Europe and Balcans",sub = "Food",xlab = "",#col = "blue",bty = "n",xaxs="i",yaxs="i",xaxt = "n",cex.axis = 0.9,cex.lab = 0.8,cex.main = 0.9,fg = 'grey')
lines(x = data$date[data$sub_reg == 'ECA: W. Eur. and Balcans'],y = data$sector_any_food_l[data$sub_reg == 'ECA: W. Eur. and Balcans'],col = "darkgreen")
lines(x = data$date[dato_reg$sub_reg == 'ECA: W. Eur. and Balcans'],y = data$sector_any_food_r[data$sub_reg == 'ECA: W. Eur. and Balcans'],col = "maroon")

# Secondary axis
par(new = TRUE,mar = c(2,1))
plot(x = data$date[data$sub_reg == 'ECA: W. Eur. and Balcans'],y = data$Tot_cov[data$sub_reg == 'ECA: W. Eur. and Balcans'],type = "l",yaxt = "n",# Set null superposition of axis on LHS
     ylab = "",col = "black",lty = 2,lwd = 1.5,fg = 'grey')

markers <- c(0,400000,800000,1200000)

axis(side = 4,at = markers,labels=format(markers,scientific=FALSE),fg = 'grey',cex.lab = 0.8)
mtext("Total Number of Covid Cases",side = 4,line = 3,srt = -180,cex = 0.8)

legend("topleft",c("# Policies - Lib","# Policies - Res","Total Cases"),col = c("darkgreen","maroon",'black'),lty = c(1,1,2),cex = 0.7)

p <- recordplot()

第二个情节看起来也不错:

Bottom Image

# -----------
# Second plot
# -----------

plot.new() ## clean up device

par(mar = c(2,4))
plot(x = data$date[data$sub_reg == 'ECA: W. Eur. and Balcans'],y = data$delta_cases_week_sh[data$sub_reg == 'ECA: W. Eur. and Balcans'],#main = 'Weekly variation of new cases',type = 'l',col = 'darkgrey',cex = 0.5,#xaxt = "n",lty = 5,lwd = 1.2,fg = 'grey')

g <- recordplot()

当我尝试将两个图合并并导出为单列plot_grid()输出时,就会出现问题

# ---------------
# Combining Plots
# ---------------

h <- plot_grid(p,g,#label_fontfamily = "Open Sans",#align = 'h',ncol = 1,labels = '',hjust = 0,vjust = 0,rel_heights = c(1,0.5)
          )

save_plot(filename = file.path(countryplots,"z_combined.png"),plot = h,device = "png")

在这种情况下,组合图在RStudio的绘图设备中的外观与ggsave() / save_plot()输出有所不同,如下面的两个图片所示

Plot as it looks from the device

Plot as it looks when exported

摘要

我猜我的问题可能与定义边距的方式有关,但是我无法解决此问题。我尝试使用par()plot_grid()参数,但是我无法解决在RStudio中看到的内容与保存图形时得到的内容间的差异。您对发生的事情有任何想法吗?问题的一部分可能与我建立图表的方式有关吗?我更喜欢使用ggplot()而不是plot()(我认为这样可以更容易地合并绘图),但是我找不到包含不相关的辅助y轴的方法。有什么解决方法吗?

数据

data <- structure(list(date = structure(c(18263,18264,18265,18266,18267,18268,18269,18270,18271,18272,18273,18274,18275,18276,18277,18278,18279,18280,18281,18282,18283,18284,18285,18286,18287,18288,18289,18290,18291,18292,18293,18294,18295,18296,18297,18298,18299,18300,18301,18302,18303,18304,18305,18306,18307,18308,18309,18310,18311,18312,18313,18314,18315,18316,18317,18318,18319,18320,18321,18322,18323,18324,18325,18326,18327,18328,18329,18330,18331,18332,18333,18334,18335,18336,18337,18338,18339,18340,18341,18342,18343,18344,18345,18346,18347,18348,18349,18350,18351,18352,18353,18354,18355,18356,18357,18358,18359,18360,18361,18362,18363,18364,18365,18366,18367,18368,18369,18370,18371,18372,18373,18374,18375,18376,18377,18378,18379,18380,18381,18382,18383,18384,18385,18386,18387,18388,18389,18390,18391,18392,18393,18394,18395,18396,18397,18398,18399,18400,18401,18402,18403,18404,18405,18406,18407,18408,18409,18410,18411,18412,18413,18414,18415,18416,18417,18418,18419,18420,18421,18422,18423,18424,18425,18426,18427,18428,18429,18430,18431,18432,18433,18434,18435,18436,18437,18438,18439,18440,18441,18442,18443,18444,18445,18446,18447,18448,18449,18450,18451,18452,18453,18454,18455,18456,18457,18458),class = "Date",format.stata = "%td"),reg = c("ECA","ECA","ECA"
    ),sub_reg = c("ECA: W. Eur. and Balcans","ECA: W. Eur. and Balcans","ECA: W. Eur. and Balcans"),sector_any_food_l = c(0,3,5,sector_any_food_r = c(0,1),sector_any_med_l = c(0,7,9,8,10,11,11),sector_any_med_r = c(0,6,12,13,14,15,18,19,24,25,26,28,30,31,32,34,35,36,38,40,41,42,44,45,46,43,39,42
    ),Tot_cov = c(0,20,29,50,52,53,57,64,66,77,78,80,101,145,245,324,435,593,899,1179,1526,2267,2855,3499,4528,5957,7667,9682,12008,14502,17207,21948,25650,33781,40850,48336,57182,65965,78327,92443,109238,104109,119291,133670,150189,166726,186787,207880,229033,247545,268153,292001,317213,340228,366347,391125,408620,431075,455340,480191,506435,531235,553995,597601,616404,636598,660851,694167,716686,735861,756772,774963,796650,814188,835028,855914,872891,886168,902710,921151,934798,954629,968834,981640,991798,1002663,1016059,1038908,1053239,1067231,1079238,1087868,1097826,1111055,1122862,1135539,1148532,1152011,1160828,1168828,1178849,1189528,1199234,1215186,1222710,1228846,1235014,1242656,1251069,1263659,1271301,1280131,1285518,1289965,1006491,1011167,1015849,1021596,1030570,1034475,1038257,1043582,1048289,1053243,1058091,1063284,1067521,1068086,1073092,1077711,1082519,1087033,1090777,1094066,1097409,1101924,1107192,1111933,1116564,1121333,1124695,1127855,1132052,1135751,1139260,1143411,1147672,1151121,1153914,1157815,1163128,1167502,1172161,1177010,1181115,1184497,1189332,1194167),delta_cases_week_sh = c(NA,NA,6.15,1.02097902097902,0.294117647058824,2.25133689839572,9.35608552631579,4.91439688715953,3.61151987110634,2.18097278915526,1.30624242601752,0.658109956784521,0.363328034068481,0.24245404589266,0.133501787648649,0.0834565412298084,0.0625291317023914,0.0449483257607112,0.0348509652307993,0.0290414713215884,0.0250526276453955,0.0240567914961615,0.0247184338499397,0.0240095369396734,0.0231936536897942,0.0233031474558156,-0.709567845919194)),row.names = c(NA,-196L),class = c("tbl_df","tbl","data.frame"))

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)