日期 x 轴在 y=0 且标签在底部的 ggplot
ggplot with date-x-axis at y=0 and labels at the bottom
我想制作一个带有日期 x 轴的绘图 (ggplot),其中 x 轴位于 y=0,但 x 标签位于底部。它应该看起来或多或少像这张图片中的图形:
我这样用 hline
试过:
ggplot(coe_melt, aes(x=time, y=value, color=score))+
geom_hline(yintercept=0)+
geom_line(size=2)+
scale_color_manual(values=c('blue','magenta','red','green'), breaks=c('Profitability', 'Growth', 'Safety','Payout'))+
theme_bw()+
theme(legend.position = 'bottom')+
theme(axis.ticks.x = element_blank())
我在几个线程中读到它可以用 scale_x_continuous()
完成,但问题是我的 x 轴包含日期而不是数字。当我用 scale_x_continous()
尝试时,出现错误(未提供来源)。我用 scale_x_date
试过了,但我没能得到结果。
使用上面的代码,我得到以下情节:
最后我想要一个水平 line/axis,在 y=0 处有刻度,我想删除 "lower x axis" 另外我想要 "tight" 轴(比如第一张图)。
我的数据是这样的:
> head(coe_melt)
time score value
1 1977-07-01 Profitability 0.4737371
2 1978-07-01 Profitability 0.4918117
3 1979-07-01 Profitability 0.4249600
4 1980-07-01 Profitability 0.3847234
5 1981-07-01 Profitability 0.3604534
6 1982-07-01 Profitability 0.4012554
> coe_melt[c(1,40,79,118),]
time score value
1 1977-07-01 Profitability 0.47373711
40 1977-07-01 Growth 0.51024065
79 1977-07-01 Safety 0.02525786
118 1977-07-01 Payout -0.12501210
看下面我的回答
ggplot(coe_melt, aes(x=time, y=value, color=score))+
geom_hline(yintercept=0)+
geom_line(size=2)+
scale_color_manual(values=c('blue','magenta','red','green'),
breaks=c('Profitability', 'Growth', 'Safety','Payout'))+
theme_bw()+
theme(legend.position = 'bottom')+
theme(axis.ticks.x = element_blank())+
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank() )+
theme(panel.border= element_blank())+
theme(axis.line.y = element_line(color="black", size = 0.5))+
expand_limits(y=c(-0.4, 0.8))+
scale_y_continuous(breaks=seq(-0.4, 0.8, 0.2))
结合 Al14 的答案和链接(类似)问题中 baptiste 的答案 provided by Axeman,我设法使用以下代码非常接近预期的结果:
shift_axis <- function(p, y=0){
g <- ggplotGrob(p)
dummy <- data.frame(y=y)
ax <- g[["grobs"]][g$layout$name == "axis-b"][[1]]
p + annotation_custom(grid::grobTree(ax, vp = grid::viewport(y=1, height=sum(ax$height))), ymax=y, ymin=y)+
geom_hline(aes(yintercept=y), data = dummy) +
theme(axis.ticks.x=element_blank())+
theme(axis.line.y = element_line(color='black'), axis.text.x = element_blank(), legend.title=element_blank(),
plot.background = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), panel.border= element_blank())
}
colo2 <- c("#E41A1C", "#984EA3", "#377EB8", "#4DAF4A")
p <- ggplot(coe_melt, aes(x=time, y=value, color=score))+geom_line(size=2)+
scale_color_manual(values=colo2, breaks=c('Profitability', 'Growth', 'Safety','Payout'))+
theme_bw()+theme(legend.position = 'bottom', axis.title.x = element_blank(), axis.title.y = element_blank())+
scale_x_date(limit=as.Date(c('1977-07-01', '2015-07-01')), expand=c(0,0))
shift_axis(p, 0)
对我来说已经很接近了,谢谢大家的帮助;)
我想制作一个带有日期 x 轴的绘图 (ggplot),其中 x 轴位于 y=0,但 x 标签位于底部。它应该看起来或多或少像这张图片中的图形:
我这样用 hline
试过:
ggplot(coe_melt, aes(x=time, y=value, color=score))+
geom_hline(yintercept=0)+
geom_line(size=2)+
scale_color_manual(values=c('blue','magenta','red','green'), breaks=c('Profitability', 'Growth', 'Safety','Payout'))+
theme_bw()+
theme(legend.position = 'bottom')+
theme(axis.ticks.x = element_blank())
我在几个线程中读到它可以用 scale_x_continuous()
完成,但问题是我的 x 轴包含日期而不是数字。当我用 scale_x_continous()
尝试时,出现错误(未提供来源)。我用 scale_x_date
试过了,但我没能得到结果。
使用上面的代码,我得到以下情节:
最后我想要一个水平 line/axis,在 y=0 处有刻度,我想删除 "lower x axis" 另外我想要 "tight" 轴(比如第一张图)。
我的数据是这样的:
> head(coe_melt)
time score value
1 1977-07-01 Profitability 0.4737371
2 1978-07-01 Profitability 0.4918117
3 1979-07-01 Profitability 0.4249600
4 1980-07-01 Profitability 0.3847234
5 1981-07-01 Profitability 0.3604534
6 1982-07-01 Profitability 0.4012554
> coe_melt[c(1,40,79,118),]
time score value
1 1977-07-01 Profitability 0.47373711
40 1977-07-01 Growth 0.51024065
79 1977-07-01 Safety 0.02525786
118 1977-07-01 Payout -0.12501210
看下面我的回答
ggplot(coe_melt, aes(x=time, y=value, color=score))+
geom_hline(yintercept=0)+
geom_line(size=2)+
scale_color_manual(values=c('blue','magenta','red','green'),
breaks=c('Profitability', 'Growth', 'Safety','Payout'))+
theme_bw()+
theme(legend.position = 'bottom')+
theme(axis.ticks.x = element_blank())+
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank() )+
theme(panel.border= element_blank())+
theme(axis.line.y = element_line(color="black", size = 0.5))+
expand_limits(y=c(-0.4, 0.8))+
scale_y_continuous(breaks=seq(-0.4, 0.8, 0.2))
结合 Al14 的答案和链接(类似)问题中 baptiste 的答案 provided by Axeman,我设法使用以下代码非常接近预期的结果:
shift_axis <- function(p, y=0){
g <- ggplotGrob(p)
dummy <- data.frame(y=y)
ax <- g[["grobs"]][g$layout$name == "axis-b"][[1]]
p + annotation_custom(grid::grobTree(ax, vp = grid::viewport(y=1, height=sum(ax$height))), ymax=y, ymin=y)+
geom_hline(aes(yintercept=y), data = dummy) +
theme(axis.ticks.x=element_blank())+
theme(axis.line.y = element_line(color='black'), axis.text.x = element_blank(), legend.title=element_blank(),
plot.background = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), panel.border= element_blank())
}
colo2 <- c("#E41A1C", "#984EA3", "#377EB8", "#4DAF4A")
p <- ggplot(coe_melt, aes(x=time, y=value, color=score))+geom_line(size=2)+
scale_color_manual(values=colo2, breaks=c('Profitability', 'Growth', 'Safety','Payout'))+
theme_bw()+theme(legend.position = 'bottom', axis.title.x = element_blank(), axis.title.y = element_blank())+
scale_x_date(limit=as.Date(c('1977-07-01', '2015-07-01')), expand=c(0,0))
shift_axis(p, 0)
对我来说已经很接近了,谢谢大家的帮助;)