Custom axis tick labels in ggplot2

前端 未结 2 682
眼角桃花
眼角桃花 2021-01-25 15:53

I would like to show a short time series showing heterogeneity of heroin seizures in Europe over the span of 22 years. However there are different amount countries included in s

相关标签:
2条回答
  • 2021-01-25 16:35

    I found this as a solution:

    #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
    

    Here is the final result:

    0 讨论(0)
  • 2021-01-25 16:40

    Have you tried using the label argument in scale_x_continuous? If you have a vector with the "xx" you want as labels this should work.

    0 讨论(0)
提交回复
热议问题