如何更改图例符号(关键字形)以匹配图解符号? [英] How can I change legend symbols (key glyphs) to match the plot symbols?

查看:60
本文介绍了如何更改图例符号(关键字形)以匹配图解符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据点分成几组绘制,然后添加一个不同变量(相同单位)的箱形图.参见下图.如何更改图例中的符号以匹配图中的符号?

代码:

  p<-ggplot(data = tdata,aes(y = Temp,x = Distance,color = Type))+geom_point()+geom_boxplot(aes(y = Ambient,x = 5,color ="Ambient''))p + facet_grid(cols = vars(Time),rows = vars(Day)) 

数据:

  structure(list(Day = c(1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L),时间= c("a","a","a","a","a","b","b","b","b","b","a","a","a","a","a","b","b","b","b","b"),类型= c("s","f","s","f","s","f","s","f","s","f","s","f","s","f","s","f","s",""f","s","f"),Temp = c(5,3.1,5.9,8.4,5.5,9.9,2.2,6.9,2.9,8.8,0.6,2.2、3.1、1.4、4、8.4、5.2、2.2、7.9、2.7),距离= c(1L,2L,3L,4L,1L,2L,3L,4L,1L,2L,3L,4L,1L,2L,3L,4L,1L,2L,3L,4L),环境= c(4.4、6.3、3.1、2.5、7.7、7.1、2.1、5.1,9.4,5.7,6.3,4.9,0.8,6.5,2.1,1.6,4.4,7.9,5.3,5.2)),row.names = c(NA,-20L),类="data.frame") 

F和S分别是绿色和蓝色的点,环境"必须是图例"中的箱形图符号.

解决方案

通常,可以通过参数 key_glyph 为每个geom设置图例中的符号或字形.有关可用字形的概述,请参见(v0.3.0)创建于2020-06-22 sup>

I plot data points in several groups, but then add a box plot of a different variable (same unit). See the image below. How can I change the symbols in the legend to match the symbols in the plot?

The code:

p <- ggplot(data = tdata, aes( y=Temp ,x=Distance, color=Type)) +
  geom_point() +
  geom_boxplot( aes(y = Ambient,x=5,color="Ambient"))
p + facet_grid(cols = vars(Time),rows = vars(Day))

The data:

structure(list(Day = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Time = c("a", "a", "a", 
"a", "a", "b", "b", "b", "b", "b", "a", "a", "a", "a", "a", "b", 
"b", "b", "b", "b"), Type = c("s", "f", "s", "f", "s", "f", "s", 
"f", "s", "f", "s", "f", "s", "f", "s", "f", "s", "f", "s", "f"
), Temp = c(5, 3.1, 5.9, 8.4, 5.5, 9.9, 2.2, 6.9, 2.9, 8.8, 0.6, 
2.2, 3.1, 1.4, 4, 8.4, 5.2, 2.2, 7.9, 2.7), Distance = c(1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
2L, 3L, 4L), Ambient = c(4.4, 6.3, 3.1, 2.5, 7.7, 7.1, 2.1, 5.1, 
9.4, 5.7, 6.3, 4.9, 0.8, 6.5, 2.1, 1.6, 4.4, 7.9, 5.3, 5.2)), row.names = c(NA, 
-20L), class = "data.frame")

F and S should be green and blue dotes respectively and Ambient must be a box plot symbol in the Legend.

解决方案

In general the symbol or glyph in the legend can be set for each geom via the argument key_glyph. For an overview of available glyphs see here. (Thanks to @Tjebo for pointing that out.)

However, with the key_glyph argument one sets the glpyh globally for the "whole" geom and legend, i.e. one can not have different glyphs for different groups.

To overcome this and get to the desired result my approach uses two legends instead which I "glue" together so that they look as one. This is achieved by adding a second legend and removing the spacing and margin of the legends via theme().

To get a second legend I use the fill aes in geom_boxplot instead of color. To mimic a mapping on color I set the fill color to white via scale_fill_manual and adjust the fill guide via guide_legend. Try this:

library(ggplot2)

p <- ggplot() + 
  geom_point(data = tdata, aes(y=Temp, x = Distance, color = Type)) + 
  geom_boxplot(data = tdata, aes(x = 5, y = Ambient, fill = "box"), color = scales::hue_pal()(3)[1]) +
  scale_fill_manual(values = "white") +
  scale_color_manual(values = scales::hue_pal()(3)[2:3]) +
  guides(fill = guide_legend(title = "Type", order = 1), color = guide_legend(title = NULL, order = 2)) +
  theme(legend.margin = margin(t = 0, b = 0), legend.spacing.y = unit(0, "pt"), legend.title = element_text(margin = margin(b = 10)))
p + facet_grid(cols = vars(Time),rows = vars(Day))

Created on 2020-06-22 by the reprex package (v0.3.0)

这篇关于如何更改图例符号(关键字形)以匹配图解符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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