2017-06-13 103 views
1

我想绘制一个日期x轴的图(ggplot),其中x轴是在y = 0,但x标签是在底端。它应该看起来或多或少像图中这样的画面:enter image description hereggplot与日期x轴在y = 0和标签在底部

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,但我没有设法得到结果。 随着上面的代码我得到以下情节: enter image description here

最后,我想与在y = 0蜱水平线/轴,我想删除“下x轴”和另外我想有“紧”轴(如第一张图)。

我的数据是这样的:

> 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 
+0

您可以检查什么'is.Date(coe_melt $时间)'返回? – AK88

+0

你说你已经尝试过使用比例尺的东西,但是你没有展示你尝试过的东西。这很有用。据我所知,这是不容易在ggplot,但你可以看看[在这个类似的问题](https://stackoverflow.com/questions/39071002/moving-x-or-y-axis-一起与 - 蜱标签到最中间的一个单ggplot-N)。 – Axeman

回答

2

见下

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

我的答案随着AL14的答案,巴蒂斯特从链接(类似)的问题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) 

enter image description here

对我来说,这是足够接近,感谢您的帮助大家;)