根据ggplot中的密度使geom更大

Make geom's bigger based on density in ggplot

我目前正在 ggplot 上将值映射为 Geoms。这些值在 y 轴上,它们的类别在 x 轴上。

数据看起来像这样:

Value   condition
1         TRUE
1         TRUE
1         TRUE
6         TRUE
10        TRUE

最小 运行 示例:

df <- data.frame(c(1,1,1,1,1,2,3,3,1,6), TRUE) 
colnames(df) <- c("value", "condition")
ggplot(df, aes(x = condition, y = value)) + geom_point()

因此,例如,在条件 TRUE 下的值 1,我怎样才能使点(或任何形状)比其他点 更大 ,因为那里有更多的数据点。

6 个比 3 个三分或 1 个二分或 1 个六分有更多的数据点。 因此,在此示例中,1 将是最大的,其次是 3,而 2 和 6 的大小相同

使用 dplyr 包,您可以重塑数据。然后,就可以画图了。

mydf <- data.frame(c(1,1,1,1,1,1,2,3,3,1,6), TRUE) 
colnames(mydf) <- c("value", "condition")

library(dplyr)
count(mydf, condition, value) -> mydf2 

ggplot(mydf2, aes(x = condition, y = value, size = n)) + geom_point()