R-根据因子水平拆分直方图
R- split histogram according to factor level
这是我的数据:
type<-rep(c(0,1),100)
diff<-rnorm(100)
data<-data.frame(type,diff)
如果我想绘制 diff
的直方图,我这样做:
hist(data$diff)
但是我想根据 type
分割直方图。我可以这样做:
par(mfrow=c(1,2))
hist(data$diff[data$type==0])
hist(data$diff[data$type==1])
但这给我的是两个并排的不同直方图。我想要做的是生成一个直方图,其中 diff
的一侧为 0
,另一侧的 diff
为 1
。像这样的条形图是连续的,中间没有中断或边界。这大概意味着轴将针对每个因素一分为二。
您可以使用 ggplot2
包:
library(ggplot2)
ggplot(data,aes(x=diff))+geom_histogram()+facet_grid(~type)+theme_bw()
你也可以通过"dodging"把他们放在同一个地块上:
ggplot(data,aes(x=diff,group=type,fill=type))+
geom_histogram(position="dodge",binwidth=0.25)+theme_bw()
如果你想让它们重叠,位置必须是position="identity"
ggplot(data,aes(x=diff,group=type,fill=type))+
geom_histogram(position="identity",alpha=0.5,binwidth=0.25)+theme_bw()
如果你想让它们看起来像第一个但没有边框,你必须稍微修改一下:
data$diff[data$type==1] <- data$diff[data$type==1] + 6
ggplot(data,aes(x=diff,group=type,fill=type))+
geom_histogram(position="identity",alpha=0.5,binwidth=0.25)+theme_bw()+
scale_x_continuous(breaks=c(-2:2,4:8),labels=c(-2:2,-2:2))
该图是使用 lattice 包制作的
set.seed(1)
type<-rep(c(0,1),100)
diff<-rnorm(100)
data<-data.frame(type,diff)
library('lattice')
histogram(~ diff | type, data = data)
这是在基本图形中的操作方法
## first plot - left half of x-axis, right margin set to 0 lines
par(fig = c(0, .5, 0, 1), mar = c(5,4,3,0))
hist(data$diff[data$type==0], ann = FALSE, las = 1)
## second plot - right half of x-axis, left margin set to 0 lines
par(fig = c(.5, 1, 0, 1), mar = c(5,0,3,2), new = TRUE)
hist(data$diff[data$type==1], ann = FALSE, axes = FALSE)
axis(1)
axis(2, lwd.ticks = 0, labels = FALSE)
title(main = 'Histogram', xlab = 'x label', outer = TRUE, line = -2)
这是我的数据:
type<-rep(c(0,1),100)
diff<-rnorm(100)
data<-data.frame(type,diff)
如果我想绘制 diff
的直方图,我这样做:
hist(data$diff)
但是我想根据 type
分割直方图。我可以这样做:
par(mfrow=c(1,2))
hist(data$diff[data$type==0])
hist(data$diff[data$type==1])
但这给我的是两个并排的不同直方图。我想要做的是生成一个直方图,其中 diff
的一侧为 0
,另一侧的 diff
为 1
。像这样的条形图是连续的,中间没有中断或边界。这大概意味着轴将针对每个因素一分为二。
您可以使用 ggplot2
包:
library(ggplot2)
ggplot(data,aes(x=diff))+geom_histogram()+facet_grid(~type)+theme_bw()
你也可以通过"dodging"把他们放在同一个地块上:
ggplot(data,aes(x=diff,group=type,fill=type))+
geom_histogram(position="dodge",binwidth=0.25)+theme_bw()
如果你想让它们重叠,位置必须是position="identity"
ggplot(data,aes(x=diff,group=type,fill=type))+
geom_histogram(position="identity",alpha=0.5,binwidth=0.25)+theme_bw()
如果你想让它们看起来像第一个但没有边框,你必须稍微修改一下:
data$diff[data$type==1] <- data$diff[data$type==1] + 6
ggplot(data,aes(x=diff,group=type,fill=type))+
geom_histogram(position="identity",alpha=0.5,binwidth=0.25)+theme_bw()+
scale_x_continuous(breaks=c(-2:2,4:8),labels=c(-2:2,-2:2))
该图是使用 lattice 包制作的
set.seed(1)
type<-rep(c(0,1),100)
diff<-rnorm(100)
data<-data.frame(type,diff)
library('lattice')
histogram(~ diff | type, data = data)
这是在基本图形中的操作方法
## first plot - left half of x-axis, right margin set to 0 lines
par(fig = c(0, .5, 0, 1), mar = c(5,4,3,0))
hist(data$diff[data$type==0], ann = FALSE, las = 1)
## second plot - right half of x-axis, left margin set to 0 lines
par(fig = c(.5, 1, 0, 1), mar = c(5,0,3,2), new = TRUE)
hist(data$diff[data$type==1], ann = FALSE, axes = FALSE)
axis(1)
axis(2, lwd.ticks = 0, labels = FALSE)
title(main = 'Histogram', xlab = 'x label', outer = TRUE, line = -2)