This is my data:
这是我的数据:
type<-rep(c(0,1),100)
diff<-rnorm(100)
data<-data.frame(type,diff)
If I want to plot historgram of diff
, I do this:
如果我想绘制差异的历史图,我这样做:
hist(data$diff)
But what I want to do to split my histogram according to type
. I could do this:
但是我想根据类型分割直方图。我能做到这一点:
par(mfrow=c(1,2))
hist(data$diff[data$type==0])
hist(data$diff[data$type==1])
But what this is giving me are two different histograms side by side. What I want to do is produce a single histogram with diff
of 0
at one side and diff
of 1
at other side. Something like this with bars as continuous without the breaks or border in between. This presumably would mean the axis will be split into two for each factor.
但这给我的是两个不同的直方图。我想要做的是产生一个直方图,一边是diff,另一边是diffof 1。像这样的东西,条形连续,没有断裂或边界。这可能意味着轴将被分成两个因子。
2 个解决方案
#1
17
You can use the ggplot2
package:
您可以使用ggplot2包:
library(ggplot2)
ggplot(data,aes(x=diff))+geom_histogram()+facet_grid(~type)+theme_bw()
You can also put them on the same plot by "dodging" them:
您也可以通过“躲避”它们将它们放在同一个地块上:
ggplot(data,aes(x=diff,group=type,fill=type))+
geom_histogram(position="dodge",binwidth=0.25)+theme_bw()
If you want them to overlap, the position has to be position="identity"
如果你想让它们重叠,那么位置必须是position =“identity”
ggplot(data,aes(x=diff,group=type,fill=type))+
geom_histogram(position="identity",alpha=0.5,binwidth=0.25)+theme_bw()
If you want them to look like it does in the first one but without the border, you have to hack it a little:
如果你希望它们看起来像第一个但没有边框,你必须破解它:
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))
#2
7
That plot was made using the lattice package
该图是使用格子包制作的
set.seed(1)
type<-rep(c(0,1),100)
diff<-rnorm(100)
data<-data.frame(type,diff)
library('lattice')
histogram(~ diff | type, data = data)
here is how you can do it in base graphics
这是你如何在基本图形中做到这一点
## 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)
#1
17
You can use the ggplot2
package:
您可以使用ggplot2包:
library(ggplot2)
ggplot(data,aes(x=diff))+geom_histogram()+facet_grid(~type)+theme_bw()
You can also put them on the same plot by "dodging" them:
您也可以通过“躲避”它们将它们放在同一个地块上:
ggplot(data,aes(x=diff,group=type,fill=type))+
geom_histogram(position="dodge",binwidth=0.25)+theme_bw()
If you want them to overlap, the position has to be position="identity"
如果你想让它们重叠,那么位置必须是position =“identity”
ggplot(data,aes(x=diff,group=type,fill=type))+
geom_histogram(position="identity",alpha=0.5,binwidth=0.25)+theme_bw()
If you want them to look like it does in the first one but without the border, you have to hack it a little:
如果你希望它们看起来像第一个但没有边框,你必须破解它:
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))
#2
7
That plot was made using the lattice package
该图是使用格子包制作的
set.seed(1)
type<-rep(c(0,1),100)
diff<-rnorm(100)
data<-data.frame(type,diff)
library('lattice')
histogram(~ diff | type, data = data)
here is how you can do it in base graphics
这是你如何在基本图形中做到这一点
## 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)