Factor order within faceted dotplot using ggplot2

后端 未结 3 1710
终归单人心
终归单人心 2020-12-08 23:47

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.m         


        
3条回答
  •  悲哀的现实
    2020-12-09 00:31

    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

提交回复
热议问题