簡體   English   中英

在R中按組繪制散點圖

[英]Plotting scatter plot by groups in r

r中是否有任何函數可以繪制這種散點圖,從而將點按組分開?

在此處輸入圖片說明

到目前為止,這是我所做的:

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))

但是結果卻很奇怪:

在此處輸入圖片說明

任何幫助都非常感謝!

這是使用汽車數據的方法

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()

在此處輸入圖片說明

帶有剛剛發布的數據:

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()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM