Graphics Reference
In-Depth Information
have an na.rm flag, but we can get the same effect by using sum(!is.na(...)) . The is.na()
function returns a logical vector: it has a TRUE for each NA item, and a FALSE for all other items.
It is inverted by the ! , and then sum() adds up the number of TRUE is The end result is a count
of non- NA s:
ddply(c1, c( "Cult" , "Date" ), summarise,
Weight = mean(HeadWt, na.rm = TRUE
TRUE ),
sd = sd(HeadWt, na.rm = TRUE
TRUE ),
n = sum(!is.na(HeadWt)))
Cult Date Weight sd n
c39 d16 3.255556 0.9824855 9
c39 d20 2.722222 0.1394433 9
c39 d21 2.740000 0.9834181 10
c52 d16 2.260000 0.4452215 10
c52 d20 3.044444 0.8094923 9
c52 d21 1.470000 0.2110819 10
Missing combinations
If there are any empty combinations of the grouping variables, they will not appear in the sum-
marized data frame. These missing combinations can cause problems when making graphs. To
illustrate, we'll remove all entries that have levels c52 and d21 . The graph on the left in Fig-
ure 15-3 shows what happens when there's a missing combination in a bar graph:
# Copy cabbages and remove all rows with both c52 and d21
c2 <- subset(c1, !( Cult == "c52" & Date == "d21" ) )
c2a <- ddply(c2, c( "Cult" , "Date" ), summarise,
Weight = mean(HeadWt, na.rm = TRUE
TRUE ),
sd = sd(HeadWt, na.rm = TRUE
TRUE ),
n = sum(!is.na(HeadWt)))
c2a
Cult Date Weight sd n
c39 d16 3.255556 0.9824855 9
c39 d20 2.722222 0.1394433 9
c39 d21 2.740000 0.9834181 10
c52 d16 2.260000 0.4452215 10
c52 d20 3.044444 0.8094923 9
# Make the graph
ggplot(c2a, aes(x = Date, fill = Cult, y = Weight)) + geom_bar(position = "dodge" )
Search WWH ::




Custom Search