【问题标题】:ggplot2 completely custom legend?ggplot2 完全自定义图例?
【发布时间】:2016-12-01 15:16:00
【问题描述】:

有什么方法可以让我创建一个完全自定义的图例,而不关心我的情节中的美学或其他任何内容?如果可能的话,我想从头开始设计一切。我将拥有多少组图例,每个图例的标题、形状、颜色、大小、线型、填充、标签、顺序等是什么。我已经花了将近两个工作日试图弄清楚如何创建图例以使其看起来像我想要的样子(在我获得数据后,情节本身并没有超过几分钟的时间)。

看看下面的示例代码(随机数据,但可以很好地展示我想要的):

require(dplyr)
require(RColorBrewer)

col <- brewer.pal(3, "Spectral")

a <- data.frame(multiplier = c(0.5, 0.7, 1.0), multiplier2 = c(0.3, 0.1), random = runif(3 * 500))
a$result = a$multiplier * a$random * runif(length(a$random)) * a$multiplier2

a_grouped_by_multiplier = group_by(a, multiplier)
means = summarise(a_grouped_by_multiplier, mean_rand = mean(random), mean_res = mean(result))

ggplot(a, aes(x = random, y = result)) +
  geom_density2d(bins = 20) +
  geom_point(aes(color = factor(multiplier2)), size = 0.7) +
  geom_vline(data = means, aes(xintercept = mean_rand), color = "orange", linetype = "solid", size = 1.5, show.legend = TRUE) +
  geom_hline(data = means, aes(yintercept = mean_res), color = "red", linetype = "dashed", size = 1.5, show.legend = TRUE) +
  scale_color_manual(name = "Values", values = c(col[1], col[2]), labels = c("* 0.1", "* 0.3")) +
  facet_grid(~ multiplier) +
  theme(panel.grid.major = element_line(colour = "white", linetype = "dashed", size = 0.3),
        panel.grid.minor = element_blank(),
        panel.background = element_rect(fill = "#555555"),
        legend.key = element_rect(fill = "#555555"))

这将创建以下情节:

我尝试自定义无尽的参数以获得所需的结果。使用不同参数的所有不同scale_*_manual 函数,使用show.legends = FALSE,尝试使用不同guide_legend() 参数的guide() 函数,我尝试制作colorlinetypesize 等参数部分的美学(一个一个和所有结合),但到目前为止没有任何工作可以创造一个像下面这样的传奇(由inkscape在这里创建):

我的第一个问题是我无法获得两个图例组:一个用于“值”,一个用于“平均值”。第二个问题是,由于geom_hlinegeom_vline,所有图例框中都出现了垂直和水平线。请注意,我还为geom_hlinegeom_vline 使用了不同的数据框。

【问题讨论】:

  • 简短的回答是否定的,ggplot 传说与美学密不可分。不过,它们是非常可定制的。在这种情况下,您需要找到一种方法来使平均值具有美感,可能是通过制作一个虚拟 data.frame 并使用geom_abline

标签: r ggplot2 legend


【解决方案1】:

我设法绘制了我想要的东西,但在编码方面却以一种丑陋的方式。 简而言之,我添加了不同的美学,直到我得到至少两个具有所需元素和形状数量的图例组。然后我使用scale_*_manual 函数和guide() 函数来覆盖一些美学,使图例看起来几乎应有的样子(但我还是没有设法在图例框中有一条垂直线)。

代码如下:

ggplot(a, aes(x = random, y = result)) +
  geom_density2d(bins = 20) +
  geom_point(aes(color = factor(multiplier2), shape = factor(multiplier2), fill = factor(multiplier2)), stroke = 0.01, size = 2.5) +
  geom_vline(data = means, aes(xintercept = mean_rand, color = "mean_rand", size = "mean_rand"), show.legend = FALSE) +
  geom_hline(data = means, aes(yintercept = mean_res, color = "mean_res", size = "mean_res"), linetype = 12, show.legend = FALSE) +
  scale_color_manual(values = c(col[1], col[2], "orange", "red")) +                             # Provides the desired colors for the plot
  scale_shape_manual(name = "Values", values = c(21, 22), labels = c("*0.1", "*0.3")) +         # Provides the desired shape and plots the "Values" legend
  scale_fill_manual(name = "Values", values = c(col[1], col[2]), labels = c("*0.1", "*0.3")) +  # Provides the fill for the shapes and merges the legend with the shape ("Values") legend
  scale_size_manual(name = "Averages", values = c(1.5, 1.5), labels = c("Random", "Result")) +  # Provides a legend for the Averages but looks ugly.....
  guides(color = FALSE,  # Hides the "color" legend because it has 4 values and we don't want that.
         shape = guide_legend(order = 1), # Forces the "Values" legend group to be on top
         fill = guide_legend(order = 1),  # Forces the "Values" legend group to be on top
         size = guide_legend(order = 2, override.aes = list(linetype = c(1, 12), color = c("orange", "red")))) +  # Makes the "Average" legend to look as it should, by overriding the aesthetics
  facet_grid(~ multiplier) +
  theme_dark() +
  theme(panel.grid.major = element_line(colour = "white", linetype = "dashed", size = 0.2),
        panel.background = element_rect(fill = "#555555"),
        legend.key = element_rect(fill = "#555555"))

结果是这样的:

【讨论】:

    猜你喜欢
    • 2012-11-07
    • 2015-04-28
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多