Graphics Programs Reference
In-Depth Information
Instead of looking at only the number of HDBs eaten by the winners, look
at the top three for each year. each stack represents a year, each with
three bars, and one for each top-three eater. Wikipedia has this data con-
sistently starting only in 2000, so start there.
First things first. Load the data in R. You can load it directly from the URL
with the following.
hot_dog_places <-
read.csv('http://datasets.flowingdata.com/hot-dog-places.csv',
sep=”,”, header=TRUE)
Type hot_dog_places to view the data. each column shows the results for a
year, and each row is for places 1 through 3.
X2000 X2001 X2002 X2003 X2004 X2005 X2006 X2007 X2008 X2009 X2010
1 25 50.0 50.5 44.5 53.5 49 54 66 59 68.0 54
2 24 31.0 26.0 30.5 38.0 37 52 63 59 64.5 43
3 22 23.5 25.5 29.5 32.0 32 37 49 42 55.0 37
See how an “X” precedes all the column names, which were added by
default when you loaded the data because the header names are num-
bers? R doesn't like that, so it adds a letter to make it word-like, or more
technically speaking, a string . You need to use the header names for labels
in the stacked bar chart, so change those back.
names(hot_dog_places) <- c(“2000”, “2001”, “2002”, “2003”, “2004”,
“2005”, “2006”, “2007”, “2008”, “2009”, “2010”)
Use quotes around each year to specify that it is a string. Type hot_dog_
places again, and the headers are just the years now.
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
1 25 50.0 50.5 44.5 53.5 49 54 66 59 68.0 54
2 24 31.0 26.0 30.5 38.0 37 52 63 59 64.5 43
3 22 23.5 25.5 29.5 32.0 32 37 49 42 55.0 37
Like before, use the barplot() function, but use data that's in a different
format. To pass all the preceding values to barplot() , you need to convert
hot_dog_places to a matrix. Right now it's a data frame. These are different
structures in R, but the differences between the two are not so important
right now. What you need to know is how to convert a data frame to a matrix.
hot_dog_matrix <- as.matrix(hot_dog_places)
Search WWH ::




Custom Search