在ggplot2中使用带有分层符号(R)的override.aes() [英] Using override.aes() in ggplot2 with layered symbols (R)

查看:1162
本文介绍了在ggplot2中使用带有分层符号(R)的override.aes()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近问了一个问题这里允许我在 ggplot2 中用可自定义的颜色和粗细添加边框到 geom_point() s。诚然,这有点破解,现在我可能会遇到一些麻烦,这可能是由此导致的。边框由两个 geom_point() s生成,其中一个是我想要的边框的颜色,并且比 geom_point()它提供了填充。这里有一个可重复的例子:

  require('ggplot2')

values< - rnorm(n = 10,mean = 1,sd = 0.5)+ c(1:10)

df < - data.frame(id = rep(c('hq','lq')) = 5),
values = values,
period = rep(c(1:5),2))

plot < -
ggplot(df, aes(x = period,
y = values,
group = id,
shape = id,
color = id))+
geom_line(color ='gray40' )+
geom_point(color ='gray24',
size = 4)+
geom_point(size = 3)+
guides(shape = guide_legend(override.aes = list size = 5)))+
scale_color_manual(values = c('lightskyblue1','lightpink'),
labels = c('HQ','LQ'))+
scale_shape_manual值= c(15,17,0,2),
labels = c('HQ','LQ'))+
th eme_bw()

因为我希望图例符号大于情节符号,所以我使用 override.aes()。但是,这意味着我创建的边界不会出现在图例中:


是否有一些方法可以给出多个 size 参数给 override.aes(),这样我在图中使用的图层在图例中保持不变(即图例符号也包含边框) ?

解决方案

如果您只需一次调用 geom_point if你可以使用带有边框和填充的点标记(pch值从21到25;参见?pch )。然后,您可以使用 override.aes 设置图例标记的大小,边框和填充将始终显示正确。例如:

  ggplot(dat,aes(x = period,
y = values,
group = id,
shape = id,
fill = id))+
geom_line(color ='gray40')+
geom_point(size = 4)+
guides(形状= guide_legend(override.aes = list(size = 5)))+
scale_fill_manual(values = c('lightskyblue1','lightpink'),
labels = c('HQ','LQ '))+
scale_shape_manual(values = c(22,24),#这些是标记形状
labels = c('HQ','LQ'))+
theme_bw()

这会导致点标记的黑色边框(默认值,但您可以更改它)和您指定的填充颜色。





更新:为回应您的评论,以下是如何在图例标记上取得黑色边框,而无需对原始代码进行任何其他更改:

b
$ b

  guides(shape = guide_legend(override.aes = 
list(size = 5,shape = c(22,24),
color =black,
fill = c('lightskyblue1','lightpink')))+


I recently asked a question here which allowed me to add borders to geom_point()s in ggplot2 with customizable colors and thickness. Admittedly, it's a bit of a hack and I'm now having some trouble which may be a result of this. The borders are generated by layering two geom_point()s, one of which is the color of the border that I want and which is slightly larger than the geom_point() which provides the "fill". A reproducible example here:

require('ggplot2')

values <- rnorm(n = 10, mean = 1, sd = 0.5) + c(1:10)

df <- data.frame(id = rep(c('hq', 'lq'), each = 5),
                 values = values,
                 period = rep(c(1:5), 2))

plot <- 
    ggplot(df, aes(x = period,
                   y = values,
                   group = id,
                   shape = id,
                   color = id)) +
    geom_line(color = 'gray40') +
    geom_point(color = 'gray24',
               size = 4) +
    geom_point(size = 3) +
    guides(shape = guide_legend(override.aes = list(size = 5))) +
    scale_color_manual(values = c('lightskyblue1', 'lightpink'),
                       labels = c('HQ', 'LQ')) +
    scale_shape_manual(values = c(15, 17, 0, 2),
                       labels = c('HQ', 'LQ')) +
    theme_bw()

Because I would like the legend symbols to be larger than the plot symbols, I've used override.aes(). However, this means that the "borders" I've created do not appear in the legend:

Is there some way to give multiple size arguments to override.aes() so that the layering I've used in the plot is left intact in the legend (i.e. the legend symbols also contain the "borders")?

解决方案

You can do this with a single call to geom_point if you use a point-marker with both a border and a fill (pch values from 21 through 25; see ?pch). Then, you can set the size of the legend markers with override.aes and the borders and fill will always appear correctly. For example:

ggplot(dat, aes(x = period,
                y = values,
                group = id,
                shape = id,
                fill=id)) +
  geom_line(color = 'gray40') +
  geom_point(size=4) +
  guides(shape = guide_legend(override.aes = list(size = 5))) +
  scale_fill_manual(values = c('lightskyblue1', 'lightpink'),
                    labels = c('HQ', 'LQ')) +
  scale_shape_manual(values = c(22,24),   # These are the marker shapes
                     labels = c('HQ', 'LQ')) +
  theme_bw()

This results in a black border for the point markers (the default value, but you can change it) and the fill colour you specify.

UPDATE: In response to your comment, here's how to get the black border on the legend markers without any other changes to your original code:

  guides(shape = guide_legend(override.aes = 
                                list(size = 5, shape=c(22,24), 
                                     colour="black", 
                                     fill=c('lightskyblue1', 'lightpink')))) +

这篇关于在ggplot2中使用带有分层符号(R)的override.aes()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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