1

I am plotting a graph using ggplot2 in RStudio and I am having a hard time trying to figure out where I have gone wrong in my R codes regarding the sort order of a column in my data set named 'Mth'.

My data set is called data1 and it has only 2 columns, namely Age and Mth

My codes stand as follows:

library(ggplot2)
data1 <- read.csv("myfile.csv", as.is=TRUE, header = TRUE)

Month <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

Month2 <- factor (Month,
              levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
              ordered = TRUE)
mean_age <- data1 %>%
filter(Mth %in% Month2) %>%
group_by(Mth) %>%
summarize(xbar = round(mean(Age, na.rm = TRUE), 0))

mean_age$y <- 0.3

yr_labs <- data.frame(x = 85, y = 0.8,
                  Mth = Month2)
p <- ggplot(subset(data1, Mth %in% Month2), aes(x = Age))
p1 <- p + geom_density(fill = "gray20", color = FALSE,
                   alpha = 0.9, mapping = aes(y = ..scaled..)) +
geom_vline(data = subset(mean_age, Mth %in% Month2),
         aes(xintercept = xbar), color = "white", size = 0.5) +
geom_text(data = subset(mean_age, Mth %in% Month2),
        aes(x = xbar, y = y, label = xbar), nudge_x = 1.2,
        color = "white", size = 3.5, hjust = 1) +
geom_text(data = subset(yr_labs, Mth %in% Month2),
        aes(x = x, y = y, label = Mth)) +
facet_grid(Mth ~ ., switch = "y")

And here is the output of the current codes:

enter image description here

As can be seen, the months (Mth column in my data set) is not respecting the sort order I created above.

What am I doing wrong?

pogibas
  • 27,303
  • 19
  • 84
  • 117
user3115933
  • 4,303
  • 15
  • 54
  • 94
  • You have to reorder the factor levels. Sorting won't affect how ggplot2 displays it. – BLT Sep 16 '17 at 16:11
  • @BLT Thanks for your answer. Haven't I already reordered the factor levels by assigning it to 'Month2' ? – user3115933 Sep 16 '17 at 16:35
  • Looking at your code, I think you actually need `Mth` to be a factor with the right level order. You're not grouping by `Month2` or plotting by it, you're using `Mth`, which I can't see that you've releveled. – BLT Sep 16 '17 at 16:51
  • @user3115933 - Can you copy/paste the output from dput(data1) or dput(head(data1)) into your question so we can reproduce this? – www Sep 16 '17 at 17:13
  • @BLT Thanks. Got your point! – user3115933 Sep 17 '17 at 17:46

1 Answers1

1

Try with

data1$Mth <- factor (data1$Mth,
          levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
          ordered = TRUE)

You need ordered factor in the data you pass to ggplot

p <- ggplot(subset(data1, Mth %in% Month2), aes(x = Age))
CPak
  • 13,260
  • 3
  • 30
  • 48