仅绘制facet_grid中的几个选定面 [英] plot only a select few facets in facet_grid
本文介绍了仅绘制facet_grid中的几个选定面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种在 ggplot2
中使用 facet_grid
来绘图的方法,它只显示几个选择方面。说我有以下情节:
一直在寻找快速方法,例如,只绘制第1和第3个面。
#data
y <-1:12
x <-c(1,2,3,1,2,3,1,2,3,1, a),a,b,b,b,a,a,a,b, b,b,b)
df< -as.data.frame(cbind(x,y,z))
#plot
a < - ggplot(df,aes(x = z,y = y,
fill = z))
b < - a + geom_bar(stat =identity,position =dodge)
c < - b + facet_grid(。〜x,scale =free_y)
c
很显然,我已经想出了如何只是先切分我的数据,但是这当然可以在 ggplot2
中进行分配。即使只是微调也是非常受欢迎的。 / p>
解决方案 code> call。
plot_1 = ggplot(子集(df,x%in%c(1, 2)),aes(x = z,y = y,fill = z))+
geom_bar(stat =identity,position =dodge)+
facet_grid(。 〜x,scale =free_y)
I was looking for a way to plot using facet_grid
in ggplot2
that only displays just a few select facets. say I have the following plot:
Been looking for a quick way to, for instance, just plot facets 1 and 3.
#data
y<-1:12
x<-c(1,2,3,1,2,3,1,2,3,1,2,3)
z<-c("a","a","a","b","b","b","a","a","a","b","b","b")
df<-as.data.frame(cbind(x,y,z))
#plot
a <- ggplot(df, aes(x = z, y = y,
fill = z))
b <- a + geom_bar(stat = "identity", position = "dodge")
c <- b + facet_grid(. ~ x, scale = "free_y")
c
Obviously I figured out how to just chop up my data first but this must of course be possible to allocate in ggplot2
Even just a nudge would be most welcome.
解决方案
Use subset
in your ggplot
call.
plot_1 = ggplot(subset(df, x %in% c(1, 2)), aes(x=z, y=y, fill=z)) +
geom_bar(stat = "identity", position = "dodge") +
facet_grid(. ~ x, scale = "free_y")
这篇关于仅绘制facet_grid中的几个选定面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文