为什么这个facet

编程入门 行业动态 更新时间:2024-10-26 18:16:23
本文介绍了为什么这个facet_grid不会删除列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

tdat = structure(list(Condition = structure(c(1L,3L,2L, 1L,3L,2L, 1L,3L,2L,1L,3L,2L,1L,3L,2L,1L,3L,2L,1L,3L,2L,1L, 3L, (1L,1L,3L,2L),.Label = c(AS,Dup,MCH),class =factor), variable = 1L,1L,1L,1L,1L, 2L,2L,2L,2L,2L,2L,2L,2L,2L,3L,3L,3L,3L,3L,3L, 3L,3L),.Label = c(Bot,Top,All),class =factor), value = c(1.782726022,1,2.27,7946449,1.095240234,1.1103630141, 1.392545278,1,0.854984833,4.5163067,1,4.69271897,0.769428018, 1,0.4711117123,0.363854608,1.0195799358,0.6673186975, 1,1.661568993,1.1174998373,1.1.095026419,1.278455823, 1,0.634152231)),.Names = c(Condition,variable,value),row.names = c(NA,-27L),class =data.frame ) >头(tdat)条件变量值 1 AS Bot 1.782726 2 MCH Bot 1.000000 3 Dup Bot 2.267946 4 AS Bot 1.095240 5 MCH Bot 1.000000 6 Dup Bot 1.103630

你可以使用下面的代码来绘制它: p>

ggplot(tdat,aes(x = interaction(Condition,variable,drop = TRUE,sep =' - '),y = value , fill = Condition))+ geom_point()+ scale_color_discrete(name ='interaction levels')+ stat_summary(fun.y ='mean',geom =' bar', aes(label = signif(.. y ..,4),x = as.integer(interaction(Condition,variable))))+ facet_grid(。〜variable)

但正如你所能看到它不会删除每个方面未使用的列,你知道为什么吗? 在剧情中显示,因为使用了所有级别。如果完全不使用它们,级别将被丢弃。要删除每个方面的级别,请将 scale =free_x添加到 facet_grid()。但是这在特定情况下不起作用,因为您在 ggplot()和<$ c中使用了 x $ c> stat_summary()调用。

tdat $ int< -with(tdat,interaction(Condition,variable)我会建议在绘制交互之前添加新列。 ,drop = TRUE,sep =' - ')) ggplot(tdat,aes(int,value,fill = Condition))+ stat_summary(fun.y ='mean',geom ='bar ')+ geom_point()+ facet_grid(。〜variable,scales =free_x)

在这种情况下,您可以简化代码而无需使用 interaction(),因为您还使用 facet_grid()。

ggplot(tdat,aes(Condition,value,fill = Condition))+ stat_summary(fun.y ='mean' ,geom ='bar')+ geom_point()+ facet_grid(。〜variable)

Hi have this dataset :

tdat=structure(list(Condition = structure(c(1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L), .Label = c("AS", "Dup", "MCH"), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Bot", "Top", "All"), class = "factor"), value = c(1.782726022, 1, 2.267946449, 1.095240234, 1, 1.103630141, 1.392545278, 1, 0.854984833, 4.5163067, 1, 4.649271897, 0.769428018, 1, 0.483117123, 0.363854608, 1, 0.195799358, 0.673186975, 1, 1.661568993, 1.174998373, 1, 1.095026419, 1.278455823, 1, 0.634152231)), .Names = c("Condition", "variable", "value" ), row.names = c(NA, -27L), class = "data.frame") > head(tdat) Condition variable value 1 AS Bot 1.782726 2 MCH Bot 1.000000 3 Dup Bot 2.267946 4 AS Bot 1.095240 5 MCH Bot 1.000000 6 Dup Bot 1.103630

You can plot it like that using this code :

ggplot(tdat, aes(x=interaction(Condition,variable,drop=TRUE,sep='-'), y=value, fill=Condition)) + geom_point() + scale_color_discrete(name='interaction levels')+ stat_summary(fun.y='mean', geom='bar', aes(label=signif(..y..,4),x=as.integer(interaction(Condition,variable))))+ facet_grid(.~variable)

But as you can see it doesn't delete unused columns from each facet, do you know why ?

解决方案

You get all levels shown on plot because all levels are used. Levels are dropped if they are not used at all. To remove levels in each facet add scale="free_x" to facet_grid(). But this will not work in particular case because you use different statements of x values in ggplot() and stat_summary() calls. I would suggest to add new column before plotting with interaction.

tdat$int<-with(tdat,interaction(Condition,variable,drop=TRUE,sep='-')) ggplot(tdat,aes(int,value,fill=Condition))+ stat_summary(fun.y='mean', geom='bar')+ geom_point()+ facet_grid(.~variable,scales="free_x")

In this case you can simplify your code without interaction() because you use also facet_grid().

ggplot(tdat,aes(Condition,value,fill=Condition))+ stat_summary(fun.y='mean', geom='bar')+ geom_point()+ facet_grid(.~variable)

更多推荐

为什么这个facet

本文发布于:2023-10-17 02:58:57,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:facet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!