R语言 如何使用`ggsave`将绘图保存为全屏?

gjmwrych  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(213)
df_rate_flat <- structure(list(pair = c("0841_0851_Mandaic", "0843_0850_Mandaic", "0846_0855_Mandaic", "0849_0856_Mandaic", "084A_084D_Mandaic", "0853_0857_Mandaic", "10332_10338_Gothic", "10334_10340_Gothic", "10336_10348_Gothic", "1033A_10342_Gothic", "1033B_1034A_Gothic", "10345_10347_Gothic", "10380_10397_Ugaritic", "10381_1038B_Ugaritic", "10382_1039C_Ugaritic", "10386_10398_Ugaritic", "10388_1039D_Ugaritic", "10389_1038C_Ugaritic", "109A5_109AA_MeroiticCursive", "109A9_109B2_MeroiticCursive", "109AC_109B3_MeroiticCursive", "109AD_109AE_MeroiticCursive", "109AF_109B5_MeroiticCursive", "109B0_109B4_MeroiticCursive", "110A_1164_Hangul", "110B_116B_Hangul", "1112_1175_Hangul", "1169_116E_Hangul", "116A_116F_Hangul", "116D_1172_Hangul", "1683_168B_Ogham", "1685_168C_Ogham", "1687_168D_Ogham", "1689_168A_Ogham", "168E_168F_Ogham", "1695_1696_Ogham", "16AD1_16AE7_BassaVah", "16ADA_16AE4_BassaVah", "16ADC_16AE9_BassaVah", "16ADD_16ADE_BassaVah", "16ADF_16AE8_BassaVah", "16AE1_16AE2_BassaVah", "1761_1762_Tagbanwa", "1763_176C_Tagbanwa", "1764_1768_Tagbanwa", "1765_1770_Tagbanwa", "1769_176E_Tagbanwa", "176A_176F_Tagbanwa", "1A05_1A0A_Buginese", "1A07_1A12_Buginese", "1A08_1A13_Buginese", "1A0C_1A0F_Buginese", "1A10_1A15_Buginese", "1A14_1A16_Buginese", "A805_A807_SylotiNagri", "A810_A812_SylotiNagri", "A811_A813_SylotiNagri", "A814_A822_SylotiNagri", "A818_A81C_SylotiNagri", "A819_A81B_SylotiNagri"), Script = c("Mandaic", "Mandaic", "Mandaic", "Mandaic", "Mandaic", "Mandaic", "Gothic", "Gothic", "Gothic", "Gothic", "Gothic", "Gothic", "Ugaritic", "Ugaritic", "Ugaritic", "Ugaritic", "Ugaritic", "Ugaritic", "MeroiticCursive", "MeroiticCursive", "MeroiticCursive", "MeroiticCursive", "MeroiticCursive", "MeroiticCursive", "Hangul", "Hangul", "Hangul", "Hangul", "Hangul", "Hangul", "Ogham", "Ogham", "Ogham", "Ogham", "Ogham", "Ogham", "BassaVah", "BassaVah", "BassaVah", "BassaVah", "BassaVah", "BassaVah", "Tagbanwa", "Tagbanwa", "Tagbanwa", "Tagbanwa", "Tagbanwa", "Tagbanwa", "Buginese", "Buginese", "Buginese", "Buginese", "Buginese", "Buginese", "SylotiNagri", "SylotiNagri", "SylotiNagri", "SylotiNagri", "SylotiNagri", "SylotiNagri"),     average.rate = c(6.54444444444444, 6.5, 5.80555555555556,     3.44444444444444, 3.49444444444444, 5.78888888888889, 6.75,     6.65555555555556, 6.93333333333333, 3.53888888888889, 6.32777777777778,     5.58333333333333, 4.08888888888889, 4.77222222222222, 5.24444444444444,     6.37777777777778, 5.83888888888889, 5.03333333333333, 6.36666666666667,     3.52777777777778, 6.36666666666667, 6.37222222222222, 6.81111111111111,     6.44444444444444, 6.72222222222222, 6.92222222222222, 6.76111111111111,     2.35555555555556, 2.99444444444444, 2.4, 6.38333333333333,     5.97777777777778, 5.71111111111111, 2.14444444444444, 2.05,     6.1, 2.97777777777778, 6.43888888888889, 3.71111111111111,     6.22222222222222, 6.73333333333333, 6.78333333333333, 6.51666666666667,     6.71111111111111, 3.03888888888889, 6.31666666666667, 2.71111111111111,     5.6, 6.31666666666667, 5.91111111111111, 3.66666666666667,     5.68333333333333, 2.33888888888889, 3.16666666666667, 6.11111111111111,     5.41111111111111, 2.11666666666667, 4.91111111111111, 2.00555555555556,     5.50555555555556), average.hamming = c(0.73, 0.87, 0.13,     0.13, 0.13, 0.73, 0.77, 0.69, 0.69, 0.15, 0.15, 0.15, 0.17,     0.56, 0.56, 0.61, 0.17, 0.11, 0.13, 0.13, 0.63, 0.13, 0.75,     0.63, 0.76, 0.76, 0.71, 0.1, 0.1, 0.1, 0.69, 0.69, 0.77,     0.15, 0.15, 0.15, 0.14, 0.76, 0.14, 0.14, 0.81, 0.81, 0.78,     0.78, 0.22, 0.89, 0.22, 0.22, 0.75, 0.83, 0.17, 0.75, 0.25,     0.25, 0.7, 0.09, 0.04, 0.65, 0.09, 0.7)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -60L), groups = structure(list(    pair = c("0841_0851_Mandaic", "0843_0850_Mandaic", "0846_0855_Mandaic",     "0849_0856_Mandaic", "084A_084D_Mandaic", "0853_0857_Mandaic",     "10332_10338_Gothic", "10334_10340_Gothic", "10336_10348_Gothic",     "1033A_10342_Gothic", "1033B_1034A_Gothic", "10345_10347_Gothic",     "10380_10397_Ugaritic", "10381_1038B_Ugaritic", "10382_1039C_Ugaritic",     "10386_10398_Ugaritic", "10388_1039D_Ugaritic", "10389_1038C_Ugaritic",     "109A5_109AA_MeroiticCursive", "109A9_109B2_MeroiticCursive",     "109AC_109B3_MeroiticCursive", "109AD_109AE_MeroiticCursive",     "109AF_109B5_MeroiticCursive", "109B0_109B4_MeroiticCursive",     "110A_1164_Hangul", "110B_116B_Hangul", "1112_1175_Hangul",     "1169_116E_Hangul", "116A_116F_Hangul", "116D_1172_Hangul",     "1683_168B_Ogham", "1685_168C_Ogham", "1687_168D_Ogham",     "1689_168A_Ogham", "168E_168F_Ogham", "1695_1696_Ogham",     "16AD1_16AE7_BassaVah", "16ADA_16AE4_BassaVah", "16ADC_16AE9_BassaVah",     "16ADD_16ADE_BassaVah", "16ADF_16AE8_BassaVah", "16AE1_16AE2_BassaVah",     "1761_1762_Tagbanwa", "1763_176C_Tagbanwa", "1764_1768_Tagbanwa",     "1765_1770_Tagbanwa", "1769_176E_Tagbanwa", "176A_176F_Tagbanwa",     "1A05_1A0A_Buginese", "1A07_1A12_Buginese", "1A08_1A13_Buginese",     "1A0C_1A0F_Buginese", "1A10_1A15_Buginese", "1A14_1A16_Buginese",     "A805_A807_SylotiNagri", "A810_A812_SylotiNagri", "A811_A813_SylotiNagri",     "A814_A822_SylotiNagri", "A818_A81C_SylotiNagri", "A819_A81B_SylotiNagri"    ), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,         9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,         20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L,         31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,         42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L,         53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L), ptype = integer(0), class = c("vctrs_list_of",     "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -60L), .drop = TRUE))

library(tidyverse)
library(ggrepel)

for (s in script <- unique(df_rate_flat$Script))  {
  p <-  df_rate_flat %>% filter(Script == s) %>% 
    ggplot(aes(x = average.rate, y = average.hamming, color = Script)) +
    geom_point(size = 3) +
    geom_text_repel(aes(label = pair), nudge_x = 0.2, nudge_y = 0.2, size = 6) +
    theme_bw() +
    theme(legend.position = "none", 
          plot.background = element_blank(),
          plot.title = element_text(hjust = 0.5),
          axis.text = element_text(color = "black", size = 14),
          axis.line = element_line(color = "black")
    ) +
    ggtitle(s)
  print(p)
  ggsave(paste0("Graphiques/plot_point_", s, ".png"),
         plot = p,
         width = 4.385417, height = 6.083333,
         device = "png")
}
    • 我得到的**

      我想要的
dbf7pr2w

dbf7pr2w1#

只需先调用x11()来创建一个绘图窗口,将其缩放到正确的大小,例如全屏&然后使用dev.size()来获得widthheight的大小,或者在导出时使用widthheight的好值来使其看起来不错(全屏可能不是一个好主意,因为全屏取决于人们的系统)。就像我对你的问题的其他回答一样,我使用了我的导出包,因为这样你也可以轻松地导出到其他格式(例如:Powerpoint或pdfsvg,所以矢量格式,比位图png质量更好),它也适用于基本R或点阵图。
负载数据:

df_rate_flat <- structure(
  list(
    pair = c(
      "0841_0851_Mandaic",
      "0843_0850_Mandaic",
      "0846_0855_Mandaic",
      "0849_0856_Mandaic",
      "084A_084D_Mandaic",
      "0853_0857_Mandaic",
      "10332_10338_Gothic",
      "10334_10340_Gothic",
      "10336_10348_Gothic",
      "1033A_10342_Gothic",
      "1033B_1034A_Gothic",
      "10345_10347_Gothic",
      "10380_10397_Ugaritic",
      "10381_1038B_Ugaritic",
      "10382_1039C_Ugaritic",
      "10386_10398_Ugaritic",
      "10388_1039D_Ugaritic",
      "10389_1038C_Ugaritic",
      "109A5_109AA_MeroiticCursive",
      "109A9_109B2_MeroiticCursive",
      "109AC_109B3_MeroiticCursive",
      "109AD_109AE_MeroiticCursive",
      "109AF_109B5_MeroiticCursive",
      "109B0_109B4_MeroiticCursive",
      "110A_1164_Hangul",
      "110B_116B_Hangul",
      "1112_1175_Hangul",
      "1169_116E_Hangul",
      "116A_116F_Hangul",
      "116D_1172_Hangul",
      "1683_168B_Ogham",
      "1685_168C_Ogham",
      "1687_168D_Ogham",
      "1689_168A_Ogham",
      "168E_168F_Ogham",
      "1695_1696_Ogham",
      "16AD1_16AE7_BassaVah",
      "16ADA_16AE4_BassaVah",
      "16ADC_16AE9_BassaVah",
      "16ADD_16ADE_BassaVah",
      "16ADF_16AE8_BassaVah",
      "16AE1_16AE2_BassaVah",
      "1761_1762_Tagbanwa",
      "1763_176C_Tagbanwa",
      "1764_1768_Tagbanwa",
      "1765_1770_Tagbanwa",
      "1769_176E_Tagbanwa",
      "176A_176F_Tagbanwa",
      "1A05_1A0A_Buginese",
      "1A07_1A12_Buginese",
      "1A08_1A13_Buginese",
      "1A0C_1A0F_Buginese",
      "1A10_1A15_Buginese",
      "1A14_1A16_Buginese",
      "A805_A807_SylotiNagri",
      "A810_A812_SylotiNagri",
      "A811_A813_SylotiNagri",
      "A814_A822_SylotiNagri",
      "A818_A81C_SylotiNagri",
      "A819_A81B_SylotiNagri"
    ),
    Script = c(
      "Mandaic",
      "Mandaic",
      "Mandaic",
      "Mandaic",
      "Mandaic",
      "Mandaic",
      "Gothic",
      "Gothic",
      "Gothic",
      "Gothic",
      "Gothic",
      "Gothic",
      "Ugaritic",
      "Ugaritic",
      "Ugaritic",
      "Ugaritic",
      "Ugaritic",
      "Ugaritic",
      "MeroiticCursive",
      "MeroiticCursive",
      "MeroiticCursive",
      "MeroiticCursive",
      "MeroiticCursive",
      "MeroiticCursive",
      "Hangul",
      "Hangul",
      "Hangul",
      "Hangul",
      "Hangul",
      "Hangul",
      "Ogham",
      "Ogham",
      "Ogham",
      "Ogham",
      "Ogham",
      "Ogham",
      "BassaVah",
      "BassaVah",
      "BassaVah",
      "BassaVah",
      "BassaVah",
      "BassaVah",
      "Tagbanwa",
      "Tagbanwa",
      "Tagbanwa",
      "Tagbanwa",
      "Tagbanwa",
      "Tagbanwa",
      "Buginese",
      "Buginese",
      "Buginese",
      "Buginese",
      "Buginese",
      "Buginese",
      "SylotiNagri",
      "SylotiNagri",
      "SylotiNagri",
      "SylotiNagri",
      "SylotiNagri",
      "SylotiNagri"
    ),
    average.rate = c(
      6.54444444444444,
      6.5,
      5.80555555555556,
      3.44444444444444,
      3.49444444444444,
      5.78888888888889,
      6.75,
      6.65555555555556,
      6.93333333333333,
      3.53888888888889,
      6.32777777777778,
      5.58333333333333,
      4.08888888888889,
      4.77222222222222,
      5.24444444444444,
      6.37777777777778,
      5.83888888888889,
      5.03333333333333,
      6.36666666666667,
      3.52777777777778,
      6.36666666666667,
      6.37222222222222,
      6.81111111111111,
      6.44444444444444,
      6.72222222222222,
      6.92222222222222,
      6.76111111111111,
      2.35555555555556,
      2.99444444444444,
      2.4,
      6.38333333333333,
      5.97777777777778,
      5.71111111111111,
      2.14444444444444,
      2.05,
      6.1,
      2.97777777777778,
      6.43888888888889,
      3.71111111111111,
      6.22222222222222,
      6.73333333333333,
      6.78333333333333,
      6.51666666666667,
      6.71111111111111,
      3.03888888888889,
      6.31666666666667,
      2.71111111111111,
      5.6,
      6.31666666666667,
      5.91111111111111,
      3.66666666666667,
      5.68333333333333,
      2.33888888888889,
      3.16666666666667,
      6.11111111111111,
      5.41111111111111,
      2.11666666666667,
      4.91111111111111,
      2.00555555555556,
      5.50555555555556
    ),
    average.hamming = c(
      0.73,
      0.87,
      0.13,
      0.13,
      0.13,
      0.73,
      0.77,
      0.69,
      0.69,
      0.15,
      0.15,
      0.15,
      0.17,
      0.56,
      0.56,
      0.61,
      0.17,
      0.11,
      0.13,
      0.13,
      0.63,
      0.13,
      0.75,
      0.63,
      0.76,
      0.76,
      0.71,
      0.1,
      0.1,
      0.1,
      0.69,
      0.69,
      0.77,
      0.15,
      0.15,
      0.15,
      0.14,
      0.76,
      0.14,
      0.14,
      0.81,
      0.81,
      0.78,
      0.78,
      0.22,
      0.89,
      0.22,
      0.22,
      0.75,
      0.83,
      0.17,
      0.75,
      0.25,
      0.25,
      0.7,
      0.09,
      0.04,
      0.65,
      0.09,
      0.7
    )
  ),
  class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
  row.names = c(NA,-60L),
  groups = structure(
    list(
      pair = c(
        "0841_0851_Mandaic",
        "0843_0850_Mandaic",
        "0846_0855_Mandaic",
        "0849_0856_Mandaic",
        "084A_084D_Mandaic",
        "0853_0857_Mandaic",
        "10332_10338_Gothic",
        "10334_10340_Gothic",
        "10336_10348_Gothic",
        "1033A_10342_Gothic",
        "1033B_1034A_Gothic",
        "10345_10347_Gothic",
        "10380_10397_Ugaritic",
        "10381_1038B_Ugaritic",
        "10382_1039C_Ugaritic",
        "10386_10398_Ugaritic",
        "10388_1039D_Ugaritic",
        "10389_1038C_Ugaritic",
        "109A5_109AA_MeroiticCursive",
        "109A9_109B2_MeroiticCursive",
        "109AC_109B3_MeroiticCursive",
        "109AD_109AE_MeroiticCursive",
        "109AF_109B5_MeroiticCursive",
        "109B0_109B4_MeroiticCursive",
        "110A_1164_Hangul",
        "110B_116B_Hangul",
        "1112_1175_Hangul",
        "1169_116E_Hangul",
        "116A_116F_Hangul",
        "116D_1172_Hangul",
        "1683_168B_Ogham",
        "1685_168C_Ogham",
        "1687_168D_Ogham",
        "1689_168A_Ogham",
        "168E_168F_Ogham",
        "1695_1696_Ogham",
        "16AD1_16AE7_BassaVah",
        "16ADA_16AE4_BassaVah",
        "16ADC_16AE9_BassaVah",
        "16ADD_16ADE_BassaVah",
        "16ADF_16AE8_BassaVah",
        "16AE1_16AE2_BassaVah",
        "1761_1762_Tagbanwa",
        "1763_176C_Tagbanwa",
        "1764_1768_Tagbanwa",
        "1765_1770_Tagbanwa",
        "1769_176E_Tagbanwa",
        "176A_176F_Tagbanwa",
        "1A05_1A0A_Buginese",
        "1A07_1A12_Buginese",
        "1A08_1A13_Buginese",
        "1A0C_1A0F_Buginese",
        "1A10_1A15_Buginese",
        "1A14_1A16_Buginese",
        "A805_A807_SylotiNagri",
        "A810_A812_SylotiNagri",
        "A811_A813_SylotiNagri",
        "A814_A822_SylotiNagri",
        "A818_A81C_SylotiNagri",
        "A819_A81B_SylotiNagri"
      ),
      .rows = structure(
        list(
          1L,
          2L,
          3L,
          4L,
          5L,
          6L,
          7L,
          8L,
          9L,
          10L,
          11L,
          12L,
          13L,
          14L,
          15L,
          16L,
          17L,
          18L,
          19L,
          20L,
          21L,
          22L,
          23L,
          24L,
          25L,
          26L,
          27L,
          28L,
          29L,
          30L,
          31L,
          32L,
          33L,
          34L,
          35L,
          36L,
          37L,
          38L,
          39L,
          40L,
          41L,
          42L,
          43L,
          44L,
          45L,
          46L,
          47L,
          48L,
          49L,
          50L,
          51L,
          52L,
          53L,
          54L,
          55L,
          56L,
          57L,
          58L,
          59L,
          60L
        ),
        ptype = integer(0),
        class = c("vctrs_list_of",     "vctrs_vctr", "list")
      )
    ),
    class = c("tbl_df", "tbl", "data.frame"),
    row.names = c(NA,-60L),
    .drop = TRUE))

生成绘图并导出:

library(tidyverse)
library(ggrepel)
library(export)

x11() # open graphics window
# now maximize this window so it is full screen

for (s in script <- unique(df_rate_flat$Script))  {
  p <-  df_rate_flat %>% filter(Script == s) %>% 
    ggplot(aes(x = average.rate, y = average.hamming, color = Script)) +
    geom_point(size = 3) +
    geom_text_repel(aes(label = pair), nudge_x = 0.2, nudge_y = 0.2, size = 6) +
    theme_bw() +
    theme(legend.position = "none", 
          plot.background = element_blank(),
          plot.title = element_text(hjust = 0.5),
          axis.text = element_text(color = "black", size = 14),
          axis.line = element_line(color = "black")
    ) +
    ggtitle(s)
  print(p)
  graph2png(file=paste0("Graphiques/plot_point_", s, ".png"),
         width = dev.size(units="px")[[1]]/90, # for me fullscreen is 17.06667
         height = dev.size(units="px")[[2]]/90, # for me fullscreen is 8.588889
         dpi = 900)
  # or graph2pdf, graph2ppt, graph2svg to export to other formats
  dev.off() # close window 
}

相关问题