2015-04-02 135 views
1

我想展示一個簡短的時間序列,顯示22年間歐洲海洛因緝獲量的異質性。然而,一些年份中包括了不同數量的國家。我想通過在x軸上放置每年的「n = xx」來在圖表中顯示。有誰知道我應該怎麼做?

ggplot2中的自定義座標軸刻度標籤

across_time<- ggplot(by_year, aes(year, value) + 
      geom_errorbar(aes(ymin=value-se, ymax=value+se), width=.4) + 
      geom_line(colour="black", size= 2) + 
      geom_point(size=4, shape=21, fill="white") + # 21 is filled circle 
      xlab("Year") + 
      ylab("Siezures") + 
      ggtitle("Hetrogeniety Across Time") + 
      scale_x_continuous(breaks = round(seq(min(1990), max(2012), by=2))) 
across_time 

這裏是什麼圖形看起來像一個鏈接:

http://imgur.com/XWhBqqi

回答

0

您是否嘗試過使用scale_x_continuous標籤的說法?如果你有一個帶有「xx」的矢量作爲標籤,它應該可以工作。

+0

是的,我有。但是,當我使用標籤參數指定標籤時,我得到錯誤:「提供給連續縮放的離散值」。這是有道理的,因爲我的標籤看起來像這樣:「1990 \\ n = 41」。 有什麼建議嗎? – rajenur 2015-04-03 08:21:01

+0

如果您的數據中的實驗數字是數字,那麼該比例會自動選取爲連續的,因此您需要將其更改爲一個因子以緩解此問題。這是我能想到的唯一正確的atm。 – user3239929 2015-04-04 08:38:43

2

,我發現這是一個解決方案:

#make a list of the lables you want 
lab<- c("1990\nn=26", "1991\nn=29", "1992\nn=30", "1993\nn=32", "1994\nn=36", "1995\nn=35", "1996\nn=33", "1997\nn=38", "1998\nn=36", "1999\nn=39", "2000\nn=39", "2001\nn=40", "2002\nn=38", "2003\nn=40", "2004\nn=39", "2005\nn=41", "2006\nn=42", "2007\nn=43", "2008\nn=44", "2009\nn=41", "2010\nn=41", "2011\nn=41", "2012\nn=42") 
lab<- as.factor(lab) 

#bind our label list to our table 
by_year<-cbind(lab, by_year) 

#make a column of zeros to group by for the line 
by_year$g<- 0 

# Standard error of the mean 
    across_time<- ggplot(by_year, aes(x=lab, y=value)) + 
     geom_errorbar(aes(ymin=value-se, ymax=value+se), width=.4) + 
     geom_line(aes(group=g), colour="black", size= 2) + #notice the grouping 
     geom_point(size=4, shape=21, fill="white") + # 21 is filled circle 
     scale_x_discrete(labels = by_year$lab) + # discrete not continuous 
     xlab("Year & Number of Reporting Countries") + 
     ylab("Total Annual Seizures") + 
     ggtitle("Heterogeneity of Heroin Seizures in Europe") 
across_time 

下面是最終的結果:

相關問題