如何避免情节中的空因子水平?

时间:2013-10-21 13:32:41

标签: r plot subset

用户,

我有这个数据框:

A<- c(10,2,4,5,3,5,98,65,36,65,6,100,70,54,25,23,22,30,15,23)
B<- c(1,0.1,0.5,0.8,0.2,0.9,3,1.2,5.6,3.5,15.9,10.2,5,5.1,7.1,5,6,10,4,8)
C<- c("a","a","a","a","a","a","b","b","b","b","c","c","c","c","d","d","d","d","d","d")
mydf<- data.frame(A,B,C)

我做了一个只保留级别“a”的子集。

subset<- subset(mydf, mydf$C=="a")

但是当我制作一个情节(请看图片)时,图表也会显示删除的水平。

plot(B~ C, data=subset)

如何绘制子集化数据框以避免删除的级别?

plot subset data

谢谢!

3 个答案:

答案 0 :(得分:3)

str(subset)
#'data.frame':   6 obs. of  3 variables:
# $ A: num  10 2 4 5 3 5
# $ B: num  1 0.1 0.5 0.8 0.2 0.9
# $ C: Factor w/ 4 levels "a","b","c","d": 1 1 1 1 1 1

通过factor

删除缺失的因子级别
subset$C <- factor(subset$C)

str(subset)
#'data.frame':   6 obs. of  3 variables:
#$ A: num  10 2 4 5 3 5
#$ B: num  1 0.1 0.5 0.8 0.2 0.9
#$ C: Factor w/ 1 level "a": 1 1 1 1 1 1

答案 1 :(得分:2)

使用droplevels

subset$C <- droplevels(subset$C)
plot(B~ C, data=subset)

顺便说一句,subset不是data.frame的好名字。

答案 2 :(得分:1)

只是做:

plot(B~ droplevels(C), data=subset)