根据大小将图例添加到ggplot2 geom_tile图中

时间:2016-05-22 21:55:29

标签: r ggplot2

我正在使用geom_tile在ggplot2中创建波动图,并希望为大小添加图例。我不知道该怎么做。这是一个MWE:

library(dplyr)
library(ggplot2)

# create data frame of total number of passengers in each Sex-Age group

df <- data.frame(Titanic) %>% group_by(Sex, Age) %>%
    summarise (freq = sum(Freq))

# calculate the lengths of the sides of the tiles so the largest has
# area = 1 and the others are smaller proportional to frequency

df$tileside <- sqrt(df$freq / max(df$freq))

df

## Source: local data frame [4 x 4]
## Groups: Sex [?]
## 
##      Sex    Age  freq  tileside
##   (fctr) (fctr) (dbl)     (dbl)
## 1   Male  Child    64 0.1959396
## 2   Male  Adult  1667 1.0000000
## 3 Female  Child    45 0.1643003
## 4 Female  Adult   425 0.5049248
# using geom_tile, no size legend

ggplot(df, aes(x = Sex, y = Age, 
               height = tileside, width = tileside)) +
    geom_tile() + coord_fixed (ratio = 1)

manual

我应该提到一种替代方法是使用geom_point而不是geom_tile(参见这篇文章:enter image description here

这是这种方法的MWE:

ggplot(df, aes(x = Sex, y = Age, size = freq)) +
    geom_point(shape = 15) + coord_fixed (ratio = 1)

https://stats.stackexchange.com/questions/56322/graph-for-relationship-between-two-ordinal-variables/56357#56357

问题是方块太小,如果我用scale_size()重新缩放它,我失去了波动图的最重要特征 - 方块的面积与频率成正比。 (即使没有重新缩放,我也不确定这种情况是否得到满足 - 很难说出该区域是如何计算的。)

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:2)

至于尺寸问题,您可以使用scale_size_area。我也添加了颜色。

ggplot(df, aes(x = Sex, y = Age, size = freq, color = freq)) +
  geom_point(shape = 15)  + coord_fixed (ratio = 1) + 
  scale_size_area(max_size = 20) +
  scale_color_gradient(high="red", low="black", guide = "legend") 

enter image description here