【问题标题】:how can i show two histogram in same window but different plots in R?如何在同一窗口中显示两个直方图,但在 R 中显示不同的图?
【发布时间】:2020-02-23 06:15:09
【问题描述】:

我想在直方图上显示去除异常值的效果,所以我必须将两个直方图绘制在一起。

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 中做到这一点?

【问题讨论】:

    标签: r plot histogram


    【解决方案1】:

    对于多个绘图,您应该使用ggplot2facet_wrap。这是一个例子:

    Plot several histograms with ggplot in one window with several variables

    【讨论】:

    • 我以前看过这个库,但无法理解它是如何工作的,因为在这个示例中两个数据粘在一起然后显示。我希望找到更简单的东西或了解它如何与单个直方图一起工作
    • @ssssoooo,如果您使用ggplot2,您确实需要将两个表与数据框堆叠在一起。您的标签将是:值 |标签 xx |费用 xx |费用... xx |费用1 xx | Costs1 ....那么你facet_wrap(~label),ggplot二就会知道你要使用label作为facet条件。
    【解决方案2】:

    对于基本 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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      相关资源
      最近更新 更多