簡體   English   中英

在 ggplot2 中手動中斷 facet_wrap()

[英]Manual breaks of facet_wrap() in ggplot2

問題

我試圖了解如何手動為刻面提供 y 軸中斷,刻面是由Rggplot2中的facet_wrap()函數生成的。

最小的可重復示例

免責聲明:我從R studio 社區借用了以下示例

使用以下代碼,您可以根據 de 數據集中的 y 軸值指定 y 軸中斷。

library(ggplot2)
diamonds2 <- subset(diamonds, !(cut == "Fair" & price > 5000))

my_breaks <- function(x) { if (max(x) < 6000) seq(0, 5000, 1000) else seq(0, 15000, 5000) }

ggplot(data = diamonds2, aes(x = carat, y = price)) +
  facet_wrap(~ cut, scales = "free_y") +
  geom_point() +
  scale_y_continuous(breaks = my_breaks)

在此處輸入圖片說明

我希望能夠根據facet手動指定中斷(在這種情況下:基於鑽石的“切割”)。 例如,我想將 'Fair' 剪輯的中斷設置為seq(1000, 2500, 5000) ,將 'Good' 剪輯的中斷設置為seq(1500, 3000, 4500, 6000, ..., 15000)其余為seq(0,15000, 5000)

試圖

我認為用 if-else-ladder 調整my_breaks函數,指定“cut”可以解決問題,但是,它沒有:

my_breaks <- function(x) { 
  if (cut == "Fair") {seq(0, 5000, 1000) }
  else if (cut == "Good") {seq(1500,15000, 1500)}
  else { seq(0, 15000, 5000) }
}

它提供了錯誤:

cut == "Fair" 中的錯誤:比較 (1) 僅適用於原子和列表類型

是否有另一種方法可以手動為不同方面提供中斷?

嘗試通過過濾數據手動定義每個方面,然后使用patchwork包“修補”繪圖

library(ggplot2)
library(patchwork)

my_plot <- function(df, var, sc) {
  ggplot(data = df, aes(x = carat, y = price)) +
  facet_grid(~ {{var}}) +
  geom_point() +
  coord_cartesian(ylim = {{sc}})
}

diamonds2 <- subset(diamonds, !(cut == "Fair" & price > 5000))

p1 <- diamonds2 %>% filter(cut == "Fair") %>% my_plot(var = "Fair",  sc = c(0, 5000))
p2 <- diamonds2 %>% filter(cut == "Good") %>% my_plot(var = "Good",  sc = c(0, 15000))
p3 <- diamonds2 %>% filter(cut == "Very Good") %>% my_plot(var = "Very Good",  sc = c(0, 1000))
p4 <- diamonds2 %>% filter(cut == "Premium") %>% my_plot(var = "Premium",  sc = c(0, 500))
p5 <- diamonds2 %>% filter(cut == "Ideal") %>% my_plot(var = "Ideal",  sc = c(0, 20000))

p1+p2+p3+p3+p4+plot_layout(ncol = 3)

在此處輸入圖片說明

編輯:我意識到這是要設置 pr 的 y 軸中斷。 方面。 在這種情況下

my_plot <- function(df, var, my_breaks) {
  ggplot(data = df, aes(x = carat, y = price)) +
  facet_grid(~ {{var}}) +
  geom_point() +
  scale_y_continuous(breaks = {{my_breaks}})
}

library(ggplot2)
diamonds2 <- subset(diamonds, !(cut == "Fair" & price > 5000))

p1 <- diamonds2 %>% filter(cut == "Fair") %>% my_plot(var = "Fair",  my_breaks = seq(0, 5000, 1000))
p2 <- diamonds2 %>% filter(cut == "Good") %>% my_plot(var = "Good",  my_breaks = seq(1500,15000, 1500))
p3 <- diamonds2 %>% filter(cut == "Very Good") %>% my_plot(var = "Very Good",  my_breaks =seq(1500,15000, 5000))
p4 <- diamonds2 %>% filter(cut == "Premium") %>% my_plot(var = "Premium",  my_breaks = seq(1500,15000, 5000))
p5 <- diamonds2 %>% filter(cut == "Ideal") %>% my_plot(var = "Ideal",  my_breaks = seq(1500,15000, 5000))

p1+p2+p3+p3+p4+plot_layout(ncol = 3)

在此處輸入圖片說明

編輯 2:使用 purrr 執行此操作的更精簡方法

library(ggplot2)
library(patchwork)
library(purrr)
facets = diamonds2 %>% distinct(cut) %>% pull()
my_breaks = list(seq(0, 5000, 1000), seq(1500,15000, 1500), seq(1500,15000, 5000),
                 seq(1500,15000, 5000), seq(1500,15000, 5000))

plt_list <- purrr::map2(facets, my_breaks, ~my_plot(diamonds2, .x, .y))

Reduce('+', plt_list) + plot_layout(ncol = 3)

暫無
暫無

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

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