通过facet改变geom_point大小的比例

时间:2015-08-25 14:42:39

标签: r ggplot2 facet-wrap

我正在使用带有facet_wrap的ggplot来生成3个带有线性模型的并排图。另外,我有另一个维度(我们称之为“z”)我想通过改变图上点的大小来想象。

目前,我生成的图表在所有3个方面上保持相同比例的点的大小。我希望通过facet缩放点大小 - 这样,可以快速判断哪个点包含每个方面的最高“z”值。

有没有办法在不创建3个独立图的情况下执行此操作?我已经包含了我的数据样本和我在下面使用的代码:

x <- c(0.03,1.32,2.61,3.90,5.20,6.48,7.77,0.75,2.04,3.33,4.62,5.91,7.20,8.49,0.41,1.70,3.00,4.28,5.57,6.86,8.15)
y <- c(650,526,382,110,72,209,60,559,296,76,48,64,20,22,50,102,176,21,20,25,5)
z <- c(391174,244856,836435,46282,40351,27118,17411,26232,59162,9737,1917,20575,1484,450,12071,13689,133326,1662,711,728,412)
facet <- c("A","A","A","A","A","A","A","B","B","B","B","B","B","B","C","C","C","C","C","C","C")
df <- data.frame(x,y,z,facet)

ggplot(df, aes(x=x, y=y)) + 
  geom_point(aes(size=z)) + 
  geom_smooth(method="lm") +
  facet_wrap(~facet)

3 个答案:

答案 0 :(得分:0)

我只想按df$z

df$facet的平均值
AverageFacet  <- df %>% group_by(facet) %>% summarize(meanwithinfacet= mean(z, na.rm=TRUE))
df <- merge(df, AverageFacet) 
df$pointsize<- df$z - df$meanwithinfacet

现在每个点的大小取决于方面的平均值

> head(df,10)
   facet    x   y      z meanwithinfacet   pointsize
1      A 0.03 650 391174       229089.57  162084.429
2      A 1.32 526 244856       229089.57   15766.429
3      A 2.61 382 836435       229089.57  607345.429
4      A 3.90 110  46282       229089.57 -182807.571
5      A 5.20  72  40351       229089.57 -188738.571
6      A 6.48 209  27118       229089.57 -201971.571
7      A 7.77  60  17411       229089.57 -211678.571
8      B 0.75 559  26232        17079.57    9152.429
9      B 2.04 296  59162        17079.57   42082.429

和情节

ggplot(df, aes(x=x, y=y)) + 
        geom_point(aes(size=pointsize)) + 
        geom_smooth(method="lm") +
        facet_wrap(~facet)

enter image description here

看起来像这样,但不确定传说。

您也可以使用与平均值的绝对差值而不是使用与给定z的平均值相差多少标准

AverageFacet  <- df %>% group_by(facet) %>% summarize(meanwithinfacet= mean(z, na.rm=TRUE), sdwithinfacet= sd(z, na.rm=TRUE))
df <- merge(df, AverageFacet) 
df$absoluteDiff<- df$z - df$meanwithinfacet
df$SDfromMean <- df$absoluteDiff / df$sdwithinfacet


ggplot(df, aes(x=x, y=y)) + 
        geom_point(aes(size=SDfromMean)) + 
        geom_smooth(method="lm") +
        facet_wrap(~facet)

enter image description here

答案 1 :(得分:0)

下面的方法将z重新分配给它的z-score 它的方面:

require(dplyr)
require(ggplot)
require(magrittr)
require(scales)

x <- c(0.03,1.32,2.61,3.90,5.20,6.48,7.77,0.75,2.04,3.33,4.62,5.91,7.20,8.49,0.41,1.70,3.00,4.28,5.57,6.86,8.15)
y <- c(650,526,382,110,72,209,60,559,296,76,48,64,20,22,50,102,176,21,20,25,5)
z <- c(391174,244856,836435,46282,40351,27118,17411,26232,59162,9737,1917,20575,1484,450,12071,13689,133326,1662,711,728,412)
facet <- c("A","A","A","A","A","A","A","B","B","B","B","B","B","B","C","C","C","C","C","C","C")

df <- data.frame(x,y,z,facet)
df %<>% 
    group_by(facet) %>% 
    mutate(z = scale(z)) # calculate point size within group

ggplot(df, aes(x=x, y=y, group = facet)) + 
    geom_point(aes(size=z)) + 
    geom_smooth(method="lm") +
    facet_wrap(~facet )

答案 2 :(得分:0)

尝试重新缩放每个构面的大小以获取(0,1]中的值:

df %>% 
  group_by(facet) %>% 
  mutate(newz = z/max(z)) %>%
  ggplot(., aes(x=x, y=y)) + 
  geom_point(aes(size=newz)) + 
  geom_smooth(method="lm") +
  facet_wrap(~facet)

enter image description here