r - 在r中按组绘制散点图

标签 r scatter

r 中是否有任何函数可以允许绘制这种按组分隔点的散点图?

enter image description here

这是我到目前为止所做的:

hours = c(0.00  ,-1.78  ,-0.50  ,-2.00  ,-2.80  ,2.00   ,-0.16  ,-0.34  ,1.00   ,1.00   ,2.00   ,-1.34  ,-1.00  ,-1.10  ,-0.43  ,-0.49  ,-0.02  ,-0.91,  0.48   ,2.33   ,1.00   ,0.00   ,1.18   ,1.29   ,-1.07  ,-0.26  ,1.96   ,0.36   ,2.00   ,-0.63  ,-0.80  ,-0.70  ,-2.00  ,1.17   ,0.67   ,-3.00)
group = c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2)
df = data.frame(hours,group)

ggplot(df, aes(group, hours)) + geom_point(shape = 16, size = 5, position = position_jitter(w=0.3,h=0.3))

但结果很奇怪:

enter image description here

非常感谢任何帮助!

最佳答案

这是一种使用汽车数据的方法

library(tidyverse)
library(ggplot2)
data(cars)

cars %>%
  gather(variable, value) %>%
  ggplot()+
  geom_jitter(aes(x = variable, y = value), width = 0.2, height = 0)+
  geom_errorbar(data = cars %>%
            gather(variable, value) %>%
            group_by(variable) %>% 
            summarise(value  = mean(value)), aes(x = variable, ymin= value, ymax = value))+
  geom_hline(aes(yintercept = mean(value)), lty = 2)+
  theme_bw()

enter image description here

刚刚发布的数据:

ggplot(df)+
  geom_jitter(aes(x = as.factor(group), y = hours), width = 0.2, height = 0)+
  geom_errorbar(data = df%>%
                  group_by(group) %>% 
                  summarise(hours  = mean(hours)), aes(x = group, ymin= hours, ymax = hours))+
  geom_hline(aes(yintercept = mean(hours)), lty = 2)+
  theme_bw()

enter image description here

关于r - 在r中按组绘制散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46619838/

相关文章:

r - 将带有列表列的数据框另存为 csv 文件

regex - 使用正则表达式 (?) 填充数据框列

r - 曲线 R 下的阴影面积

python - 如何为散点图上的每个点添加标签? Matplotlib

matplotlib 中的动画与分散和使用 set_offsets : Autoscale of figure is not working

R:如何删除正方形右下半部分的值,以便结果是三角形?

r - 如何将一组函数应用于 R data.frame 中分组变量的每组

r - 基于距 0 :0 lines 距离的颜色渐变散点图

google-visualization - 如何让 Google Charts 在散点图中显示多种颜色?

python - 在 python 3 中使用 matplotlib scatter 函数时如何更改点子集的绘图标记