散点图显示具有相同值的所有点

use*_*709 1 statistics plot r scatter-plot

如何在R中创建散点图,以便即使在某些类别中具有相同的值,也会显示所有点.除了数据点,我希望每个类别都有平均值.

例如,如果我有两个变量的数据集,其中一个(棉花重量百分比)是因素:

dat <- structure(list(`Tensile Strength` = c(12L, 19L, 17L, 7L, 25L, 
7L, 14L, 12L, 18L, 22L, 18L, 7L, 18L, 18L, 15L, 10L, 11L, 19L, 
11L, 19L, 15L, 19L, 11L, 23L, 9L), `Cotton weight percent` = c(20L, 
30L, 20L, 35L, 30L, 15L, 25L, 20L, 25L, 30L, 20L, 15L, 25L, 20L, 
15L, 35L, 35L, 25L, 15L, 25L, 35L, 30L, 35L, 30L, 15L)), .Names = c("Tensile Strength", 
"Cotton weight percent"), class = "data.frame", row.names = c(NA, 
-25L))
Run Code Online (Sandbox Code Playgroud)

如何制作像这样的散点图:在此输入图像描述

这里,实心点是单独的观察结果,空心圆是平均观察到的拉伸强度.

Dav*_*son 5

这可以在geom_jitter和ggplot2中完成stat_summary.具体来说,geom_jitter会在图表上显示黑点:

library(ggplot2)
ggplot(mtcars, aes(factor(cyl), mpg)) +
    geom_jitter(position = position_jitter(width = .1))
p
Run Code Online (Sandbox Code Playgroud)

("抖动"是根据x轴添加一些噪声,如示例中所示).

然后该stat_summary图层允许您为每个x值的平均值添加一个点(我已经做了大而红的):

ggplot(mtcars, aes(factor(cyl), mpg)) +
    geom_jitter(position = position_jitter(width = .1)) +
    stat_summary(fun.y = "mean", geom = "point", color = "red", size = 3)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述