我正在構建一個分面線圖,並且想要修改軸標籤。具體來說,我想通過完全標記每個軸的起始範圍來消除視覺冗餘,然後對所有後續標籤使用速記標籤格式。ggplot2:軸標籤中的混合格式
對於x軸,我希望標籤以全年開始(%Y
),否則只顯示以撇號開頭的十年(%y
)。對於y軸,我希望上面的範圍標籤後跟一個'%'符號,並且所有其他標籤只顯示該數字。
這裏的背後這篇文章的靈感(注意y軸標籤的精心調整):
我的情節是多方面的,雖然我不認爲應該的問題,對於這個問題:
df <-
tribble(
~YEAR, ~FPL_100PCT, ~RENT, ~GEOG, ~ALL,
2015, .270, .223, 'Seattle', .152,
2014, .212, .225, 'Seattle', .148,
2013, .181, .217, 'Seattle', .145,
2012, .166, .199, 'Seattle', .126,
2011, .236, .238, 'Seattle', .145,
2010, .241, .249, 'Seattle', .156,
2009, .246, .247, 'Seattle', .141,
2008, .187, .216, 'Seattle', .139,
2007, .226, .232, 'Seattle', .142,
2006, .233, .249, 'Seattle', .155,
2015, .200, .210, 'KC', .122,
2014, .186, .207, 'KC', .118,
2013, .201, .215, 'KC', .124,
2012, .189, .209, 'KC', .116,
2011, .208, .230, 'KC', .119,
2010, .207, .233, 'KC', .126,
2009, .206, .244, 'KC', .121,
2008, .198, .226, 'KC', .116,
2007, .210, .229, 'KC', .120,
2006, .226, .232, 'KC', .126
) %>%
mutate(YEAR = as.Date(as.character(YEAR),format = "%Y"),
YEAR = floor_date(YEAR, unit = 'year'))%>%
gather(TBL,ESTIMATE,FPL_100PCT, RENT, ALL)
gg <- ggplot(data = df, aes(x = YEAR, y = ESTIMATE, color = TBL))
gg <- gg + geom_line()
gg <- gg + scale_y_continuous(breaks = seq(0,.5,.1),labels = scales::percent(seq(0,.5,.1)),limits = c(0,.5))
gg <- gg + scale_x_date(date_breaks = '1 year', date_labels = '%y')
gg <- gg + facet_grid(. ~GEOG)
gg <- gg + theme_minimal()
gg <- gg + theme(axis.title = element_blank(),
legend.title = element_blank(),
panel.grid.minor.x = element_blank())
gg <- gg + labs(title = 'Mobility')
謝謝!
將軸文本與主題元素對齊應該注意這一點。 –