0

我正在寻找使用拼凑而成的两个 ggplots。我有这两个情节:

library(patchwork)
library(forecast)
plt1 <- autoplot(df_ts)
plt2 <- ggseasonplot(df_ts, polar = TRUE, continuous = FALSE, year.labels = FALSE)

plt1 + plt2

我想让第plt1一个更长一点,所以我应用以下内容:

plt1 + plt2 + plot_layout(widths = c(3, 1), heights = c(1, 1))

但是,这会在我的地块下方创建一个大的空白区域,当我使用它保存它时,ggsave它仍然有这个空白区域。

当我做类似的事情时:

(plt1 + plt2) / plt1

并保存看起来很棒的文件。

我正在使用以下内容保存文件:

ggsave(file = "mysaveLocation", width = 14, height = 8)

数据:

df_ts <- structure(c(37.7741935483871, 35.2142857142857, 30.5161290322581, 
25.5, 20.2903225806452, 29.1666666666667, 28.2903225806452, 29.741935483871, 
39.6666666666667, 50.5483870967742, 46.4333333333333, 52.8387096774194, 
48.258064516129, 49.3571428571429, 33.4193548387097, 32.5, 29.258064516129, 
28.5666666666667, 24.1612903225806, 28.7096774193548, 39.2, 42.1290322580645, 
40.1666666666667, 44.8387096774194, 50.1612903225806, 40.2857142857143, 
34.9032258064516, 21.8, 26.4193548387097, 21, 21.0891600806671, 
23.5161290322581, 27.6666666666667, 39.1935483870968, 34.6, 43.390938227204, 
37.1290322580645, 31.7857142857143, 24.0967741935484, 22, 20.0967741935484, 
19.2333333333333, 23.258064516129, 21.6451612903226, 31.6666666666667, 
31.8709677419355, 33.7333333333333, 45.3225806451613, 33.6129032258064, 
29.9642857142857, 29.8387096774194, 19.8, 16.6774193548387, 16.6, 
17.8387096774194, 15.0322580645161, 19.4333333333333, 26.9677419354839, 
22.7, 41.0933133487127, 57.9032258064516, 24.1428571428571, 40.4193548387097, 
25.4, 24.5483870967742, 24.6, 24.5161290322581, 19.6774193548387, 
26.981986726703, 34.8387096774194, 50.5091064388085, 59.4838709677419, 
38.9327668165151, 31.4285714285714, 32.0322580645161, 25.8666666666667, 
24.258064516129, 23.2666666666667, 22.0967741935484, 18.0645161290323, 
32.7, 42.9032258064516, 44.0333333333333, 48.4516129032258, 45.9677419354839, 
35.3828321071011, 31.4516129032258, 22.1, 19.7956770110016, 20.2333333333333, 
21.0967741935484, 17.1935483870968, 29.8648288832595, 41.1935483870968, 
53.5, 44.8709677419355, 41.6774193548387, 39.6071428571429, 23.8064516129032, 
24.2, 22.5161290322581, 20.1, 19.5806451612903, 21.0645161290323, 
28.6, 32.4838709677419, 32.9, 44.6451612903226, 45.5806451612903, 
42.0357142857143, 25.8709677419355, 17.3666666666667, 14.3548387096774, 
13.9, 16.1290322580645, 16.3225806451613, 19.4333333333333), .Dim = c(117L, 
1L), .Dimnames = list(NULL, "ave"), index = structure(c(1262304000, 
1264982400, 1267401600, 1270080000, 1272672000, 1275350400, 1277942400, 
1280620800, 1283299200, 1285891200, 1288569600, 1291161600, 1293840000, 
1296518400, 1298937600, 1301616000, 1304208000, 1306886400, 1309478400, 
1312156800, 1314835200, 1317427200, 1320105600, 1322697600, 1325376000, 
1328054400, 1330560000, 1333238400, 1335830400, 1338508800, 1341100800, 
1343779200, 1346457600, 1349049600, 1351728000, 1354320000, 1356998400, 
1359676800, 1362096000, 1364774400, 1367366400, 1370044800, 1372636800, 
1375315200, 1377993600, 1380585600, 1383264000, 1385856000, 1388534400, 
1391212800, 1393632000, 1396310400, 1398902400, 1401580800, 1404172800, 
1406851200, 1409529600, 1412121600, 1414800000, 1417392000, 1420070400, 
1422748800, 1425168000, 1427846400, 1430438400, 1433116800, 1435708800, 
1438387200, 1441065600, 1443657600, 1446336000, 1448928000, 1451606400, 
1454284800, 1456790400, 1459468800, 1462060800, 1464739200, 1467331200, 
1470009600, 1472688000, 1475280000, 1477958400, 1480550400, 1483228800, 
1485907200, 1488326400, 1491004800, 1493596800, 1496275200, 1498867200, 
1501545600, 1504224000, 1506816000, 1509494400, 1512086400, 1514764800, 
1517443200, 1519862400, 1522540800, 1525132800, 1527811200, 1530403200, 
1533081600, 1535760000, 1538352000, 1541030400, 1543622400, 1546300800, 
1548979200, 1551398400, 1554076800, 1556668800, 1559347200, 1561939200, 
1564617600, 1567296000), tzone = "UTC", tclass = "yearmon"), class = "ts", .Tsp = c(2010, 
2019.66666666667, 12))
4

1 回答 1

1

删除似乎可以解决问题heights = c(1, 1)plot_layout

library(ggplot2)
library(patchwork)

p <- ggplot(mtcars, aes(mpg, cyl)) + geom_point()
p + p + plot_layout(widths = c(3, 1))

reprex 包(v0.2.1)于 2020-04-16 创建

然后只需确保在保存时通过调整width和中的height参数来获得所需的纵横比ggsave

于 2020-04-16T20:18:11.140 回答