2015-07-20 75 views
1

我想在同一個窗口中繪製3個繪圖。每個將有不同數量的條形圖。我怎樣才能讓它們在大小相同的情況下彼此靠近(距離彼此相同),而無需在較小的條形圖中使用NA。下面的示例代碼。我想指出我的真實數據將繪製來自數據框$ column的數字,而不是如下所示的數字矢量。我相信有這樣做的神奇方式,但似乎無法找到有用的信息在網絡上。感謝如何使用barplot函數在R中的繪圖窗口中製作相同大小的barplot酒吧

pdf(file="PATH".pdf"); 
par(mfrow=c(1,3)); 
par(mar=c(9,6,4,2)+0.1); 
barcenter1<- barplot(c(1,2,3,4,5)); 
mtext("Average Emergent", side=2, line=4); 
par(mar=c(9,2,4,2)+0.1); 
barcenter2<- barplot(c(1,2,3)); 
par(mar=c(9,2,4,2)+0.1); 
barcenter3<- barplot(c(1,2,3,4,5,6,7)); 

還是會有一種方法,而不是使用標準桿(mfrow ....),使情節窗口,可能我們小組與欄之間的空白空間的單一情節barcenter數據?這樣一切都有間隔,看起來一樣嗎?

回答

2

使用參數xlimwidth

par(mfrow = c(1, 3)) 
    par(mar = c(9, 6, 4, 2) + 0.1) 
    barcenter1 <- barplot(c(1, 2, 3, 4, 5), xlim = c(0, 1), width = 0.1) 
    mtext("Average Emergent", side = 2, line = 4) 
    par(mar = c(9, 2, 4, 2) + 0.1) 
    barcenter2 <- barplot(c(1, 2, 3), xlim = c(0, 1), width = 0.1) 
    par(mar = c(9, 2, 4, 2) + 0.1) 
    barcenter1 <- barplot(c(1, 2, 3, 4, 5, 6, 7), xlim = c(0, 1), width = 0.1) 

enter image description here

介紹零:

df <- data.frame(barcenter1 = c(1, 2, 3, 4, 5, 0, 0), 
       barcenter2 = c(1, 2, 3, 0, 0, 0, 0), 
       barcenter3 = c(1, 2, 3, 4, 5, 6, 7)) 
barplot(as.matrix(df), beside = TRUE) 

enter image description here

隨着ggplot2你CA ñ得到這樣的:

df <- data.frame(x=c(1, 2, 3, 4, 5,1, 2, 3,1, 2, 3, 4, 5, 6, 7), 
y=c(rep("bar1",5), rep("bar2",3),rep("bar3",7))) 
library(ggplot2) 
ggplot(data=df, aes(x = x, y = x)) + 
    geom_bar(stat = "identity")+ 
    facet_grid(~ y) 

enter image description here

爲了在你的第二個評論中提到的選項,你將需要:

x <- c(1, 2, 3, 4, 5, NA, 1, 2, 3, NA, 1, 2, 3, 4, 5, 6, 7) 
barplot(x) 

enter image description here

+0

@ mpalanco,將工作,但會喜歡爲出版的理由保持密切合作 – user3919708

+0

@ mpalanco這將工作,但仍然不是我想到的。無論如何,我只是再次編輯它。如何在每個組之間的空白區域上繪製所有關於barplot的內容? – user3919708

+0

@ user3919708如果這個或任何答案解決了您的問題,請考慮通過點擊複選標記來接受它。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 – mpalanco