![]() ![]() A simple fix would be to use shorter names rather than the full name of all teams, and in many situations that’s probably the right thing to do. To fix this, we have to be a bit cleverer again. We’ve fixed the problem, but we’ve created a new one: the axis labels don’t quite fit anymore. … the result is the bar graph shown in Figure 6.23. Las = 2) # rotate the labels Figure 6.23: we’ve rotated the labels, but now the text is too long to fit When I do that, as per the following command… barplot(height = freq, # the frequencies What I’ll do is tell R to rotate the text so that it’s always perpendicular to the axes (i.e., I’ll set las = 2). To do this, we need to alter set the las parameter, which I discussed briefly in Section ?. The simplest way to fix this is to rotate the labels, so that the text runs vertically not horizontally. However, the fact that barplot() has omitted the names of every team in between Adelaide and Fitzroy is a lot more problematic. However, in Figure 6.16 it wasn’t an issue: it’s pretty obvious from inspection that the two unlabelled plots in between 19 must correspond to the data from 19. This is the same behaviour we saw earlier with the multiple-boxplot graph in Figure 6.16. R has only included a few of the labels, because it can’t fit them in the plot. This is an improvement, but not much of an improvement. In this case, the teams vector is exactly what we need, so the command we’re looking for is: barplot( height = freq, names.arg = teams ) Figure 6.22: we’ve added the labels, but because the text runs horizontally R only includes a few of them The names.arg argument needs to be a vector of character strings containing the text that needs to be used as the label for each of the items. It doesn’t have any labels, obviously, because we didn’t actually tell the barplot() function what the labels are! To do this, we need to specify the names.arg argument. Figure 6.21: the simplest version of a bargraph, containing the data but no labelsĪs you can see, R has drawn a pretty minimal plot. ![]() > barplot( freq ) # the lazier version (panel a)Įither of these two commands will produce the simple bar graph shown in Figure 6.21. The main argument that you need to specify for a bar graph is the height of the bars, which in our case correspond to the values stored in the freq variable: > barplot( height = freq ) # specifying the argument name (panel a) ![]() Okay, so now that we have the information we need, let’s draw our bar graph. "Richmond" "St Kilda" "Sydney" "West Coast" "Hawthorn" "Melbourne" "North Melbourne" "Port Adelaide" "Essendon" "Fitzroy" "Fremantle" "Geelong" "Adelaide" "Brisbane" "Carlton" "Collingwood" I’ll do this using the levels() function, which outputs the names of all the levels of a factor (see Section 4.7: > teams print( teams ) That being said, I’m obviously going to need the team names to create some labels, so let’s create a variable with those. I’m only doing it this way so that you can see the barplot() function in it’s “purest” form: when the input is just an ordinary numeric vector. This isn’t exactly the prettiest of frequency tables, of course. I’ll use the tabulate() function to do this (which will be discussed properly in Section ?, since it creates a simple numeric vector: > freq print( freq ) So, let’s start by creating a vector that contains this information. What I want to do is draw a bar graph that displays the number of finals that each team has played in over the time spanned by the afl data set. 102 And to illustrate the use of the function, I’ll use the finalists variable that I introduced in Section 5.1.7. The main function that you can use in R to draw them is the barplot() function. \)Īnother form of graph that you often want to plot is the bar graph.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |