John Tarr
John Tarr

Reputation: 727

ggplot data year over year - axis labels not showing

I am trying to get data to plot year over year, exactly as is shown in the answer to this question. Unfortunately, it looks as though ggplot has changed since then, and the answer no longer works correctly.

If you try out the below code, you'll notice that the x axis labels no longer exist, compared to the image in the answer.

How can this answer be modified to show the abbreviated month names on the x axis?

library(ggplot2)
# Sample data
data <- read.table(text = "Month             Marg            Fiscal.Year
                           2009-01-01        20904494        2009
                           2009-02-01        30904494        2009
                           2009-03-01        40904494        2009
                           2009-04-01        30904494        2009
                           2009-05-01        43301981        2009
                           2009-06-01        14004552        2009
                           2009-07-01        24004552        2009
                           2009-08-01        34004552        2009
                           2009-09-01        44004552        2009
                           2009-10-01        54004552        2009
                           2009-11-01        64004552        2009
                           2009-12-01        44004552        2009
                           2012-02-01        58343271        2012
                           2012-03-01        68343271        2012
                           2012-04-01        58343271        2012
                           2012-05-01        58343271        2012
                           2012-06-01        38723765        2012
                           2012-07-01        77246753        2012",
                   header=TRUE, sep="", nrows=18)
data$MonthN <- as.numeric(format(as.Date(data$Month),"%m")) # Month's number
data$Month  <- months(as.Date(data$Month), abbreviate=TRUE) # Month's abbr.

g <- ggplot(data = data, aes(x = MonthN, y = Marg, group = Fiscal.Year, colour=Fiscal.Year)) + 
     geom_line() +
     geom_point() +
     scale_x_discrete(breaks = data$MonthN, labels = data$Month)
g

Upvotes: 4

Views: 11745

Answers (2)

eipi10
eipi10

Reputation: 93811

The x-labels disappeared due to the use of scale_x_discrete instead of scale_x_continuous. But you don't need to do any transformation of the Month values outside of ggplot. Instead, you can use Month directly and transform it with the lubridate month function to get the month labels:

library(lubridate)

ggplot(data, aes(x=month(Month, label=TRUE, abbr=TRUE), 
                 y=Marg, group=Fiscal.Year, colour=factor(Fiscal.Year))) + 
  geom_line() +
  geom_point() +
  labs(x="", colour="Fiscal Year")

enter image description here

Upvotes: 5

Kristofersen
Kristofersen

Reputation: 2806

I think this is what you're looking for.

g <- ggplot(data = data, aes(x = MonthN, y = Marg, group = Fiscal.Year, colour=Fiscal.Year)) + 
  geom_line() +
  geom_point() +
  scale_x_continuous(breaks = data$MonthN, labels = data$Month)
g

Upvotes: 1

Related Questions