2017-08-31 117 views
0

我是R的初学者。我设法将我的数据绘制成重叠直方图。不过,我想将所有的直方图放在一个页面上。我挣扎着,因为我无法告诉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) 

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

+0

如果您正在使用的基础HIST(),你似乎做,做同等的呼叫(mfrow = C(A ,b))其中a =图的行数,b =图的列数。或者你的意思是真的要在单个图表上显示重叠? – akaDrHouse

+0

你好,感谢par命令。它确实有效,现在我遇到了传言被挤压的问题。有什么方法可以在页面上放置图例? – Orbis

+0

看看这个。 https://stackoverflow.com/a/10391001/4001897 – akaDrHouse

回答

1

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

我假设你正在寻找然后就是我第一次提出的意见:

par(mfrow = c(a,b))其中A和B是行数和列数你想要的图形对象进行打印。我用c(2,2)来拍这张照片。

enter image description here

我做了一个评论,但听起来好像你可以谈论附加= 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)作为颜色。

enter image description here

随着透明度的颜色:

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)) 

enter image description here