代码之家  ›  专栏  ›  技术社区  ›  Macter

更改ggplot2中的勾号标签[复制]

  •  0
  • Macter  · 技术社区  · 6 年前

    我想展示一个短时间序列,显示22年来欧洲海洛因缉获量的异质性。然而,在某些年份,有不同数量的国家被包括在内。我想通过在X轴上把每年的“n=xx”显示在图表中。有人知道我该怎么做吗?

    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 回复  |  直到 10 年前
        1
  •  2
  •   grg    10 年前

    我发现这是一个解决方案:

    #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
    

    以下是最终结果:

        2
  •  0
  •   user3239929    10 年前

    你试过在scale_x_continuous中使用label参数吗?如果你有一个带“xx”的向量作为标签,这应该是可行的。