【问题标题】:Overlapping histograms on one page in RR中一页上的重叠直方图
【发布时间】:2017-08-31 13:22:18
【问题描述】:

我是 R 的初学者。我设法将我的数据绘制成重叠的直方图。但是,我想将所有直方图放在一页上。我正在苦苦挣扎,因为我无法告诉 R,选择哪一个(只能设法绘制其中一个图)。

这是代码:

        df<-read.csv("Salt dshalo sizes.csv",header=T)
    #View(df)

    library(ggplot2)

    DSA<-df[,1]
    DS1<-df[,2]
    DSB<-df[,5]
    DS2<-df[,6]
    DSC<-df[,9]
    DS3<-df[,10]

    #remove the NA column by columns separately or it will chop the data
    DSA=na.omit(DSA)
    DS1=na.omit(DS1)
    DSB=na.omit(DSB)
    DS2=na.omit(DS2)
    DSC=na.omit(DSC)
    DS3=na.omit(DS3)


    #plot histograms for DSA, DSB and DSC on one same graph 
    hist(DSA, prob=TRUE, main="Controls", xlab="Sizes (um)", ylab="Frequency", col="yellowgreen",xlim= c(5,25), ylim=c(0,0.5), breaks=10)
    hist(DSB, prob=TRUE, col=rgb(0,0,1,0.5),add=T)
    hist(DSC, prob=TRUE, col=rgb(0.8,0,1,0.5),add=T)
    #add a legend to the histogram 
    legend("topright", c("Control 1", "Control2", "Control3"), text.width=c(1,1,1),lwd=c(2,2,2), 
           col=c(col="yellowgreen", col="blue", col="pink",cex= 1))
    box()

 #plot histograms for DS1, DS2 and DS3 on one same graph 
    hist(DS1, prob=TRUE, main="Monoculture Stressed", xlab="Sizes (um)", ylab="Frequency", col="yellowgreen",xlim= c(5,25), ylim=c(0,0.5), breaks=10)
    hist(DS2, prob=TRUE, col=rgb(0,0,1,0.5),add=T)
    hist(DS3, prob=TRUE, col=rgb(0.8,0,1,0.5),add=T)
    #add a legend to the histogram 
    legend("topright", c("DS1", "DS2", "DS3"), text.width=c(1,1,1),lwd=c(2,2,2), 
           col=c(col="yellowgreen", col="blue", col="pink",cex= 1))
    box()

# put both overlapping histograms onto one page
    combined <- par(mfrow=c(1, 2))
    plot(hist(DSA),main="Controls")
    plot(hist(DS1),main="Monoculture stressed")
    par(combined)

基本上,我得到两个独立的重叠直方图,但不能将它们放在同一页面上。

【问题讨论】:

  • 如果您正在使用 base hist(),请调用 par(mfrow=c(a,b)) 其中 a = 图表的行数,b =图表的列数。还是您的意思是在单个图表上实际显示重叠?
  • 您好,感谢 par 命令。它确实有效,现在我遇到了传说被压扁的问题。有没有办法在页面上放置图例?
  • 嗨,这是我遵循的示例,但是我对图例有疑问。

标签: r histogram


【解决方案1】:

编辑:我显然没有仔细阅读你的问题。我看到你想出了 add =T。

我假设您正在寻找的是我首先发表的评论:

par(mfrow = c(a,b)) 其中 a 和 b 是您希望打印图形对象的行数和列数。我在这张照片中使用了 c(2,2)。

我发表了评论,但听起来您可能在谈论 add=T 选项。

a=rnorm(100, 2, 1)
b=rnorm(100, 4, 1)
hist(a, xlim=c(0,10), col="yellow")
hist(b, add=T, col="purple" )

您可以使用颜色的透明度选项来查看两者的重叠。比如 rgb(1,0,0,1/4) 作为颜色。

使用透明色:

a=rnorm(100, 2, 1)
b=rnorm(100, 4, 1)
hist(a, xlim=c(0,10), col=rgb(1,1,0,1/4))
hist(b, add=T, col=rgb(1,0,0,1/4) )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 2020-02-23
    • 1970-01-01
    • 2020-12-31
    • 2013-01-15
    • 2021-11-24
    • 2016-12-19
    相关资源
    最近更新 更多