题
假定下列GGPLOT2图表:
ggplot(my_data, aes(colour=my_factor) +
geom_point(aes(x=prior, y=current)) +
facet_grid(gender ~ age)
我想使点的大小成比例的my_factor对于现有/当前组合的计数。
ggplot(my_data, aes(colour=my_factor,
size=<something-here>(my_factor)) +
geom_point(aes(x=prior, y=current)) +
facet_grid(gender ~ age)
任何想法?
==编辑==
下面是基于MPG数据集非常简单的例子。让我们定义 “great_hwy” 为HWY> 35,以及 “great_cty” 作为CTY> 25:
mpg$great_hwy[mpg$hwy > 35] <-1
mpg$great_hwy[mpg$hwy <= 35] <-0
mpg$great_hwy <- factor(mpg$great_hwy)
mpg$great_cty[mpg$cty > 25] <- 1
mpg$great_cty[mpg$cty <= 25] <- 0
mpg$great_cty <- factor(mpg$great_cty)
如果我们绘制great_hwy与great_cty,它不会告诉我们多少:
ggplot(mpg) + geom_point(aes(x=great_cty, y=great_hwy))
我怎么能使在尺寸更大的数据点,这取决于的x / y的点的数量?希望这会清除它,但让我知道,否则。
解决方案
您可以肯定是由计数外部ggplot做到这一点,但约ggplot伟大的事情之一是,你可以做很多这些统计数据的内部!
使用上面的MPG示例:
ggplot(mpg) +
geom_point(aes(x=great_cty, y=great_hwy,
size=..count..), stat="bin")
其他提示
由于接受的答案使用弃用特征我指出,对于ggplot2 1.0.1
工作此替代答案
不隶属于 StackOverflow