使用多个构面去除ggplot2中聚集条形图中的空因子 [英] Remove empty factors from clustered bargraph in ggplot2 with multiple facets

查看:122
本文介绍了使用多个构面去除ggplot2中聚集条形图中的空因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ggplot2制作一个更好的R底图版本。不仅有一个共同的传说,而且因为我喜欢ggplot2风格和定制。我的数据由3个独立的数据集组成,这些数据集包含几个(但不同的)治疗的相同两组观察值。因此,我想用1个图表生成3个独立的图表,但是具有不同的因子水平。为了说明我的观点,这里的第一张图片是我迄今为止用R base生成的图片:



我尝试使用与我的数据完全相同的结构生成伪数据的ggplot2图:

  foo <-data.frame(c(letters,letters),c(rep('T1',26),rep('T2',26)),
runif(52),rep(c(rep('Ori1',12),rep('Ori2',8),rep('ori3',6)),2))
names(foo )< -c('Treatment','Type','Count','Origin')

a <-ggplot(foo,aes(x =因子(治疗),y =计数) )
a + facet_grid(Origin〜。,scales =free_y,space =free)+
geom_bar(stat =identity,aes(fill = factor(foo $ Type)),position =dodge)
+ theme_bw()+ theme(axis.text.x = element_text(angle = 60,hjust = 1))+ coord_flip()

这给我下面的不良结果。



我是了解堆栈溢出主题从ggplot2中删除未使用的因子如何从ggplot2方面删除空因素?但是,他们不处理我试图在这里实现的聚簇条形图,我觉得它们是问题,但是现在不需要如何解决它。

解决方案

为了说明我的评论:

<$ p $ b $ a + facet_wrap(〜Origin,scales =free_x)+ $ b $(a =($ x $因子(治疗),y =计数) b)geom_bar(stat =identity,aes(fill = factor(Type)),position =dodge)+
theme_bw()+
theme(axis.text.x = element_text(angle = 60,hjust = 1))


请注意,如果您添加 coord_flip 并切换到 free_y ,您将得到一个关于 coord_flip 的特定错误,它不适用于某些类型的自由标尺,你的问题。


I am trying to make a better version of an R base plot with ggplot2. Not only to have a common legend but also because I like the ggplot2 styles and customization. My data consists of 3 seperate datasets that contain the same two groups of observations for several (but different) treatments. Hence I want to generate 3 separate plots in 1 graph with a common legend however with different factor levels. To illustrate my point the first image here is what I have generated with R base so far:

I tried to generate a ggplot2 plot with dummy data that has exactly the same structure as my data:

foo<-data.frame(c(letters,letters),c(rep('T1',26),rep('T2',26)),
runif(52),rep(c(rep('Ori1',12),rep('Ori2',8),rep('ori3',6)),2))
names(foo)<-c('Treatment','Type','Count','Origin')

a<-ggplot(foo,aes(x = factor(Treatment),y = Count))
a+ facet_grid(Origin~., scales="free_y", space="free") + 
geom_bar(stat="identity",aes(fill=factor(foo$Type)),position="dodge")
+theme_bw()+theme(axis.text.x=element_text(angle=60,hjust=1))+coord_flip()

Which gives me the following undesirable result.

I am aware of the stack overflow topics Removing Unused Factors from a Facet in ggplot2 and How can I remove empty factors from ggplot2 facets? however, they do not deal with the clustered bar graphs I try to realise here and I feel they are the problem, however do not now how to solve it. All pointers are welcome.

解决方案

To illustrate my comment:

a<-ggplot(foo,aes(x = factor(Treatment),y = Count))
a+ facet_wrap(~Origin, scales="free_x") + 
    geom_bar(stat="identity",aes(fill=factor(Type)),position="dodge") + 
    theme_bw() + 
    theme(axis.text.x=element_text(angle=60,hjust=1))

Note that if you add coord_flip and switch to free_y you get a specific error about coord_flip not working with some types of free scales, which is the source of you problem.

这篇关于使用多个构面去除ggplot2中聚集条形图中的空因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆