2013-08-28 183 views
2

是否可以在R圖中包含多行軸標籤?ggplot2中x軸的多行標籤

我想包括x軸兩線的標籤,因爲它出現在下面的鏈接 - http://www.rita.dot.gov/bts/airfares/national/chart

在我的代碼,我已經包括了所有的全年季度組合爲x軸標籤。但是,這使得X軸標籤看起來非常混亂。

有沒有一種方法可以使x軸標籤更清晰和描述性,最好是它們出現在上面鏈接的圖表中?

這裏是我使用的代碼 -

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

如果你在標籤中使用'「\ n」'作爲分隔符,它們應該出現在兩行 – baptiste

+0

另一個選擇是在圖表的底部創建一個表格,該表格可以包含儘可能多的行。要做到這一點,請按照以下示例進行操作:http://learnr.wordpress.com/2009/04/29/ggplot2-labelling-data-series-and-adding-a-data-table/ – amzu

+0

如果其中一個答案對您有幫助,接受它是一種很好的形式 –

回答

1

一種解決方案是創建一個特定的列您的標籤:我選擇只包括每月的第四個標籤。另請注意,爲了將標籤放在兩條不同的線上,我添加了「\ n」。最後,您使用scale_x_discrete(labels=national.fare.long$Year_Quarter_lab)添加您的自定義標籤。

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

如果您將宿舍轉換爲日期,您可以輕鬆地將軸的比例更改爲您認爲看起來不錯的任何值。

要更改宿舍到時間(我說的Q1 = 1月1日,改變你的願望):

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

然後選擇您的軸粒度:

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

現在做一些輕微改變你的情節:

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