簡體   English   中英

R:將 facet_grid() 對齊到網格的頂部

[英]R: Align facet_grid() to the top of the grid

我正在努力嘗試在 R 中格式化繪圖的 facet_grid。這是我的代碼的一個可重現示例,其數據來自 R (ToothGrowth) 中的數據集包。

library(ggplot2)

df <- ToothGrowth

bp <- ggplot(df, aes(x=dose, y=len, group=dose)) +
             geom_boxplot(aes(fill=dose)) +   
             facet_grid(supp ~ .) + 
             theme_minimal(base_size = 16) +   
             theme(legend.position = "none",
                   axis.title.x = element_blank(),
                   axis.title.y = element_blank(),
                   axis.ticks = element_blank(),
                   panel.grid.major.x = element_line(size = 0.25,
                                                     colour = "grey80"),
                   strip.text = element_text(size = 22, face = "bold"))

bp

使用代碼我獲得了第一張圖片:

用代碼獲得的圖像

我想將 facet_grid 的標簽與網格圖例的頂部對齊,如第二張圖片所示:

我想要達到的目標

謝謝。

將以下內容添加到您的繪圖代碼中:

theme(strip.text.y=element_text(hjust=0))

如果要旋轉標簽,可以執行以下操作:

theme(strip.text.y=element_text(vjust=1, angle=0))

(是的, vjusthjust事情令人困惑)

p1 = bp + theme(strip.text.y=element_text(hjust=0))
p2 = bp + theme(strip.text.y=element_text(vjust=1, angle=0))

在此處輸入圖片說明

暫無
暫無

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

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