1

I'm using ggplot package in R to plot a simple point graph with the "Lecture" number on the xlab and the "music week" on the ylab.

Here is the simple dataset I'm trying to plot:

Dataset - Music2

Here is the code I used:

ggplot(music2, aes(Lecture, Music)) + geom_point() + xlab("Lecture") +     ylab("Music Week")

Here is the plot I get:

point plot of dataset-music2

As showed above in the plot, the xlab of the plot doesn't go in the sequence of the numbers as in the dataset. Number 11 and 12 jump in between 1 and 2, instead of staying behind 10.

How can I change the sequence into its natural order?

Jaap
  • 81,064
  • 34
  • 182
  • 193
F.L
  • 11
  • 2
  • Do not post your data as an image, please learn how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). That said, your `Lecture` column is highly probably stored as a character variable (check the difference in alignment with `Music` column). To check the class: `class(dataset$Lecture)`. With `dataset$Lecture <- as.numeric(dataset$Lecture)` you can convert it to a numeric variable. If it is stored as a factor variable, use `as.numeric(as.character(dataset$Lecture))`. After that your plotting code should work fine. – Jaap May 15 '16 at 16:44
  • Thanks for your suggestion!! I will improve next time. – F.L May 15 '16 at 17:59

1 Answers1

0

You have to convert the variable on the x-axis to a factor or at least it should not be of type character since in this case R tries to guess what the natural order of the characters should be

library(ggplot2)

# Example data. Replace with yours
data <- data.frame(a = 1:12, b = sample(c(0, 1)) )

ggplot(data, aes(x = as.factor(a), y = b)) + 
geom_point() + 
labs(x = "Label x", y = "Label y")
Manuel R
  • 3,976
  • 4
  • 28
  • 41