Graphics Reference
In-Depth Information
It's also possible to swap the axes so that the names go along the x-axis and the values go along
the y-axis, as shown in Figure 3-29 . We'll also rotate the text labels by 60 degrees:
ggplot(tophit, aes(x = reorder(name, avg), y = avg)) +
geom_point(size = 3 ) +
# Use a larger dot
theme_bw() +
theme(axis.text.x = element_text(angle = 60 , hjust = 1 ),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_line(colour = "grey60" , linetype = "dashed" ))
Figure 3-29. Dot plot with names on x-axis and values on y-axis
It's also sometimes desirable to group the items by another variable. In this case we'll use the
factor lg , which has the levels NL and AL , representing the National League and the American
League. This time we want to sort first by lg and then by avg . Unfortunately, the reorder()
function will only order factor levels by one other variable; to order the factor levels by two vari-
ables, we must do it manually:
# Get the names, sorted first by lg, then by avg
nameorder <- tophit$name[order(tophit$lg, tophit$avg)]
# Turn name into a factor, with levels in the order of nameorder
tophit$name <- factor(tophit$name, levels = nameorder)
Search WWH ::




Custom Search