从ggplot条形图中删除未使用的因子水平 [英] Remove unused factor levels from a ggplot bar plot

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

问题描述

我想做与这个问题相反的情况,而有点相反这个问题,尽管那是关于传说的,而不是情节本身。



其他SO问题似乎在问如何保持未使用的因子水平。我真的喜欢我的删除。我有几个名称变量和几列(宽格式)的变量属性,我用它来创建多个条形图。这里有一个可重现的例子:

  library(ggplot2)
df< - data.frame(name = c( (a,b,c),var1 = c(1,NA,2),var2 = c(3,4,5))
ggplot(df,aes(x = name,y = var b1))+ geom_bar()

我得到这个:



我只想在我的柱状图中显示具有相应的var n 的名称(因为在B中没有空白空间)。

如果我可以简单地更改输出文件名和 y = var 位,则重新使用基本代码将非常简单。如果可能的话,我不希望子集我的数据框只是为每个绘图的结果使用小节!




根据 na.omit()建议进行更新

考虑修改后的数据集:

  library(ggplot2)
df< - data.frame(name = c(A, B,C),var1 = c(1,NA,2),var2 = c(3,4,5),var3 = c(NA,6,7))
ggplot(df,aes (x = name,y = var1))+ geom_bar()

我需要使用 na.omit()用于绘制 var1 ,因为存在NA。但是由于na.omit确保所有列都存在的值,因此该图除去 A ,因为它的NA < VAR3 。这更类似于我的数据。我有15个全面的回应,其中新来的助教有很多。我只想删除整个数据框中没有当前绘制的y矢量值的因子级别,而不是任何矢量中的NA。

解决方案

一个简单的选择是在您的数据框上使用 na.omit()使用 NA

df > ggplot(na.omit(df),aes(x = name,y = var1))+ geom_bar()

考虑到你的更新,以下

  ggplot(df [!is.na(df 

















$可以正常工作,只考虑 Var1 中的 NA 。考虑到你只绘制了 name Var ,应用 na.omit() code $>到一个只包含这些变量的数据框

  ggplot(na.omit(df [,c(name ,var1)]),aes(x = name,y = var1))+ geom_bar()


I want to do the opposite of this question, and sort of the opposite of this question, though that's about legends, not the plot itself.

The other SO questions seem to be asking about how to keep unused factor levels. I'd actually like mine removed. I have several name variables and several columns (wide format) of variable attributes that I'm using to create numerous bar plots. Here's a reproducible example:

library(ggplot2)
df <- data.frame(name=c("A","B","C"), var1=c(1,NA,2),var2=c(3,4,5))
ggplot(df, aes(x=name,y=var1)) + geom_bar()

I get this:

I'd like only the names that have corresponding varn's show up in my bar plot (as in, there would be no empty space for B).

Reusing the base plot code will be quite easy if I can simply change my output file name and y=var bit. I'd like not have to subset my data frame just to use droplevels on the result for each plot if possible!


Update based on the na.omit() suggestion

Consider a revised data set:

library(ggplot2)
df <- data.frame(name=c("A","B","C"), var1=c(1,NA,2),var2=c(3,4,5), var3=c(NA,6,7))
ggplot(df, aes(x=name,y=var1)) + geom_bar()

I need to use na.omit() for plotting var1 because there's an NA present. But since na.omit makes sure values are present for all columns, the plot removes A as well since it has an NA in var3. This is more analogous to my data. I have 15 total responses with NAs peppered about. I only want to remove factor levels that don't have values for the current plotted y vector, not that have NAs in any vector in the whole data frame.

解决方案

One easy options is to use na.omit() on your data frame df to remove those rows with NA

ggplot(na.omit(df), aes(x=name,y=var1)) + geom_bar()

Given your update, the following

ggplot(df[!is.na(df$var1), ], aes(x=name,y=var1)) + geom_bar()

works OK and only considers NA in Var1. Given that you are only plotting name and Var, apply na.omit() to a data frame containing only those variables

ggplot(na.omit(df[, c("name", "var1")]), aes(x=name,y=var1)) + geom_bar()

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

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