Soma
Soma

Reputation: 329

how can i show two histogram in same window but different plots in R?

I want to show the effect of removing outliers on my histogram, so I have to plot both hists together.

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

so i tried this:

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)

the first code give me to box plot, but I tried it for hist it doesn't work can anyone tell me how to do it in R?

Upvotes: 3

Views: 884

Answers (2)

G5W
G5W

Reputation: 37641

For a base R solution, use par with mfrow.

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

Upvotes: 5

Bill Chen
Bill Chen

Reputation: 1749

For multiple plots, you should use ggplot2 with facet_wrap. Here is an example:

Plot several histograms with ggplot in one window with several variables

Upvotes: 2

Related Questions