简体   繁体   English

了解 facet_grid scale=“free”

[英]Understanding facet_grid scale=“free”

I want to use facet_grid with stat_compare_means and have individual scales for y.我想将 facet_grid 与 stat_compare_means 一起使用,并为 y 设置单独的比例。 I thought scale = "free" would take care of it, but it doesn't.我认为 scale = "free" 会处理它,但事实并非如此。 Level "B" in my Name factor is not scaled properly.我的名称因子中的“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

I thought it might be related to the labels in stat_compare_means.我认为这可能与 stat_compare_means 中的标签有关。 But removing stat_compare_means yields similar result.但是删除 stat_compare_means 会产生类似的结果。

没有 stat_compare_means

What am I not considering here?我在这里没有考虑什么?

I see my confusion now.我现在看到了我的困惑。 As RonakShah pointed out correctly, I use geom_flat_violin from the the PupillometryR in my original code.正如 RonakShah 正确指出的那样,我在原始代码中使用了 PupillometryR 中的 geom_flat_violin。 I wanted to simplify the problem when I wrote the post here which is the reason why I showed the boxplots.当我在这里写这篇文章时,我想简化问题,这就是我展示箱线图的原因。 If I add the line with the geom_flat_vioilin back in, and use greom_wrap as suggested by Suren, the scale = "free" option doesn't work anymore.如果我重新添加带有 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

I guess I have to check out the geom_flat_violin function for further debuging.我想我必须查看 geom_flat_violin function 以进行进一步调试。

Instead of facet_grid use facet_wrap for example,例如,而不是facet_grid使用facet_wrap

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

With facet_grid one can not get both x and y scales free;使用facet_grid不能同时获得 x 和 y 尺度; see here https://github.com/tidyverse/ggplot2/issues/1152见这里https://github.com/tidyverse/ggplot2/issues/1152

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM