我正在构建刻面线图并想修改轴标签。具体来说,我想通过完全标记每个轴的起始范围,然后使用速记标签格式来消除所有后续标签,从而消除视觉冗余。
对于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')
谢谢!
答案 0 :(得分:1)
您可以提前完成所需的休息时间。我删除了格式化YEAR
列的部分,因为我发现使用连续而非日期比例更容易。
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 = c(0,10,20,30,40,"50%"),limits = c(0,.5))
gg <- gg + scale_x_continuous(breaks = seq(2007,2015), labels = c(2007, "08","09","10","11","12","13","14","15"))
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')
您可以轻松地将其扩展为一个函数,该函数将根据数据构建标签并构建图。
答案 1 :(得分:1)
基于@Jake Kaupp的回答,混合标签可以表达为如下函数:
library(magrittr)
library(stringr)
label_pct <- function(breaks){
b <- breaks
b_max <- max(b) %>% scales::percent()
b_others <- b[(!(b %in% max(b)))]*100 %>% as.integer()
b_final <- c(b_others,b_max)
return(b_final)
}
label_yr <- function(breaks){
b <- breaks
b_min <- min(b)
b_others <- b[(!(b %in% b_min))] %>% str_sub(3,4) %>% paste0("'",.)
b_final <- c(b_min,b_others)
return(b_final)
}
现在可以将它们插入到Jake提供的代码中,并在其他图中重复使用
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 = label_pct(seq(0,.5,.1)),limits = c(0,.5))
gg <- gg + scale_x_continuous(breaks = seq(2007,2015), labels = label_yr(seq(2007,2015)))
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')
唯一突出的问题是y轴标签中的数字与顶部标签的数字位置不完全对齐,但我现在称其为“足够好”。