简体   繁体   English

R ggplot:更改Grouped Boxplot Median行

[英]R ggplot: Change Grouped Boxplot Median line

I have plotted grouped boxplots and now I would like to replace the standard black median line with a white line while keeping the borders in an other color. 我已经绘制了分组的箱形图,现在我想用白线代替标准的黑色中线,同时保持边框为另一种颜色。 I'm following the instructions on the site Minimalist Boxplots because I really like their style. 我正在按照Minimalist Boxplots网站上的说明进行操作,因为我非常喜欢他们的风格。

They use the command stat_summary(geom = "crossbar", width=0.65, fatten=0, color="white", fun.data = function(x){ return(c(y=median(x), ymin=median(x), ymax=median(x))) }) . 他们使用命令stat_summary(geom = "crossbar", width=0.65, fatten=0, color="white", fun.data = function(x){ return(c(y=median(x), ymin=median(x), ymax=median(x))) }) That command worked well with simple boxplots: 该命令适用于简单的箱形图: 在此输入图像描述

But now that I'm trying to use it on my grouped boxplots its not working anymore (white median line is missing): 但是现在我正在尝试在我的分组箱图上使用它,它不再工作了(缺少白色中线):

在此输入图像描述

Here is my the code for reproducing my data: 这是我复制数据的代码:

Data <- data.frame(
  W = sample(1:100),
  M = sample(1:100),
  A = sample(1:100),
  O = sample(1:100),
  Type = sample(c("1", "2", "3", "4", "5")))

And my script: 我的剧本:

Data_Boxplot <- melt(Data,id.vars='Type', measure.vars=c('W','M','A', 'O'))
Boxplots <- ggplot(Data_Boxplot, aes(Type, value, group=variable)) +
  geom_boxplot(outlier.colour = NULL, aes(color=variable, fill=variable)) + 
  stat_summary(geom = "crossbar", width=0.65, fatten=0, color="white", 
               fun.data = function(x){c(y=median(x), ymin=median(x), ymax=median(x))})
Boxplots

What do I have to change? 我需要改变什么? Thank you very much for your help. 非常感谢您的帮助。

You only mapped x and y for the geom_boxplot . 您只映射了geom_boxplot xy Variables that are shared among geoms should be mapped for all. 应该为所有人映射在地理位置之间共享的变量。 In this case, you also need to group by variable . 在这种情况下,您还需要按variable分组。

ggplot(Data.m, aes(Type, value, group=variable) +
  geom_boxplot(outlier.colour = NULL, aes(color=variable, fill=variable)) + 
  stat_summary(geom = "crossbar", width=0.65, fatten=0, color="white", 
               fun.data = function(x){c(y=median(x), ymin=median(x), ymax=median(x))})

I did not test, as you did not provide data. 我没有测试,因为你没有提供数据。

Edit: 编辑:

Ok, now that we have a good example with data we can see what's going on. 好的,现在我们有一个很好的数据示例,我们可以看到正在发生的事情。

There is actually a two issues I had missed. 实际上我错过了两个问题。 Both are because geom_boxplot will automatically solve some problems for you because of the fill , that stat_summary doesn't. 两者都是因为geom_boxplot会自动解决一些问题给你,因为的fill ,即stat_summary没有。 So we'll have to do them manually. 所以我们必须手动完成它们。

Firstly, we want to be grouping on both variable as well as Type , we can do this by using the interaction function. 首先,我们想要对variableType进行分组,我们可以通过使用interaction功能来实现。

Secondly, the boxplots are automatically dodged (ie moved apart within groups), while the horizontal lines aren't. 其次,箱形图被自动躲避(即在组内移动),而水平线则不是。 We'll define our positioning using position_dodge , and apply it to both geoms. 我们将使用position_dodge定义我们的定位,并将其应用于两个geoms。 Applying it to both is the easiest way to make them exactly line up. 将它应用于两者是使它们完全对齐的最简单方法。 We end up with: 我们最终得到:

p <- position_dodge(0.8)
ggplot(Data_Boxplot, aes(Type, value, group = interaction(variable, Type))) +
  geom_boxplot(aes(color = variable, fill = variable), outlier.colour = NULL, position = p) + 
  stat_summary(geom = "crossbar", width = 0.6, fatten=0, color="white", position = p,
               fun.data = function(x){c(y=median(x), ymin=median(x), ymax=median(x))}) +
  theme_minimal()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM