facet_wrap和缩放点大小 [英] facet_wrap and scaling point size

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

问题描述

简而言之:
我希望为每个使用 facet_wrap 创建的双面板情节的面板 。使用 facet_wrap(scales =free)适用于当我想要不同的轴比例时,但不适用于点的大小。



背景:
我有几个样品的数据,每个样品有三个测量值: x y ž。每个样本来自第1类或第2类。 x y 在每个类中具有相同的分布。但是,1级的所有 z 度量值都小于1.0;类别2的测量值范围为0到100。



我卡在哪里:
在x和y轴上分别绘制 x y 。使每个点的面积与它的 z 值成比例。

  d =矩阵(c(runif (100),runif(20)* 100),ncol = 3)
e = data.frame(gl(2,20),d)
colnames(e)= c(class, x),y,z)
ggplot(data = e,aes(x = x,y = y,size = z))+
geom_point()+ scale_area()+
facet_wrap(〜class,ncol = 1,scales =free)



问题:
请注意,第一个面板上的点很难看到,因为它们处于单个图例的范围的最低端,范围从0到100.是否有可能有两个单独的图例(每个都有不同的范围)或者我应该制作两个图并将它们与视口结合?使用<$ c

解决方案

使用 grid.arrange 。我留下了对 facet_wrap 的调用,因此 strip.text 仍然存在。

 #对于类1 
c1 < - ggplot(e [e $ class == 1,],aes(x = x,y = y,size = z))+ geom_point()+ scale_area()+ facet_wrap(〜class)
#class 2
c2 < ; - c1%+%e [e $ class == 2,]

library(gridExtra)

grid.arrange(c1,c2,ncol = 1)


In short: I would like to have separate legends for each "panel" of two-panel plot made using facet_wrap . Using facet_wrap(scales="free") works fine for when I want different axis scales, but not for the size of points.

Background: I have data for several samples with three measurements each: x, y, and z. Each sample is from either class 1 or class 2. x and y have the same distributions in each class. However, all the z measurements for class 1 are less than 1.0; z measurements for class 2 range from 0 to 100.

Where I'm stuck: Plot x and y on the x and y axes, respectively. Make the area of each point proportional to its z value.

d = matrix(c(runif(100),runif(20)*100),ncol=3)
e = data.frame( gl(2,20), d )
colnames(e) = c("class","x","y","z")
ggplot( data = e, aes(x=x, y=y, size=z) ) + 
  geom_point() + scale_area() +
  facet_wrap( ~ class, ncol=1, scales="free" )

Problem: Note that the dots on the first panel are difficult to see because they are on the very low end of the scale used for the single legend which ranges from 0 to 100. Is it even possible to have two separate legends (each with a different range) or should I make two plots and combine them with viewports?

解决方案

A solution using grid.arrange. I've left in the call to facet_wrap so the strip.text remains. You could easily remove this.

# plot for class 1
c1 <- ggplot(e[e$class==1,], aes(x=x,y=y,size=z)) + geom_point() + scale_area() + facet_wrap(~class)
# plot for class 2
c2 <- c1 %+% e[e$class==2,]

library(gridExtra)

grid.arrange(c1,c2, ncol=1)

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

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