2

Is it possible to include multiple lines of axis labels in R charts?

I would like to include two-line labels for x-axis as it appears in the link below – http://www.rita.dot.gov/bts/airfares/national/chart

In my code, I have included all the year-quarter combinations as x-axis labels. But that makes the x-axis labels look quite cluttered.

Is there a way I could make the x-axis labels cleaner and descriptive, preferably as they appear in the chart in the link above?

Here is the code I am using –</p>

national.fare <- read.csv("http://www.rita.dot.gov/bts/airfares/national/csv", 
              header = TRUE)[ , 1:4]

names(national.fare) <- c("Year", "Quarter", "US_Average_Current", "US_Average_Inflation-Adjusted")

# Convert Year and Quarter as characters
national.fare$Year <- as.character(national.fare$Year)
national.fare$Quarter <- as.character(national.fare$Quarter)

# Convert to Long-version
national.fare.long <- melt(national.fare, measure.vars = c("US_Average_Current", "US_Average_Inflation-Adjusted"))

# Combine Year and Quarter for Graph
national.fare.long$Year_Quarter <- as.character(paste(national.fare.long$Year, "Q", national.fare.long$Quarter, sep = ""))


# Chart: National Average Domestic Fare Current and Inflation-Adjusted

p <- ggplot(national.fare.long, aes(x = Year_Quarter, y = value, group = variable))

p + geom_line(aes(color = variable), size = 1.5) +
  scale_color_manual(values = c("navy", "red")) +
  ylim(250, 500) +
  ggtitle("National Average Domestic Fare 1995 - 2013") +
  xlab("Year-Quarter") +
  ylab("Average Domestic Fare") +
  theme(legend.position = "top", 
        legend.key = element_rect(fill = "transparent", color = NA),
        legend.title = element_blank(),
        axis.title = element_text(family = "sans", color = "grey50", face = "bold"),
        axis.line.x = element_line(color = "grey"),
        axis.text.y = element_text(family = "sans"),
        axis.text.x = element_text(family = "sans", face = "plain", hjust = 0, vjust = 1, angle = 285),
        axis.ticks.x = element_line(),
        panel.grid.major.y = element_line(color = "grey", size = 0.5, linetype = "dashed"),
        panel.background = element_rect(fill = "transparent", color = NA),
        plot.background = element_rect(fill = "transparent", color = NA),
        plot.title = element_text(family = "sans", size = 18, face = "bold")) 
4

2 回答 2

1

One solution is to create a column specifically for your labels: I chose to only include every fourth label. Notice also that I have added "\n" in order to put the label on two different lines. Finally you use scale_x_discrete(labels=national.fare.long$Year_Quarter_lab) to add your custom labels.

library(reshape)
library(ggplot2)
national.fare <- read.csv("http://www.rita.dot.gov/bts/airfares/national/csv", 
              header = TRUE)[ , 1:4]

names(national.fare) <- c("Year", "Quarter", "US_Average_Current", "US_Average_Inflation-Adjusted")

# Convert Year and Quarter as characters
national.fare$Year <- as.character(national.fare$Year)
national.fare$Quarter <- as.character(national.fare$Quarter)

# Convert to Long-version
national.fare.long <- melt(national.fare, measure.vars = c("US_Average_Current", "US_Average_Inflation-Adjusted"))

# Combine Year and Quarter for Graph
national.fare.long$Year_Quarter <- as.character(paste(national.fare.long$Year, "Q", national.fare.long$Quarter, sep = ""))
# Create a vector of labels with every fourth label
skip <- 0:(dim(national.fare.long)[1]-1)%%4
national.fare.long$Year_Quarter_lab <- ifelse(skip==0,as.character(paste(national.fare.long$Year, "\nQ", national.fare.long$Quarter, sep = "")),"")


# Chart: National Average Domestic Fare Current and Inflation-Adjusted

p <- ggplot(national.fare.long, aes(x = Year_Quarter, y = value, group = variable))

p + geom_line(aes(color = variable), size = 1.5) +
  scale_color_manual(values = c("navy", "red")) +
  scale_x_discrete(labels=national.fare.long$Year_Quarter_lab) +
  ylim(250, 500) +
  ggtitle("National Average Domestic Fare 1995 - 2013") +
  xlab("Year-Quarter") +
  ylab("Average Domestic Fare") +
  theme(legend.position = "top", 
        legend.key = element_rect(fill = "transparent", color = NA),
        legend.title = element_blank(),
        axis.title = element_text(family = "sans", color = "grey50", face = "bold"),
        axis.line.x = element_line(color = "grey"),
        axis.text.y = element_text(family = "sans"),
        axis.text.x = element_text(family = "sans", face = "plain", hjust = .5, vjust = 1, angle = 0),
        axis.ticks.x = element_line(),
        panel.grid.major.y = element_line(color = "grey", size = 0.5, linetype = "dashed"),
        panel.background = element_rect(fill = "transparent", color = NA),
        plot.background = element_rect(fill = "transparent", color = NA),
        plot.title = element_text(family = "sans", size = 18, face = "bold")) 
于 2013-08-28T19:24:27.603 回答
0

If you convert the quarters into dates, you can easily change the scale of the axis to whatever you think looks good.

To change Quarters into dates (I'm saying Q1 = Jan 1st, change as you wish):

library(gsubfn)
national.fare.long$Year_Q_Date <- gsubfn("Q.*", 
    list('Q1' = '-01-01', 'Q2' = '-04-01', 'Q3' = '-06-01', 'Q4' = '-10-01'), 
    national.fare.long$Year_Quarter)
national.fare.long$Year_Q_Date <- as.Date(national.fare.long$Year_Q_Date)
firstObs <- national.fare.long$Year_Q_Date[1]
lastObs <- national.fare.long$Year_Q_Date[length(national.fare.long$Year_Q_Date)]

Then choose your axis granularity:

dateticks <- seq(firstObs, lastObs, by="8 month")

And now make some slight changes to your plot:

library(scales)

p <- ggplot(national.fare.long, aes(x = Year_Q_Date, y = value, group = variable))

p + geom_line(aes(color = variable), size = 1.5, ) +
  scale_color_manual(values = c("navy", "red")) +
  ylim(250, 500) +
  ggtitle("National Average Domestic Fare 2001 - 2012") +
  xlab("Year-Quater") +
  ylab("Average Domestic Fare") +
  scale_x_date(breaks=dateticks, labels=date_format("%Y %b")) +
  theme(legend.position = "top", 
      legend.key = element_rect(fill = "transparent", color = NA),
      legend.title = element_blank(),
      axis.title = element_text(family = "sans", color = "grey50", face = "bold"),
      axis.line.x = element_line(color = "grey"),
      axis.text.y = element_text(family = "sans"),
      axis.text.x = element_text(family = "sans", face = "plain", hjust = 0, vjust = 1, angle = 285),
      axis.ticks.x = element_line(),
      panel.grid.major.y = element_line(color = "grey", size = 0.5, linetype = "dashed"),
      panel.background = element_rect(fill = "transparent", color = NA),
      plot.background = element_rect(fill = "transparent", color = NA),
      plot.title = element_text(family = "sans", size = 18, face = "bold")) 
于 2013-08-28T19:24:31.973 回答