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"))