Graphics Programs Reference
In-Depth Information
The final bar graph in Figure 4-5 highlights years when a record was bro-
ken though—not when the United States won. The process and logic are
the same. You just need to change some of the conditions. The New.record
column in your data frame indicates new records, so if it's 1 , you do dark
red, and gray otherwise. Here's how to do it in R.
fill_colors <- c()
for ( i in 1:length(hotdogs$New.record) ) {
if (hotdogs$New.record[i] == 1) {
fill_colors <- c(fill_colors, “#821122”)
} else {
fill_colors <- c(fill_colors, “#cccccc”)
}
}
barplot(hotdogs$Dogs.eaten, names.arg=hotdogs$Year, col=fill_colors,
border=NA, xlab=”Year”, ylab=”Hot dogs and buns (HDB) eaten”)
It's the same as the United States example, just with different if state-
ments. Your result should look like Figure 4-10.
FIGurE 4-10 Bar graph with bars colored individually, but using different conditions than
Figure 4-9
At this point you can play around with some of the other barplot() options
such as spacing or adding a title.
barplot(hotdogs$Dogs.eaten, names.arg=hotdogs$Year, col=fill_colors,
border=NA, space=0.3, xlab=”Year”, ylab=”Hot dogs and buns (HDB)
eaten”)
Search WWH ::




Custom Search