如何在同一窗口中显示两个直方图,但在R中显示不同的图?

时间:2019-10-27 20:37:33

标签: r plot histogram

我想展示在直方图上消除离群值的效果,因此必须将两个组图一起绘制。

boxplot(Costs, Costs1,
    xlab=" Costs    and    Costs after  removig outliers",
    col=topo.colors(2))

所以我尝试了这个:

hist(Costs,Costs1,main="Histogram of  Maintenance_cost ",col="blue",
 border="darkblue",xlab="Total_cost",ylab=" ",yaxt = 'n',
 #ylim=c(0,3000),
 #xlim=c(0,max(My_Costs)),
 breaks=60)

第一个代码将我带到箱形图上,但是我试了一下,因为它不起作用 谁能告诉我在R中怎么做?

2 个答案:

答案 0 :(得分:3)

对于基本的R解决方案,请将parmfrow一起使用。

set.seed(1234)
Costs = rnorm(5000, 100, 20)
OUT = which(Costs %in% boxplot(Costs, plot=FALSE)$out)
Costs1 = Costs[-OUT]

par(mfrow=c(1,2), mar=c(5,1,2,1))
hist(Costs,main="Histogram of  Maintenance_cost ",col="blue",
 border="darkblue",xlab="Total_cost",ylab=" ",yaxt = 'n',
 breaks=60, xlim=c(30,170))
hist(Costs1,main="Maintenance_cost without outliers",col="blue",
 border="darkblue",xlab="Total_cost",ylab=" ",yaxt = 'n',
 breaks=60, xlim=c(30,170))

Histograms with and without outliers

答案 1 :(得分:1)

对于多个绘图,应将find_ifstd::for_each一起使用。这是一个示例:

Plot several histograms with ggplot in one window with several variables