ggplot2完全自定义图例? [英] ggplot2 completely custom legend?

查看:3066
本文介绍了ggplot2完全自定义图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法可以创建一个完全自定义的传说,不会关心我的情节中的美学或其他任何东西?我想如果可能的话,从头开始设计一切。我将拥有多少组传奇,每个传说的标题,形状,颜色,大小,线型,填充,标签,顺序等将会是什么。我已经花了差不多两个工作日的时间来尝试弄清楚如何创建传奇以寻找我希望他们看的方式(情节本身在获得数据后不会超过几分钟)。



看一下下面的示例代码(随机数据,但是可以用来展示我想要的):

  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乘数* a $ random * runif (长度(a $随机))* a $ multiplier2

a_grouped_by_multiplier = group_by(a,乘数)
means = summarize(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 = (color =value),values = c(col [1],col [2]),labels = c(* 0.1,size = 1.5,show.legend = TRUE)+
scale_color_manual * 0.3))+
facet_grid(〜multiplier)+
theme(panel.grid.major = element_line(color =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()函数使用不同的 guide_legend()参数,我尝试使颜色 linetype size etc参数是美学的一部分(逐个和全部合并),但目前为止没有创建像以下这样的图例(由inkscape创建):



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

解决方案

我设法绘制我想要的内容,但在编码方面却丑陋。
总之,我添加了不同的美学,直到我得到至少两个具有所需数量的元素和形状的传奇组。然后我使用 scale _ * _ manual 函数和指南()函数来覆盖一些美学,并制作图例看起来几乎是它应该的(仍然没有在图例框中设置垂直线)。



代码如下:

  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))+#提供所需的颜色
scale_shape_manual(name =Values,values = c(21,22)),标签= c(* 0.1,* 0.3))+#提供所需的形状并绘制值图例
scale_fill_manual(name =Values,values = c(col [1],col [ 2]),labels = c(* 0.1,* 0.3))+#提供形状填充并将图例与形状(值)合并图例
scale_size_manual(name =Averages ,values = c(1.5,1.5),labels = c(Random,Result))+#为Averages提供图例,但看起来很丑陋.....
guides(color = FALSE, #隐藏颜色图例,因为它有4个值,我们不希望这样。
shape = guide_legend(order = 1),#强制值图例组位于最上方$ b $ fill = guide_legend(order = 1),#强制值图例组位于最上方
size = guide_legend(order = 2,override.aes = list(linetype = c(1,12),color = c(orange,red))))+#制作平均图例通过重写美学
facet_grid(〜multiplier)+
theme_dark()+
theme(panel.grid.major = element_line(color =white,linetype =
panel.background = element_rect(fill =#555555),
legend.key = element_rect(fill =#555555))

结果如下:


Is there any way that I can create a completely custom legend that won't care about the aesthetics or anything else in my plot? I would like if possible to design everything from scratch. How many groups of legends I will have, what will be the title, shape, color, size, linetype, fill, label, order etc for each legend. I have already spent almost two working days try to figure out how to create the legends to look the way I want them to look (The plot itself didn't take more than a few minutes after I had the data).

Take a look at the following sample code (random data, but fine for demonstrating what I want):

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

That creates the following plot:

I have tried to customize endless parameters to get the desired result. Used all the different scale_*_manual functions with different parameters, used the show.legends = FALSE, tried the guide() function with different guide_legend() parameters, I tried to make the color, linetype, size etc parameters part of the aesthetics (one by one and all combined), but nothing so far worked for creating a legend like the following (created by inkscape here):

My first problem is that I cannot get two legend groups: one for the "Values" and one for the "Averages". Second problem is that due to the geom_hline and geom_vline, vertical and horizontal lines appear in all of the legend boxes. Note that I also use a different data frame for the geom_hline and geom_vline.

解决方案

I managed to plot what I wanted but in an ugly way when it comes to coding. In short, I added different aesthetics until I got at least two legend groups with the desired number of elements and shapes. Then I used the scale_*_manual functions and the guide() function to override some of the aesthetics and make the legend look almost as it should (still I didn't manage to have a vertical line in a legend box).

The code is the following:

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

And that's how the result looks like:

这篇关于ggplot2完全自定义图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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