如何正确调整R中ggplot各个方面的大小,包括图例? [英] How do I properly resize all aspects of a ggplot in R, including the legend?

查看:342
本文介绍了如何正确调整R中ggplot各个方面的大小,包括图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ggplot2中进行散点图绘制,然后使用ggsave导出特定宽度和高度的PDF.但是,图形图例永远不会使用ggsave正确调整大小(其边界不会留在绘图中).还有另一种方法可以同时调整ggplot的所有部分的大小以便于导出吗?我也尝试过使用pdf(),但是会出现相同的问题.

I'm making scatterplots in ggplot2 and then using ggsave to export PDFs of specific widths and heights. However, the figure legend never gets properly resized with ggsave (its borders don't stay within the plot). Is there another way to simultaneously resize all parts of a ggplot for easy exporting? I've also tried using pdf(), but the same problem occurs.

以下是使用鸢尾花的事例:

Here is an example of what's going on using iris:

data(iris)

test <- ggplot(iris, aes(x = iris$Sepal.Length, y = iris$Sepal.Width, 
  colour = iris$Species)) + 
  geom_point(size = .1) +
  theme(panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), panel.border = element_blank(),
    panel.background = element_blank(), 
    axis.line = element_line(colour = "black", size = .8), 
    legend.key=element_blank(),
    axis.ticks = element_line(colour = "black", size = .6), 
    axis.text=element_text(size=6, colour = "black"), 
    plot.title = element_text(hjust = 0.0, size = 6, colour = "black"), 
    axis.title=element_text(size= 6), 
    legend.text=element_text(size = 6),
    legend.title=element_text(size = 6, face = "bold"),
    legend.position = c(.9, .15)) + labs(colour = "Species")

##saving plot with no resizing
ggsave(plot = test, file = "NoResize.pdf", dpi = 600, family = "ArialMT")

##saving plot with resizing results in relatively larger legend
ggsave(plot = test, file = "Resize.pdf", device = "pdf", width = 3.5, 
    height = 3, units = "in", dpi = 600, family = "ArialMT")

图例似乎正在改变,但它似乎比绘图的其他方面要大一些,因此不再适合绘图轴.

The legend seems to be changing, but it seems to get relatively bigger than the other aspects of the plot, such that it no longer fits within the plot axes.

推荐答案

size参数以磅为单位.如果您对默认的7英寸乘7英寸图形具有首选的尺寸,则需要为其他尺寸的图形相应地缩放size.

The size argument is in points. If you have a preferred size for the default 7 inch by 7 inch graphic then you'll need to scale the size accordingly for a different size graphic.

另外两个音符.

  1. ggplot2::aes调用中不要使用$.将来会给您带来麻烦.使用ggplot(iris) + aes(x = Sepal.Length)就足够了.

  1. Don't use $ in the ggplot2::aes call. This will cause you problems in the future. It is sufficient to use ggplot(iris) + aes(x = Sepal.Length).

绝对legend.position可能难以在不同尺寸的图形中使用.我会推荐legend.position = "bottom".

absolute legend.position in this example can be difficult to use in the different size graphics. I would recommend legend.position = "bottom" instead.

在您的示例中,有两种方法可以控制字体的相对大小.

Here are two ways to control the relative size of the font in your example.

library(ggplot2)

data(iris)


# Okay solution, using a scaling value in an expression.  I would not recommend
# this in general, but it will work. A better solution would be to use a
# function
test <- 
  expression({
  ggplot(iris) +
  aes(x = Sepal.Length, y = Sepal.Width, colour = Species) + 
  geom_point(size = .1) +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.border     = element_blank(),
        panel.background = element_blank(),
        axis.line        = element_line(colour = "black", size = .8),
        legend.key       = element_blank(),
        axis.ticks       = element_line(colour = "black", size = .6),
        axis.text        = element_text(size = 6 * scale_value, colour = "black"),
        plot.title       = element_text(hjust = 0.0, size = 6 * scale_value, colour = "black"),
        axis.title       = element_text(size = 6 * scale_value),
        legend.text      = element_text(size = 6 * scale_value),
        legend.title     = element_text(size = 6, face = "bold"),
        legend.position  = c(.9, .15)) + 
  labs(colour = "Species")
  })

scale_value <- 1
ggsave(eval(test), width = 7 * scale_value, height = 7 * scale_value, file = "sotest1.pdf")

scale_value <- 3/7
ggsave(eval(test), width = 7 * scale_value, height = 7 * scale_value, file = "sotest2.pdf")


# Define a function to do the work.
iris_plot <- function(scale_value = 1, filename) {
  g <- ggplot(iris) +
    aes(x = Sepal.Length, y = Sepal.Width, colour = Species) + 
    geom_point(size = .1) +
    theme(panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank(),
          panel.border     = element_blank(),
          panel.background = element_blank(),
          axis.line        = element_line(colour = "black", size = .8),
          legend.key       = element_blank(),
          axis.ticks       = element_line(colour = "black", size = .6),
          axis.text        = element_text(size = 6 * scale_value, colour = "black"),
          plot.title       = element_text(hjust = 0.0, size = 6 * scale_value, colour = "black"),
          axis.title       = element_text(size = 6 * scale_value),
          legend.text      = element_text(size = 6 * scale_value),
          legend.title     = element_text(size = 6, face = "bold"),
          legend.position  = c(.9, .15)) + 
    labs(colour = "Species")

  ggsave(g, width = 7 * scale_value, height = 7 * scale_value, file = filename)
}

iris_plot(filename = "iris7x7.pdf")
iris_plot(4/7, filename = "iris4x4.pdf")

编辑

使用软件包 magick 将为您提供一个不错的编程界面,用于调整大小和进行编辑通过 imagemagick

EDIT

Using the package magick will give you a nice programming interface for resizing and editing graphics via imagemagick

例如

library(ggplot2)
library(magick)

g <-
  ggplot(iris) +
  aes(x = Sepal.Length, y = Sepal.Width, colour = Species) + 
  geom_point(size = .1) +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.border     = element_blank(),
        panel.background = element_blank(),
        axis.line        = element_line(colour = "black", size = .8),
        legend.key       = element_blank(),
        axis.ticks       = element_line(colour = "black", size = .6),
        axis.text        = element_text(size = 6, colour = "black"),
        plot.title       = element_text(hjust = 0.0, size = 6, colour = "black"),
        axis.title       = element_text(size = 6),
        legend.text      = element_text(size = 6),
        legend.title     = element_text(size = 6, face = "bold"),
        legend.position  = c(.9, .15)) + 
  labs(colour = "Species")

ggsave(g, file = "iris7x7.pdf", width = 7, height = 7)

iris_g <- image_read("iris7x7.pdf")

iris_3x3 <- image_scale(iris_g, "216x216")
image_write(iris_3x3, path = "iris3x3.pdf", format = "pdf")

请注意,调整大小后的图形可能需要进行一些编辑才能处理像素化或模糊性.

Note, the resized graphic may require some edits to deal with pixelation or blurriness.

同样,我建议不要使用绝对legend.position值.也许,如果您知道需要3英寸乘3英寸的图形,则可以使用这些尺寸打开一个开发窗口,以在其中构建图形,然后适当保存.例如,通过X11(width = 3, height = 3)打开3in x 3in X窗口.

Again, I would recommend against using an absolute legend.position value. Perhaps, if you know you need a 3in by 3in graphic you can open a dev window with those dimensions to build your graphic in and then save appropriately. For example, open a 3in by 3in X Window via X11(width = 3, height = 3).

这篇关于如何正确调整R中ggplot各个方面的大小,包括图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆