2

I want to plot country names sorted alphabetically, where Argentina is at the top. When I change arrange(Country) to arrange(desc(Country)) the data frame is sorted in descending order but ggplot plots the same.

# Turbines and manufacturers
library(XML)
library(dplyr)
library(ggplot2)

data_url <- "http://www.thewindpower.net/turbines_manufacturers_en.php"

doc <- htmlParse(data_url)

data <- readHTMLTable(doc,
                  colClasses = c("character","character","character"),
                  trim = TRUE,
                  which = 6
                  )

plot_data <- data %>%
    filter(Country != "") %>%
    group_by(Country) %>%
    summarise(Freq = n()) %>%
    arrange(Country)


# A bar graph
ggplot(plot_data, aes(Country , Freq,  fill=Country)) + 
    coord_flip() +
    geom_bar(stat="identity", width=.90) + 
    xlab("") + # Set axis labels
    ylab("") + 
    guides(fill=FALSE) +
    ggtitle("Number of Turbine Manufacturers by Country") + 
    theme_minimal()

Number of Turbine Manufacturers by Country

Enrique Pérez Herrero
  • 3,699
  • 2
  • 32
  • 33

1 Answers1

4

By default, ggplot2 plots factors in the order they are coded. Yours are coded alphabetically (see levels(plot_data$Country)), but the coord_flip() messes it up. You could use scale_x_reverse(), but you have a discrete value, so it wont work.

You need to relevel the factor of Country, to the reverse of what it currently is.

To do this, add the line:

plot_data$Country <- factor(plot_data$Country, levels = rev(levels(plot_data$Country)))

after your plot data, but before the graph.

enter image description here

jeremycg
  • 24,657
  • 5
  • 63
  • 74