簡體   English   中英

在ggplot2中為每個構面設置不同的軸限制,而不使用scales =“ free”

[英]Setting different axis limits for each facet in ggplot2 not using scales = “free”

我想要的一般解決方案是能夠為每個構面單獨指定任意軸限制。

通過將比例尺設置為免費可獲得基本功能。 例如:

ggplot(diamonds, aes(x = carat, y = price)) + geom_point() + facet_wrap(~clarity, nrow = 4, ncol = 2, scales = "free")

這實際上是一個非常不錯的功能,但實際上並不總是那么有用。 通常,我們想要的是在同一軸上具有可比較的變量子組。 作為玩具示例,請考慮上面的鑽石盒。 我們可能希望第一列中的所有構面都具有相同的軸限制,而第二列中的所有構面都具有相同的軸限制(但與第一列不同)。

是否有使用標准 ggplot使用來完成此任務的解決方案。

在結束之前,我認為擴展@Axeman的建議很重要:直接使用facet_wrap可能無法實現,但是可以通過對所需的組進行分塊並將其與cowplot縫合在一起來cowplot 在這里,我分為“低”質量和“高”質量,但是分組是任意的,可以隨心所欲。 可能想稍微改變一下樣式,但是cowplot可以接受cowplot的默認設置:

library(cowplot)

lowQ <- 
  ggplot(diamonds %>%
           filter(as.numeric(clarity) <= 4)
         , aes(x = carat
               , y = price)) +
  geom_point() +
  facet_wrap(~clarity
             , nrow = 1)  


hiQ <- 
  ggplot(diamonds %>%
           filter(as.numeric(clarity) > 4)
         , aes(x = carat
               , y = price)) +
  geom_point() +
  facet_wrap(~clarity
             , nrow = 1)

plot_grid(lowQ, hiQ, nrow = 2)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM