是否可以在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"))
如果你在標籤中使用'「\ n」'作爲分隔符,它們應該出現在兩行 – baptiste
另一個選擇是在圖表的底部創建一個表格,該表格可以包含儘可能多的行。要做到這一點,請按照以下示例進行操作:http://learnr.wordpress.com/2009/04/29/ggplot2-labelling-data-series-and-adding-a-data-table/ – amzu
如果其中一個答案對您有幫助,接受它是一種很好的形式 –