将箱线图与单个值进行比较
compare boxplots with a single value
我想比较几个变量(此处 X1
和 X2
)与单个值(此处 bm
)的分布。问题是这些变量太多(大约一打)而无法使用单个箱线图。
此外,关卡差异太大,无法使用一个地块。我需要使用 facets 让事情更有条理:
然而,对于这个图,我的基准类别 (bm
),它是 X1
和 X2
中的单个值,没有出现在 X1
中,似乎在 X2
中有多个值。我希望它只是这条绿线,它在第一个图中。任何想法为什么会改变?有什么好的解决方法吗?我尝试了 facet_wrap
/facet_grid
的选项,但没有提供正确的结果。
我还尝试将条形图与 bm
结合使用,并将三个空类别与箱线图结合使用。但首先它看起来很糟糕,其次它在刻面方面也同样搞砸了。基本上任何解决方法都会有所帮助。
在创建此处显示的最小示例的代码下方:
# Creating some sample data & loading libraries
library(ggplot2)
library(RColorBrewer)
set.seed(10111)
x=matrix(rnorm(40),20,2)
y=rep(c(-1,1),c(10,10))
x[y==1,]=x[y==1,]+1
x[,2]=x[,2]+20
df=data.frame(x,y)
# creating a benchmark point
benchmark=data.frame(y=rep("bm",2),key=c("X1","X2"),value=c(-0.216936,20.526312))
# melting the data frame, rbinding it with the benchmark
test_dat=rbind(tidyr::gather(df,key,value,-y),benchmark)
# Creating a plot
p_box <- ggplot(data = test_dat, aes(x=key, y=value,color=as.factor(test_dat$y))) +
geom_boxplot() + scale_color_manual(name="Cluster",values=brewer.pal(8,"Set1"))
# The first line delivers the first plot, the second line the second plot
p_box
p_box + facet_wrap(~key,scales = "free",drop = FALSE) + theme(legend.position = "bottom")
问题只出在颜色aes
里面使用了test_dat$y
。永远不要在 aes
中使用 $
,ggplot 会搞砸。
无论如何,我认为如果您使用 geom_hline
作为基准,而不是在单个值箱线图中进行黑客攻击,您的绘图会有所改进:
library(ggplot2)
library(RColorBrewer)
ggplot(tidyr::gather(df,key,value,-y)) +
geom_boxplot(aes(x=key, y=value, color=as.factor(y))) +
geom_hline(data = benchmark, aes(yintercept = value), color = '#4DAF4A', size = 1) +
scale_color_manual(name="Cluster",values=brewer.pal(8,"Set1")) +
facet_wrap(~key,scales = "free",drop = FALSE) +
theme(legend.position = "bottom")
我想比较几个变量(此处 X1
和 X2
)与单个值(此处 bm
)的分布。问题是这些变量太多(大约一打)而无法使用单个箱线图。
此外,关卡差异太大,无法使用一个地块。我需要使用 facets 让事情更有条理:
然而,对于这个图,我的基准类别 (bm
),它是 X1
和 X2
中的单个值,没有出现在 X1
中,似乎在 X2
中有多个值。我希望它只是这条绿线,它在第一个图中。任何想法为什么会改变?有什么好的解决方法吗?我尝试了 facet_wrap
/facet_grid
的选项,但没有提供正确的结果。
我还尝试将条形图与 bm
结合使用,并将三个空类别与箱线图结合使用。但首先它看起来很糟糕,其次它在刻面方面也同样搞砸了。基本上任何解决方法都会有所帮助。
在创建此处显示的最小示例的代码下方:
# Creating some sample data & loading libraries
library(ggplot2)
library(RColorBrewer)
set.seed(10111)
x=matrix(rnorm(40),20,2)
y=rep(c(-1,1),c(10,10))
x[y==1,]=x[y==1,]+1
x[,2]=x[,2]+20
df=data.frame(x,y)
# creating a benchmark point
benchmark=data.frame(y=rep("bm",2),key=c("X1","X2"),value=c(-0.216936,20.526312))
# melting the data frame, rbinding it with the benchmark
test_dat=rbind(tidyr::gather(df,key,value,-y),benchmark)
# Creating a plot
p_box <- ggplot(data = test_dat, aes(x=key, y=value,color=as.factor(test_dat$y))) +
geom_boxplot() + scale_color_manual(name="Cluster",values=brewer.pal(8,"Set1"))
# The first line delivers the first plot, the second line the second plot
p_box
p_box + facet_wrap(~key,scales = "free",drop = FALSE) + theme(legend.position = "bottom")
问题只出在颜色aes
里面使用了test_dat$y
。永远不要在 aes
中使用 $
,ggplot 会搞砸。
无论如何,我认为如果您使用 geom_hline
作为基准,而不是在单个值箱线图中进行黑客攻击,您的绘图会有所改进:
library(ggplot2)
library(RColorBrewer)
ggplot(tidyr::gather(df,key,value,-y)) +
geom_boxplot(aes(x=key, y=value, color=as.factor(y))) +
geom_hline(data = benchmark, aes(yintercept = value), color = '#4DAF4A', size = 1) +
scale_color_manual(name="Cluster",values=brewer.pal(8,"Set1")) +
facet_wrap(~key,scales = "free",drop = FALSE) +
theme(legend.position = "bottom")