Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factor order within faceted dotplot using ggplot2

Tags:

r

ggplot2

I'm trying to change the plotting order within facets of a faceted dotplot in ggplot2, but I can't get it to work. Here's my melted dataset:

> London.melt
                      country medal.type count
1                 South Korea       gold    13
2                       Italy       gold     8 
3                      France       gold    11
4                   Australia       gold     7
5                       Japan       gold     7
6                     Germany       gold    11
7  Great Britain & N. Ireland       gold    29
8          Russian Federation       gold    24
9                       China       gold    38
10              United States       gold    46
11                South Korea     silver     8
12                      Italy     silver     9
13                     France     silver    11
14                  Australia     silver    16
15                      Japan     silver    14
16                    Germany     silver    19
17 Great Britain & N. Ireland     silver    17
18         Russian Federation     silver    26
19                      China     silver    27
20              United States     silver    29
21                South Korea     bronze     7
22                      Italy     bronze    11
23                     France     bronze    12
24                  Australia     bronze    12
25                      Japan     bronze    17
26                    Germany     bronze    14
27 Great Britain & N. Ireland     bronze    19
28         Russian Federation     bronze    32
29                      China     bronze    23
30              United States     bronze    29

and here's my plot command:

qplot(x = count, y = country, data = London.melt, geom = "point", facets = medal.type ~.)

The result I get is as follows:

R plot

The facets themselves appear in the order I want in this plot. Within each facet, however, I'd like to sort by count. That is, for each type of medal, I'd like the country that won the greatest number of those medals on top, and so on. The procedure I have used successfully when there are no facets (say we're only looking at gold medals) is to use the reorder function on the factor country, sorting by count but this doesn't work in the present example.

I'd greatly appreciate any suggestions you might have.

like image 249
inhuretnakht Avatar asked Aug 21 '12 22:08

inhuretnakht


People also ask

How to reorder groups in ggplot?

To reorder the boxplot we will use reorder() function of ggplot2. By default, ggplot2 orders the groups in alphabetical order. But for better visualization of data sometimes we need to reorder them in increasing and decreasing order. This is where the reorder() function comes into play.

What is Dotplot in R?

A dot plot or dot chart is similar to a scatter plot. The main difference is that the dot plot in R displays the index (each category) in the vertical axis and the corresponding value in the horizontal axis, so you can see the value of each observation following a horizontal line from the label.


1 Answers

Here a solution using paste, free scales and some relabeling

library(ggplot2)
London.melt$medal.type<-factor(London.melt$medal.type, levels = c("gold","silver","bronze"))
# Make every country unique
London.melt$country_l <- with(London.melt, paste(country, medal.type, sep = "_"))
#Reorder the unique countrys
q <- qplot(x = count, y = reorder(country_l, count), data = London.melt, geom = "point") +   facet_grid(medal.type ~., scales = "free_y")
# Rename the countries using the original names
q + scale_y_discrete("Country", breaks = London.melt$country_l, label = London.melt$country)

enter image description here

like image 114
Luciano Selzer Avatar answered Sep 19 '22 01:09

Luciano Selzer