从ggplot2中删除未使用的因子 [英] Removing Unused Factors from a Facet in ggplot2

查看:126
本文介绍了从ggplot2中删除未使用的因子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一个简单的方法来从ggplot2的一个方面中删除未使用的因素。这里是一个简单的例子

 #DUMMY DATA 
mydf = data.frame(
x = rpois(6 ,
y = LETTERS [1:6],
cat = c(rep('AA',3),rep('BB',3)))

#绘制它!
p0 = ggplot(mydf,aes(x = x,y = y))+
geom_point()+
facet_wrap(〜cat,ncol = 1)


$ b

从下面的图中可以看出,因素D,E和F是在面AA中绘制的,尽管没有相应的事实数据。我想要的是从方面AA消除{D,E,F},并且类似于方面BB的{A,B,C}。



有没有一种简单的方法可以做到这一点,或者甚至可以接受一种黑客行为。

解决方案

我认为你需要的只是 scales =free_y

  p0 = ggplot(mydf,aes(x = x,y = y))+ 
geom_point()+
facet_wrap(〜cat,ncol = 1,scales =free_y)

p0


I am trying to figure out a neat way to remove unused factors from a facet in ggplot2. Here is a minimal example

# DUMMY DATA
mydf = data.frame(
  x = rpois(6, 25),
  y = LETTERS[1:6],
  cat = c(rep('AA', 3), rep('BB', 3)))

# PLOT IT!
p0 = ggplot(mydf, aes(x = x, y = y)) +
  geom_point() +
  facet_wrap(~ cat, ncol = 1)

From the plot below, you can see that factors D, E and F are plotted in facet AA despite the fact that there is no corresponding data. What I want is for a way to eliminate {D, E, F} from facet AA and similarly {A, B, C} from facet BB.

Is there a neat way to do this, or even a hack would be acceptable.

解决方案

I think all you need is scales = "free_y":

p0 = ggplot(mydf, aes(x = x, y = y)) +
  geom_point() +
  facet_wrap(~ cat, ncol = 1,scales = "free_y")

p0

这篇关于从ggplot2中删除未使用的因子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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