我正在使用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)
我应该提到一种替代方法是使用geom_point而不是geom_tile(参见这篇文章:)
这是这种方法的MWE:
ggplot(df, aes(x = Sex, y = Age, size = freq)) +
geom_point(shape = 15) + coord_fixed (ratio = 1)
问题是方块太小,如果我用scale_size()重新缩放它,我失去了波动图的最重要特征 - 方块的面积与频率成正比。 (即使没有重新缩放,我也不确定这种情况是否得到满足 - 很难说出该区域是如何计算的。)
非常感谢你的帮助。