如何在ggplot2中分别缩放线和点的大小 [英] How to scale the size of line and point separately in ggplot2

查看:274
本文介绍了如何在ggplot2中分别缩放线和点的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下:

  set.seed(123)
d1 = data.frame(x = runif(10),y = runif(10),z = runif(10,1,10))
d2 = data.frame(x = runif(10),y = runif(10) runif(10,100,1000))
ggplot()+ geom_point(aes(x,y,size = z),data = d1)+
geom_line(aes(x,y,size = z), data = d2)

结果如下:



点的大小太小,所以我想通过<$ c $改变其大小C> scale_size 。但是,似乎线条和点都受到了影响。所以我想知道是否有一种方法可以用单独的图例分别缩放线条和点?解析方案

我可以想到的两种方法是1)结合两个传奇grobs或2)黑客另一个传奇审美。这两个都是@Mike Wise在上面的评论中提到的。

方法1:结合使用grobs在同一个地块中的两个独立的图例。



我使用此



方法#2:骇人听闻另一个美学传说。


The code is as follows:

set.seed(123)
d1=data.frame(x=runif(10),y=runif(10),z=runif(10,1,10))
d2=data.frame(x=runif(10),y=runif(10),z=runif(10,100,1000))
ggplot()+geom_point(aes(x,y,size=z),data=d1)+
geom_line(aes(x,y,size=z),data=d2)

And the result is like this:

The size of points are too small so I want to change its size by scale_size. However, it seems both lines and points are influenced. So I wonder if there is a way to scale lines and points separately with a separate legend?

解决方案

The two ways I can think of are 1) combining two legend grobs or 2) hacking another legend aesthetic. Both of these were mentioned by @Mike Wise in the comments above.

Approach #1: combining 2 separate legends in the same plot using grobs.

I used code from this answer to grab the legend. Baptiste's arrangeGrob vignette is a useful reference.

library(grid); library(gridExtra)

#Function to extract legend grob
g_legend <- function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  legend
}

#Create plots
p1 <- ggplot()+  geom_point(aes(x,y,size=z),data=d1) + scale_size(name = "point")
p2 <- ggplot()+  geom_line(aes(x,y,size=z),data=d2) + scale_size(name = "line")
p3 <- ggplot()+  geom_line(aes(x,y,size=z),data=d2) + 
        geom_point(aes(x,y, size=z * 100),data=d1)  # Combined plot
legend1 <- g_legend(p1)
legend2 <- g_legend(p2)
legend.width <- sum(legend2$width)  

gplot <- grid.arrange(p3 +theme(legend.position = "none"), legend1, legend2,
             ncol = 2, nrow = 2,
             layout_matrix = rbind(c(1,2 ),  
                                   c(1,3 )), 
             widths = unit.c(unit(1, "npc") - legend.width, legend.width))
grid.draw(gplot)

Note for printing: use arrangeGrob() instead of grid.arrange(). I had to use png; grid.draw; dev.off to save the (arrangeGrob) plot.

Approach #2: hacking another aesthetic legend.

MilanoR has a great post on this, focusing on colour instead of size. More SO examples: 1) discrete colour and 2) colour gradient.

#Create discrete levels for point sizes (because points will be mapped to fill)
d1$z.bin <- findInterval(d1$z, c(0,2,4,6,8,10), all.inside= TRUE)  #Create bins

#Scale the points to the same size as the lines (points * 100).  
#Map points to a dummy aesthetic (fill)
#Hack the fill properties.
ggplot()+  geom_line(aes(x,y,size=z),data=d2) + 
  geom_point(aes(x,y, size=z * 100, fill = as.character(z.bin)),data=d1) +
  scale_size("line", range = c(1,5)) + 
  scale_fill_manual("points", values = rep(1, 10) , 
                    guide = guide_legend(override.aes = 
                              list(colour = "black", 
                              size = sort(unique(d1$z.bin)) )))

这篇关于如何在ggplot2中分别缩放线和点的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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