首页 文章

ggplot2完全自定义传奇?

提问于
浏览
3

有什么方法可以创造一个完全自定义的传奇,不关心我的情节中的美学或其他任何东西?我想尽可能从头开始设计一切 . 我将拥有多少组传说,每个图例的 Headers ,形状,颜色,大小,线型,填充,标签,顺序等 . 我已经花了差不多两个工作日来试图弄清楚如何创建传说来看我希望他们看的样子(在我获得数据之后,情节本身并不需要花费几分钟) .

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

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"))

这产生了以下情节:

enter image description here

我试图定制无穷无尽的参数以获得所需的结果 . 使用了所有不同的 scale_*_manual 函数和不同的参数,使用 show.legends = FALSE ,尝试了 guide() 函数与不同的 guide_legend() 参数,我试图使 colorlinetypesize 等参数部分美观(逐个全部合并),但没有到目前为止,创建了一个像下面这样的传奇(由inkscape在这里创建):

enter image description here

我的第一个问题是我无法获得两个传奇组:一个用于"Values",一个用于"Averages" . 第二个问题是由于 geom_hlinegeom_vline ,所有图例框中都会出现垂直和水平线 . 请注意,我还为 geom_hlinegeom_vline 使用了不同的数据框 .

1 回答

  • 4

    我设法绘制了我想要的东西,但在编码方面却是一种丑陋的方式 . 简而言之,我添加了不同的美学,直到我得到至少两个具有所需数量的元素和形状的图例组 . 然后我使用了 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"))
    

    这就是结果的样子:

    enter image description here

相关问题