Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R GGPLOT盒形图按颜色细分,平均值在盒形图中间_R_Ggplot2_Mean_Boxplot - Fatal编程技术网

R GGPLOT盒形图按颜色细分,平均值在盒形图中间

R GGPLOT盒形图按颜色细分,平均值在盒形图中间,r,ggplot2,mean,boxplot,R,Ggplot2,Mean,Boxplot,我有两个分类变量的数据。我可以用方框图来表示这些,但我无法得到在正确位置显示的方法。 我已经在iris数据集中创建了效果(红色矩形是手工添加的,而不是在ggplot中) 如何在每个方块图中间显示方法? 我很感激我可以重新安排数据,使每一组数据点都在它自己的列中,但由于它们的长度不同,这需要它自己的解决方法 当我使用fun=“mean”时,我收到一条警告消息“删除了包含缺失值的5行(geom_段)。”为什么?“平均值”行没有这个问题,但我不想自己计算平均值。您可以像下面的代码一样使用positi

我有两个分类变量的数据。我可以用方框图来表示这些,但我无法得到在正确位置显示的方法。 我已经在iris数据集中创建了效果(红色矩形是手工添加的,而不是在ggplot中)

如何在每个方块图中间显示方法? 我很感激我可以重新安排数据,使每一组数据点都在它自己的列中,但由于它们的长度不同,这需要它自己的解决方法


当我使用fun=“mean”时,我收到一条警告消息“删除了包含缺失值的5行(geom_段)。”为什么?“平均值”行没有这个问题,但我不想自己计算平均值。

您可以像下面的代码一样使用
position=position\u dodge(0.9)

library(tidyverse)

Iris <- iris %>%
  mutate(SepalLengthType = ifelse(Sepal.Length > 5.8, "high", "low"))

means <- Iris %>% 
  group_by(Species, SepalLengthType) %>% 
  summarise(Sepal.Width = mean(Sepal.Width), .groups = "keep")

plot <- ggplot(data = Iris, aes(y=Sepal.Width, x = SepalLengthType, colour = Species))+
  geom_boxplot(position=position_dodge(0.9))

plot + geom_point(data = means, aes(color = Species), shape = 15, 
                  position = position_dodge2(width = 0.9))

您可以像下面的代码一样使用
position=position\u dodge(0.9)

library(tidyverse)

Iris <- iris %>%
  mutate(SepalLengthType = ifelse(Sepal.Length > 5.8, "high", "low"))

means <- Iris %>% 
  group_by(Species, SepalLengthType) %>% 
  summarise(Sepal.Width = mean(Sepal.Width), .groups = "keep")

plot <- ggplot(data = Iris, aes(y=Sepal.Width, x = SepalLengthType, colour = Species))+
  geom_boxplot(position=position_dodge(0.9))

plot + geom_point(data = means, aes(color = Species), shape = 15, 
                  position = position_dodge2(width = 0.9))

非常感谢,道奇位置成功了。你知道为什么我会收到警告吗?“删除了包含缺失值的4行(geom_段)。”我认为这是因为setosa在高类别下没有任何值。不,我用自己的数据得到了相同的警告。我的数据删除了4行,这里删除了5行。它与方框图的数量有关。如果我删除color=Species并且只有两个箱线图,那么警告信息会说:“删除了两行包含缺失值的行(geom_段)。”非常感谢,position dodge成功了。你知道为什么我会收到警告吗?“删除了包含缺失值的4行(geom_段)。”我认为这是因为setosa在高类别下没有任何值。不,我用自己的数据得到了相同的警告。我的数据删除了4行,这里删除了5行。它与方框图的数量有关。如果我删除color=Species,并且只有两个箱线图,则警告消息会显示:“删除了包含缺失值的2行(geom_段)。”
library(tidyverse)

Iris <- iris %>%
  mutate(SepalLengthType = ifelse(Sepal.Length > 5.8, "high", "low"))

means <- Iris %>% 
  group_by(Species, SepalLengthType) %>% 
  summarise(Sepal.Width = mean(Sepal.Width), .groups = "keep")

plot <- ggplot(data = Iris, aes(y=Sepal.Width, x = SepalLengthType, colour = Species))+
  geom_boxplot(position=position_dodge(0.9))

plot + geom_point(data = means, aes(color = Species), shape = 15, 
                  position = position_dodge2(width = 0.9))
plot + stat_summary(fun = "mean", aes(group = Species), shape = 15, 
                  position = position_dodge2(width = 0.9))