簡體   English   中英

了解 facet_grid scale=“free”

[英]Understanding facet_grid scale=“free”

我想將 facet_grid 與 stat_compare_means 一起使用,並為 y 設置單獨的比例。 我認為 scale = "free" 會處理它,但事實並非如此。 我的名稱因子中的“B”級未正確縮放。

library(tidyverse) 
library(ggpubr) 

set.seed(2)

my_comparisons <- list(c("Cond1","Cond2"),
                        c("Cond2","Cond3"),
                        c("Cond1","Cond3"))

nrName  <- 4
nrCond  <- 3
nrValue <- 5

Name      <-  rep(c("A","B","C","D"),each=nrValue*nrCond)
Condition <-  rep(c("Cond1","Cond2","Cond3"),length.out=nrName*nrCond*nrValue)
Score     <-  c(rnorm(nrValue*nrCond,6,1),rnorm(nrValue*nrCond,0.01,0.01),rnorm(nrValue*nrCond,7,1),rnorm(nrValue*nrCond,7,1))

df <- data.frame(Name = Name, Condition = Condition, Score = Score)

 plt <- ggplot(df,
             aes(x= Condition,y= Score, fill= Condition))+  
   #geom_flat_violin(position = position_nudge(x = .2),adjust = 2)+
   geom_point(position = position_jitter(width = .15), size = .25)+
   geom_boxplot(outlier.shape = NA, alpha = 0.3, width = .1, colour = "BLACK") +
   facet_grid(reformulate("Name","."),scales = 'free')+
   scale_y_continuous(expand = expansion(mult = c(0, 0.1)))+
   
   stat_compare_means(
     comparisons = my_comparisons,
     method = "t.test",paired = TRUE,
     label.y.npc = 0.1)

print(plt)

使用 stat_compare_means

我認為這可能與 stat_compare_means 中的標簽有關。 但是刪除 stat_compare_means 會產生類似的結果。

沒有 stat_compare_means

我在這里沒有考慮什么?

我現在看到了我的困惑。 正如 RonakShah 正確指出的那樣,我在原始代碼中使用了 PupillometryR 中的 geom_flat_violin。 當我在這里寫這篇文章時,我想簡化問題,這就是我展示箱線圖的原因。 如果我重新添加帶有 geom_flat_vioilin 的行,並按照 Suren 的建議使用 greom_wrap,則 scale = "free" 選項不再起作用。

ggplot(df,
         aes(x= Condition,y= Score,fill= Condition))+  
geom_flat_violin(position = position_nudge(x = .2))+
geom_point(position = position_jitter(width = .15), size = .25)+
geom_boxplot(outlier.shape = NA, alpha = 0.3, width = .1, colour = "BLACK") +
facet_wrap(reformulate("Name","."),scales = 'free',nrow = 1)+
scale_y_continuous(expand = expansion(mult = c(0, 0.1)))

Geom_flat_violin + geom_wrap

我想我必須查看 geom_flat_violin function 以進行進一步調試。

例如,而不是facet_grid使用facet_wrap

facet_wrap(reformulate("Name","."), scales = 'free', nrow = 1) +

使用facet_grid不能同時獲得 x 和 y 尺度; 見這里https://github.com/tidyverse/ggplot2/issues/1152

暫無
暫無

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

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