问题描述
我想做与这个问题相反的事情,并且有点相反这个问题,虽然那是关于传说,而不是情节本身.
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.
其他 SO 问题似乎在询问如何保持未使用的因子水平.我实际上希望我的被移除.我有几个名称变量和几列(宽格式)变量属性,我用它们来创建许多条形图.这是一个可重现的示例:
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()
我明白了:
我希望只有具有相应 var 的名称出现在我的条形图中(例如,B 将没有空格).
I'd like only the names that have corresponding var's show up in my bar plot (as in, there would be no empty space for B).
如果我可以简单地更改我的输出文件名和 y=var
位,那么重用基本绘图代码将非常容易.如果可能的话,我不想仅仅为了在每个图的结果上使用 droplevels 而对我的数据框进行子集化!
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!
根据 na.omit()
建议进行更新
Update based on the na.omit()
suggestion
考虑修改后的数据集:
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
,因为它在 var3
中有一个 NA.这更类似于我的数据.我总共有 15 个回复,其中包含 NA.我只想删除没有 current 绘制的 y 向量值的因子级别,而不是在整个数据框中的 any 向量中具有 NA.
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.
推荐答案
一个简单的选择是在你的数据框 df
上使用 na.omit()
来删除那些带有 NA
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()
工作正常并且只考虑 Var1
中的 NA
.鉴于您只绘制 name
和 Var
,请将 na.omit()
应用于仅包含这些变量的数据框
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 条形图中删除未使用的因子水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!