[R] Factor level with no cases shows up in a plot

krzysztof.sakrejda at gmail.com krzysztof.sakrejda at gmail.com
Wed May 27 22:54:01 CEST 2009


If you have a vector of factors with empty levels you can get rid of them by rerunning the vector through the factor function:

vec <- factor(vec)

Don't know if this can be done at plotting time...
Sent via BlackBerry by AT&T

-----Original Message-----
From: "Arthur Burke" <burkea at nwrel.org>

Date: Wed, 27 May 2009 11:46:12 
To: <r-help at r-project.org>
Subject: [R] Factor level with no cases shows up in a plot


Consider this data structure  (df1) ...
 
      Group Year PctProf FullYr
1  Never RF 2004      87     88
2  Cohort 1 2004      83     84
3  Cohort 2 2004      84     86
4  Cohort 3 2004      87     87
5  Cohort 4 2004      73     74
6  Never RF 2005      85     86
7  Cohort 1 2005      81     82
8  Cohort 2 2005      81     81
9  Cohort 3 2005      78     79
10 Cohort 4 2005      72     74
11 Never RF 2006      83     84
12 Cohort 1 2006      78     78
13 Cohort 2 2006      78     79
14 Cohort 3 2006      70     71
15 Cohort 4 2006      80     82
16 Never RF 2007      82     83
17 Cohort 1 2007      75     76
18 Cohort 2 2007      73     74
19 Cohort 3 2007      79     80
20 Cohort 4 2007      75     77
21 Never RF 2008      83     84
22 Cohort 1 2008      81     81
23 Cohort 2 2008      81     81
24 Cohort 3 2008      76     77
25 Cohort 4 2008      62     63

.... which I subsetted to omit all cases for Cohort 4 and some cases for
Cohorts 2 & 3 ...
 
df2 <- subset(df1,
    ((Group == "Cohort 1" | Group == "Never RF") | (Group == "Cohort 2"
& Year != 2004) |
      (Group == "Cohort 3" & Year > 2006)))
 
> df2
      Group Year PctProf FullYr
1  Never RF 2004      87     88
2  Cohort 1 2004      83     84
6  Never RF 2005      85     86
7  Cohort 1 2005      81     82
8  Cohort 2 2005      81     81
11 Never RF 2006      83     84
12 Cohort 1 2006      78     78
13 Cohort 2 2006      78     79
16 Never RF 2007      82     83
17 Cohort 1 2007      75     76
18 Cohort 2 2007      73     74
19 Cohort 3 2007      79     80
21 Never RF 2008      83     84
22 Cohort 1 2008      81     81
23 Cohort 2 2008      81     81
24 Cohort 3 2008      76     77

Now, 
 
> table(df2$Group)
 
... properly shows 0 cases for the Group level "cohort 4" ...
 
Cohort 1 Cohort 2 Cohort 3 Cohort 4 Never RF 
       5        4        2        0        5 

But when I plot ...
 
coll = c("violet","blue","green","red")

with(df2, interaction.plot(Year, Group, FullYr,
     lwd=3,col=coll, bty="l", lty=1, las=1,
     ylab="Percent Proficient", xlab="",
     main = "Proficiency Trends for RF and Non-RF Schools"))
 
...  I get the four lines that I expected but the legend includes the
Group level "cohort 4" .
 
How can I get rid of "cohort 4" in Group?
 
Thanks!
Art

------------------------------------------------------------------

Art Burke
Northwest Regional Educational Laboratory
101 SW Main St, Suite 500
Portland, OR 97204-3213

Phone:  503-275-9592 / 800-547-6339

Fax: 503-275-0450

burkea at nwrel.org 




  


	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


More information about the R-help mailing list