2016-01-06 82 views
1

我的問題與R(3.2.3)中的ggplot2有關。我有4個數據集包含2列和約80,000-100,000行。我用下面的代碼來創建的曲線圖用於一個數據集:使用ggplot2

dataset1 <- read.table("file1.txt", header=T) 
ggplot(data=dataset1, aes(dataset1$length))+ geom_histogram (binwidth =500)+ 
scale_x_continuous(breaks=seq(300,1000,by=200),seq(1001,15000,by=1000)) 

在此,長度是我的數據集的二路柱,我想有劇情,與x軸示出300-之間的長度1200間隔200(300,500,700,900,1200)和長度1201-1500間隔1000.所以,我用上述代碼scale_x_continuous,但它沒有產生我想要的。你能幫我用這個陰謀的正確的代碼嗎?

下面是數據的簡短的樣本:

case length 
C1099757 300 
C1099759 300 
C1099761 300 
C1099763 300 
C1100993 301 
C1100995 301 
C1100997 301 
C1100999 301 
C1101377 302 
C1101379 302 
C1101919 303 
C1101921 303 
C1102979 304 
C1102981 304 
C1102983 304 
C1103475 305 
C1103477 305 
C1104267 306 
C1104269 306 

在原始數據文件,它會繼續在12000我張貼的類似的方式。在你看來,ggplot2是適合這種陰謀的,如果不是的話請建議正確的一個。

此外,我正在尋找一種方法來顯示一個圖表中所有4個數據集的長度分佈,以便於比較它們。如果您能請讓我知道我該怎麼做,我將不勝感激。

非常感謝提前。

+0

我下面提供了一個答案。但是,如果您提供了樣本數據(例如使用虹膜),則會更容易。此外你的間隔完成匹配。 – CAFEBABE

回答

3

我想你應該

scale_x_continuous(breaks=c(seq(300,1000,by=200),seq(1001,15000,by=1000))) 

scale_x_continuous(breaks=seq(300,1000,by=200)+ 
scale_y_continuous(breaks=seq(1001,15000,by=1000)) 

更換

scale_x_continuous(breaks=seq(300,1000,by=200),seq(1001,15000,by=1000)) 

根據您的樣品

(不太確定你的意思)數據我產生了一些僅包含長度

df1 = data.frame(length=runif(300,300,1200)) 
df2 = data.frame(length=runif(300,300,1200)) 
df3 = data.frame(length=runif(300,900,1200)) 
df2 = data.frame(length=runif(300,300,12000)) 
df4 = data.frame(length=runif(300,300,12000)) 

# plotting a single dataset 
ggplot(data=df4, aes(length))+ geom_histogram (binwidth =500)+ 
+  scale_x_continuous(breaks=c(seq(300,1000,by=200),seq(1001,15000,by=1000))) 
#combine the datframes 
df = data.frame(df1$length,df2$length,df3$length,df4$length) 
library(reshape) 
melted <- melt(df) 
ggplot(data=melted, aes(value))+aes(fill=variable)+ geom_histogram (binwidth =500)+ 
    scale_x_continuous(breaks=c(seq(300,1000,by=200),seq(1001,15000,by=1000))) 

enter image description here

ggplot(data=melted, aes(value))+aes(fill=variable)+ geom_histogram (binwidth =500,position="dodge")+ 
    scale_x_continuous(breaks=c(seq(300,1000,by=200),seq(1001,15000,by=1000))) 

enter image description here

rtificial數據要具有略微更好的X軸標記我​​重新分配的標籤位,並通過把它們45度

ggplot(data=melted, aes(value))+aes(fill=variable)+ geom_histogram (binwidth =500,position="dodge")+ 
    scale_x_continuous(breaks=c(seq(300,1000,by=200),seq(1100,15000,by=1000)))+theme(axis.text.x = element_text(angle = 45, hjust = 1)) 

enter image description here

並且圖表相應地調整分檔。我其實喜歡不同的酒吧大小。

ggplot(data=melted, aes(value))+ 
     aes(fill=variable)+ 
     geom_histogram(breaks=c(seq(300,1000,by=200),seq(1100,15000,by=1000)),position="dodge")+ 

     scale_x_continuous(breaks=c(seq(300,1000,by=200),seq(1100,15000,by=1000)))+ 
     theme(axis.text.x = element_text(angle = 45, hjust = 1)) 

enter image description here

+0

我使用了你的建議修改,但由於數據量很大,這個圖不是很有趣。我想附上照片,但不知道它應該如何在評論部分。 – Mary

+0

我剛剛做了兩個小小的更新來整合4個數據集問題。 你不能上傳圖像作爲評論:( 最好上傳到網絡的某個地方,並提供一個鏈接。然而,「intrestingness」可能是更多的數據問題。 – CAFEBABE

+0

非常感謝你的迴應。第一個組合圖是我想要的,但是,x軸標籤(圖的左側)很麻煩,這是由於大量的數據造成的。請您告訴我該如何清楚?對於進一步的可能是一個基本的問題,我並不完全理解「data.frame(length = runif(300,300,1200))」是什麼意思。「如果我對此有你的解釋,我會很高興的。 – Mary