簡體   English   中英

如何訂購具有兩個因子變量的 facet_wrap?

[英]How to order facet_wrap that has two factor variables?

library(ggplot)
library(ggforce)

我正在為 23 個國家/地區繪制一些估計值。 正如您在下面的 plot 中看到的,這些國家/地區是隨機繪制的。 我想從widening gap組的國家開始訂購它們,然后是no changeContracting gap

我在這里嘗試遵循這個建議: 控制 ggplot2 中的 facet_grid/facet_wrap 的順序? ,但由於某些原因它不起作用。

此外,如果可能的話,我希望每個組中的所有國家都使用一個詞,而不是一次又一次地顯示每個國家的“差距擴大”。 我嘗試使用ggforce來做到這一點,但沒有繪制國家/地區。

有人可以幫忙嗎?

這是代碼:

 df %>%
  ggplot(aes(x= estimate, y=term)) +
  geom_point(mapping=aes(x=estimate, y=term), size=2.3, shape=21, fill="black") +
  geom_vline(xintercept=0, color = "black", linetype= "dotted") +
  facet_wrap(vars(cntry,cat_f), scales = "free_x", strip.position = "bottom")

這是我的 plot:

在此處輸入圖像描述

這是一個示例,我嘗試使用 ggforce 將其分為三類。 但是,一旦我分為三類,它就不是在繪制國家

df %>%
  ggplot(aes(x= estimate, y=term)) +
  geom_point(mapping=aes(x=estimate, y=term), size=2.3, shape=21, fill="black") +
  geom_vline(xintercept=0, color = "black", linetype= "dotted") +
  facet_wrap( ~ cat_f + cntry,  scales = "free_x", strip.position = "bottom") +
  ggforce::facet_row(vars(cat_f), scales = 'free_x', strip.position = 'bottom')

在此處輸入圖像描述

這是數據:

structure(list(cntry = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 
4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 
12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L, 18L, 
18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L, 23L), .Label = c("Austria", 
"Belgium", "Switzerland", "Czech Republic", "Germany", "Denmark", 
"Estonia", "Greece", "Spain", "Finland", "France", "Hungary", 
"Ireland", "Iceland", "Italy", "Luxembourg", "Netherlands", "Norway", 
"Poland", "Portugal", "Sweden", "Slovakia", "United Kingdom"), class = "factor"), 
    estimate = c(-0.0913140419553834, -0.118306201172409, -0.0239667799007915, 
    -0.0443156002183645, -0.0104216581052402, -0.0548359509121889, 
    0.0131305888416785, 0.0315223511324752, -0.0190273624021191, 
    -0.0186414728272051, 0.0411967940816404, 0.0231043131714301, 
    -0.0171116200795716, -0.0154689392721703, 0.030292299601443, 
    0.0169741956257371, -0.105814083412474, -0.140455990414314, 
    0.0647774293035408, 0.0563998778231824, -0.0235327514882885, 
    0.00143103206722573, 0.000657080813472903, -0.00615188723709141, 
    -0.0262666239250381, 0.0217722749404904, 0.0056080759613924, 
    -0.0717368274077352, 0.00814944419371703, -0.0994925665252663, 
    0.0036407509645274, 0.0265557284220869, -0.0158617373908501, 
    -0.0238087514417458, 0.0167937459496994, -0.00390159593739075, 
    0.0535891490419863, 0.0127836449750844, 0.0594230533290278, 
    0.0292983217374143, -0.0240741202575357, -0.0210028856455069, 
    0.0442436113476246, 0.0569644358417757, 0.0137935174123489, 
    0.00173110517002347), cat_f = structure(c(1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 
    1L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 1L, 1L, 
    1L, 1L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 2L, 2L), .Label = c("Widening gap", 
    "No change", "Contracting gap"), class = "factor"), term = c("year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3", 
    "year2008:occup3", "year2009:occup3", "year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3", 
    "year2008:occup3", "year2009:occup3", "year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3", 
    "year2008:occup3", "year2009:occup3", "year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3", 
    "year2008:occup3", "year2009:occup3", "year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3", 
    "year2008:occup3", "year2009:occup3", "year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3", 
    "year2008:occup3", "year2009:occup3", "year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3", 
    "year2008:occup3", "year2009:occup3", "year2008:occup3", 
    "year2009:occup3", "year2008:occup3", "year2009:occup3")), row.names = c(NA, 
-46L), class = "data.frame")

要訂購它們,請嘗試像這樣更改 facet_wrap:

facet_wrap( ~ cat_f + cntry, scales = "free_x", strip.position = "bottom")

要刪除標簽,您可以使用strip.text.x刪除兩者,然后使用geom_text將國家名稱添加到圖中:

theme(strip.text.x = element_blank()) + geom_text(aes(label = cntry, x = Inf, y = Inf), vjust = 2, hjust = 2)

您必須根據 plot 的最終尺寸調整 arguments x、y、vjust 和 hjust。 在此處輸入圖像描述


我的建議是將有關間隙的信息顯示為顏色和形狀,如下面的 plot:

ggplot(dt, aes(x= estimate, y=term)) +
  geom_point(mapping=aes(x=estimate, y=term, shape = cat_f, color = cat_f), size=3) +
  geom_vline(xintercept=0, color = "black", linetype= "dotted") +
  facet_wrap( ~ cat_f + cntry, scales = "free_x", strip.position = "bottom") +
  theme(strip.text.x = element_blank()) + theme(legend.position = c(0.8,0.1)) +
  geom_text(aes(label = cntry, x =  -Inf, y = Inf), vjust = 2, hjust = -1) 

在此處輸入圖像描述

暫無
暫無

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

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