geom图层以点为计数设置两个分类轴

时间:2018-07-09 11:44:43

标签: r ggplot2

我正在完成Hadley Wickham的书ggplot2中的练习。该书要求重新创建一张图片: enter image description here

这是我的代码:

library(tidyverse)    
count <- mpg %>%
 group_by(drv, cyl) %>%
 summarise(n = n())
count
ggplot(mpg, aes(x = cyl, y = drv)) +
 geom_point(aes(size = n), data = count, position = "jitter")

enter image description here 但是它没有显示相同的图片。我无法弄清楚该图是哪个几何图形。但有一件事是,图中的点可能意味着与cyl和drv相匹配的观测值。

数据为mpg,包含在tidyverse软件包中。

1 个答案:

答案 0 :(得分:2)

您应该使用geom_jitter而不是geom_point

library(ggplot2)
ggplot(mpg, aes(cyl, drv)) +
    geom_jitter(position = position_jitter(0.05, 0.05))

默认情况下,geom_jitter中的抖动太大,我们需要使用position_jitter函数来指定自己的抖动高度和宽度。

enter image description here