2017-08-31 114 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