1

I have a question about plotting x axis in a certain order.

here is the sample data I am plotting:

  year                             names count
1 1998               allmylife - kcijojo    83
2 1997      doowopthatthing - laurynhill   196
3 1998      gettinjiggywitit - willsmith   231
4 2000 idontwanttomissathing - aerosmith    82
5 1998              imyourangel - rkelly   121
6 2013               myall - mariahcarey    70

Here is my code:

library(ggplot2)
setwd("C:/Users/Andrew/Desktop/music lyrics")
data = read.csv("summary.csv", header=FALSE, stringsAsFactors = FALSE)
names(data) = c('year', 'names', 'count')

ggplot(data, aes(names,count,  fill=year))+
geom_bar(stat='identity')+ 
theme(axis.text.x = element_text(angle = 90, hjust = 1))

And here is my current plot: enter image description here

How do I sort the x axis such that it starts from 1998, 1999, 2000....2014? (instead of plotting them in random years)

user3773503
  • 161
  • 2
  • 5
  • 12

2 Answers2

3

Do that in the ggplot function using sort:

library(ggplot2)
a$year <- factor(a$year)
ggplot(a, aes(names, count,  fill=year)) +
  geom_bar(stat='identity') + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  scale_x_discrete(limits=a$names[order(a$year)])  

enter image description here

LyzandeR
  • 37,047
  • 12
  • 77
  • 87
1

This will order the bars first by year, then alphabetically by name.

d <- read.csv(text="year,names,count
1998,               allmylife - kcijojo,    83
1997,      doowopthatthing - laurynhill,   196
1998,      gettinjiggywitit - willsmith,   231
2000, idontwanttomissathing - aerosmith,    82
1998,              imyourangel - rkelly,   121
2013,               myall - mariahcarey,    70")

d <- with(d, d[order(year, names), ])

d <- within(d, {
    names <- reorder(names, seq_along(names), order=TRUE)
    year <- as.factor(year)
})

ggplot(d, aes(names, count,  fill=year))+
  geom_bar(stat='identity')+ 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +labs(fill='year') 

enter image description here

Matthew Plourde
  • 43,932
  • 7
  • 96
  • 113
  • I understand the with() function sort the dataframe first by year, then by its names. Would you mind explaining the purpose of the within() function? – user3773503 Nov 20 '14 at 17:10
  • `within` is like `with`, but instead of returning the value of the final statement, it returns a copy of the `data.frame` with the changes applied. – Matthew Plourde Nov 20 '14 at 17:11
  • Okay, but for this case both with() and within() return a data.frame, right? Also, does reorder(names, seq_along(names), order=True) fixes the order of the names and forces ggplot to follow this order? – user3773503 Nov 20 '14 at 17:25