2016-08-21 109 views
-2

我已經創建了這個情節: -定製/刻度軸刻度標記爲4繪出彼此獨立的在facet_wrap

1

使用此代碼: -

ggplot(data = gdt, aes(x = area)) + 
geom_histogram(bins = 10, colour = "black", fill = "grey50") + 
facet_wrap(~ fires, scales = "free") + 
labs(x = "Area of Burnt Land (ha)", y = "Fires") + 
ggtitle("Count of Destructive Fires in Portugal (2007)") 

我想改變每個子圖的單獨刻度標記的間隔發生或位置。換句話說,我試圖在酒吧見面的每個點上放置一個刻度線/網格線。我曾嘗試使用scale_continuous和scale_x_discrete無效。如果我將分數設置爲適合其中一個分區,則會對其他分區產生負面影響。有沒有辦法做到這一點??

+0

不知道'ggplot :: facet_x'是否可以做到這一點。但是,您可以通過'cowplot :: plot_grid'構建一個類似的顯示,並帶有4個單獨的圖表 – Nate

+0

您需要特定的刻度線位置,但我沒有看到具體的內容? –

回答

0

有兩個部分你的問題:

  1. 「實現在酒吧滿足每一個點一個週期/網格線的位置」,並
  2. 積4分地塊。

主要的想法是與scale_x_continuous使用的binwidthboundary參數的組合geom_histogram一起,以獲得與杆對齊網格線;然後使用gridExtra包中的grid.arrange將曲線排列在同一頁上。

一些假的數據:

library(ggplot2) 
library(gridExtra) 
d <- data.frame(x = c(runif(100, 0, 1), 
         runif(500, 1, 10), 
         runif(100, 10, 50), 
         runif(100, 50, 1500))) 
cuts <- c(0, 1, 10, 50, 1500) 
d$type <- cut(d$x, cuts) 

創建的ggplot秒的列表,每個ggplot表示數據的一個子集:

g <- lapply(split(d, d$type), function(dd) { 
    i <- as.numeric(dd$type)[1] 
    bw <- (cuts[i + 1] - cuts[i])/10 
    ggplot(dd, aes(x = x)) +  
     geom_histogram(bins = 10, colour = "black", fill = "grey50", 
        binwidth = bw, boundary = cuts[i]) + 
     scale_x_continuous(breaks = seq(cuts[i], cuts[i + 1], bw)) + 
     labs(y = "", x = "") + 
     facet_wrap(~ type, scales = "free") # just to create panel 
}) 

安排頁面上的次要情節,並添加標籤:

params <- list(top = "Count of Destructive Fires in Portugal (2007)", 
       left = "Fires", 
       bottom = "Area of Burnt Land (ha)") 
do.call(grid.arrange, c(g, params)) 

enter image description here