2

I'm trying to plot the following data set in R:

fruit<-matrix(c("Apple","186","Banana","93","Elderberry","48","Durian", "28","Cherry", "28"),ncol=2,byrow=TRUE)
colnames(fruit) <- c("Name","Freq")
fruit <- data.table(fruit)
fruit$Freq  <- as.numeric(as.character(fruit$Freq))
qplot(fruit$Name, fruit$Freq, geom="bar", stat="identity") + coord_flip()

But it's being plotted in alphabetical order

enter image description here

I want the barplot to show the fruit from highest frequency value to lowest. So Apple at the top of the Y axis, then Banana, etc...:

 Apple       186
 Banana      93
 Elderberry  48
 Durian      28
 Cherry      28

I've tried to play with factors and levels but can't figure it out.

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
aiolias
  • 849
  • 1
  • 6
  • 8

2 Answers2

1

Use reorder to order Name by Freq:

ggplot(fruit, aes(reorder(Name, Freq), Freq)) + 
   geom_bar(fill=hcl(195,100,65), stat="identity") + coord_flip() +
   xlab("Fruit") + ylab("Frequency")

enter image description here

If you want an arbitrary ordering of Name, you can do it manually using factor:

fruit$Name = factor(fruit$Name, 
                    levels=c("Banana", "Durian", "Elderberry", "Apple", "Cherry"))

# Now ggplot will plot the bars in the order listed in the factor command
ggplot(fruit, aes(Name, Freq)) + geom_bar(stat="identity") + coord_flip()

One last thing: You can create your data frame with less code. For example:

fruit = data.frame(Name=c("Apple","Banana", "Elderberry", "Durian", "Cherry"),
                   Freq=c(186, 93, 48, 28, 28))
eipi10
  • 91,525
  • 24
  • 209
  • 285
  • I had to add stat="identity" to geom_bar and then it worked perfectly. Thanks. – aiolias Jun 11 '14 at 20:39
  • `stat="identity"` tells `ggplot` that you want the bars to be the actual values of `Freq`, rather than a histogram of counts of values in `Freq`. When you include both `x` and `y` values inside `aes`, `ggplot` just gives a warning and proceeds as if you've specified `stat="identity"`. – eipi10 Jun 11 '14 at 21:05
1

In addition to @Richard Scriven, you could further manipulate your barplot with the following base code:

fruit <- fruit[order(Freq),]

par(mar = c(4,5,2,2))
barplot(fruit$Freq, xlim = c(0,200), xaxt = "n", space = 1, horiz = TRUE, 
        names.arg = fruit$Name, las = 2)

axis(1, at = seq(0,200,20))
mtext("Frequency (#)", 1, line = 2.5)

enter image description here

ccapizzano
  • 1,556
  • 13
  • 20
  • Thanks, I ended up using a ggplot resolution above. I should've specified that I wanted to do it in ggplot. – aiolias Jun 11 '14 at 20:40