2017-06-13 99 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

對我來說,這是足夠接近,感謝您的幫助大家;)