有几个选项。我假设不需要图例,因为这些点标有百分比计数。
一种选择是添加另一个stat_sum()
包含标签美学和“文本”几何图形的函数。例如:
library("ggplot2")
ggplot(diamonds, aes(x = cut, y = clarity, group = 1)) +
stat_sum(geom = "point", show.legend = FALSE) +
stat_sum(aes(label = paste(round(..prop.. * 100, 2), "%", sep = "")),
size = 3, hjust = -0.4, geom = "text", show.legend = FALSE)
或者,可能不需要积分。标签可以完成所有工作 - 显示位置和大小:
ggplot(diamonds, aes(x = cut, y = clarity, group = 1)) +
stat_sum(aes(label = paste(round(..prop.. * 100, 2), "%", sep = "")),
geom = "text", show.legend = FALSE) +
scale_size(range=c(2, 8))
有时在 ggplot 之外创建汇总表更容易:
library(plyr)
df = transform(ddply(diamonds, .(cut, clarity), "nrow"),
percent = round(nrow/sum(nrow)*100, 2))
ggplot(df, aes(x = cut, y = clarity)) +
geom_text(aes(size = percent, label = paste(percent, "%", sep = "")),
show.legend = FALSE) +
scale_size(range = c(2, 8))