Coefficient order in faceted dotplot using ggplot2

I am trying to change the plotting order on the faces of a faceted point in ggplot2, but I cannot get it to work. Here is my molten 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 is my command:

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

The result is as follows:

R plot

The borders themselves appear in the order that I want in this plot. However, in every aspect, I would like to sort by graph. That is, for each type of medal, I would like the country to win the largest number of these medals from above and so on. The procedure that I successfully used when there are no edges (let's say we look only at gold medals) is to use the reorder function on the country coefficient, sorted by count , but this does not work as an example.

I would really appreciate any suggestions you might have.

+7
source share
2 answers

Here is a solution using paste, free scales and some replacement

 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

+8
source

This is the best I can do with qplot. Not quite what you requested, but closer. OOOPs, I see, you already understood this.

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

Here is a dput version so others can improve:

 dput(London.melt) structure(list(country = structure(c(9L, 6L, 3L, 1L, 7L, 4L, 5L, 8L, 2L, 10L, 9L, 6L, 3L, 1L, 7L, 4L, 5L, 8L, 2L, 10L, 9L, 6L, 3L, 1L, 7L, 4L, 5L, 8L, 2L, 10L), .Label = c("Australia", "China", "France", "Germany", "Great Britain & N. Ireland", "Italy", "Japan", "Russian Federation", "South Korea", "United States" ), class = "factor"), medal.type = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("bronze", "gold", "silver"), class = "factor"), count = c(13L, 8L, 11L, 7L, 7L, 11L, 29L, 24L, 38L, 46L, 8L, 9L, 11L, 16L, 14L, 19L, 17L, 26L, 27L, 29L, 7L, 11L, 12L, 12L, 17L, 14L, 19L, 32L, 23L, 29L)), .Names = c("country", "medal.type", "count"), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30")) 
0
source

All Articles