0

I'm trying to create a dotplot where countries are listed on my Y axis from A-Z top to bottom. The medal count will be the X axis for each of the four plots, one each for gold, silver, bronze, and total. Of course, ggplot prefers to plot countries from Z-A and despite reading all about the problem, I haven't resolved the issue. I appreciate any straightforward help on both the coding and comprehension fronts.

mdat <- melt(raw, value.name = "Count", variable.name = "Place", id.var = "Country")
mdat[, "Place"] <- factor(mdat[, "Place"], levels=c("Gold", "Silver", "Bronze",          "Total"))
##I know my problem is likely on or around the above line  ##
plot1 <- ggplot(mdat, aes(x = Count, y = Country, colour = Place)) +
  geom_point() +
  facet_grid(.~Place) + theme_bw()+
  scale_colour_manual(values=c("#FFCC33", "#999999", "#CC6600", "#000000")) 
print(plot1)


Algeria     Gold    4
Argentina   Gold    5
Armenia     Gold    1
Algeria     Silver  2
Argentina   Silver  5
Armenia     Silver  2
Algeria     Bronze  4
Argentina   Bronze  2
Armenia     Bronze  0
Kara
  • 6,115
  • 16
  • 50
  • 57
SimplySnee
  • 13
  • 3
  • 1
    Welcome to SO. Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – agstudy May 23 '14 at 06:19
  • Can you add a `dput` of your `raw` dataframe? – Jaap May 23 '14 at 07:16

1 Answers1

1

You have to sort the levels of Country before you plot. Also, there is no Total level the data you provided. The following appraoch should give you the desired result:

Reading the data (including a Total level for the Place variable):

mdat <- read.table(text="Country    Place Count
Algeria     Gold    4
Argentina   Gold    5
Armenia     Gold    1
Algeria     Silver  2
Argentina   Silver  5
Armenia     Silver  2
Algeria     Bronze  4
Argentina   Bronze  2
Armenia     Bronze  0
Algeria     Total   10
Argentina   Total   12
Armenia     Total   3", header=TRUE)

Sorting the levels of the Country variable:

mdat$Country <- factor(mdat$Country,levels=sort(unique(mdat$Country),decreasing=TRUE))

Getting your Place variable in the correct order:

levels(mdat$Place) <- c("Bronze"=3,"Gold"=1,"Silver"=2,"Total"=4)
mdat$Place <- as.numeric(mdat$Place)
mdat$Place <- as.factor(mdat$Place)
levels(mdat$Place) <- c("Gold","Silver","Bronze","Total")

Creating the plot:

ggplot(mdat, aes(x = Count, y = Country, colour = Place)) +
  geom_point(size=4) +
  facet_grid(.~Place) + theme_bw()+
  scale_colour_manual(values=c("#FFCC33","#999999","#CC6600","#000000"))

which gives the following plot: enter image description here


As you melted your data already, I suspect that there is no Total variable in the raw dataframe. You can calculte that with:

raw$Total <- rowSums(..specify the Gold, Silver & Bronze columns here..)
Jaap
  • 81,064
  • 34
  • 182
  • 193
  • Thanks, Jaap. I actually did have a 'total' level and subsequent variable, but very good catch. Sorry, it was the end of the night. Your code worked perfectly. – SimplySnee May 23 '14 at 14:56
  • By chance, does ggplot allow me to duplicate that beautifully sorted Y axis label on the right side? – SimplySnee May 23 '14 at 14:56
  • The developer of `ggplot2` [doesn't like](http://stackoverflow.com/a/3101876/2204410) dual y-axis to put it mildly (and for good reasons). As a result there is no easy way to do this. The answers in [this question](http://stackoverflow.com/questions/15334494/how-to-change-positions-of-x-and-y-axis-in-ggplot2), or [this example](http://rpubs.com/kohske/dual_axis_in_ggplot2) seem to be the only way to get the desired result. – Jaap May 23 '14 at 15:15
  • Interesting. Let's see if I learned anything. If I want to order the Y-axis by the value of "total", this seems to work, but am i right? mdat$Country <- factor(mdat$Country, levels=mdat[order(mdat$Count,decreasing=F),]$Country) – SimplySnee May 24 '14 at 02:13