2016-05-02 22 views
2

如何讓ggplot將值舍入爲一位小數(我的代碼中的freq)和逗號而不是點? (例如52.13 - > 52,1)用逗號代替點應該很簡單,用decimal.mark = ","或用更通俗的方式用label=comma(freq)(用library(scales))。事情是,我的圖很複雜(對我來說),我不能自己做。在plot上的值 - 小數點後一位,逗號而不是一個點

library(ggplot2) 
library(scales) 

df <- data.frame(years=c(1991, 1993, 1997, 2001, 2005, 2007, 2011, 2015), 
       freq=c(43.20, 52.13, 47.93, 46.29, 40.57, 53.88, 48.92, 50.92)) 

p <- (ggplot(df, aes(x=years, y=freq, label=freq)) + 
     geom_line(size=.7, color="#999999") + geom_point(size=3, color="black") + 
     geom_text(vjust=c(2, -1, -1.5*sign(diff(diff(df$freq))) + 0.5)) + 
     theme_bw() + 
     theme(panel.border=element_blank(), panel.grid.minor=element_blank(), 
      axis.title.y=element_text(vjust=1.25)) + 
     scale_x_continuous("", breaks=seq(1990, 2015, 5), minor_breaks=NULL) + 
     scale_y_continuous("", limits=c(0, 60), 
         breaks=seq(0, 60, 10), minor_breaks=NULL)) 
p 
+0

你就不能圓的值事先'$ DF頻率< - 圓(DF $頻率,位數= 1)'? – mtoto

回答

1

您可以使用roundpoint?comma_format

point <- format_format(big.mark = ".", decimal.mark = ",", scientific = FALSE) 

p <- (ggplot(df, aes(x=years, y=freq, label=point(round(freq,1)))) + 
     geom_line(size=.7, color="#999999") + 
     geom_point(size=3, color="black") + 
     geom_text(vjust=c(2, -1, -1.5*sign(diff(diff(df$freq))) + 0.5)) + 
     theme_bw() + 
     theme(panel.border=element_blank(), panel.grid.minor=element_blank(), 
       axis.title.y=element_text(vjust=1.25)) + 
     scale_x_continuous("", breaks=seq(1990, 2015, 5), minor_breaks=NULL) + 
     scale_y_continuous("", limits=c(0, 60), 
          breaks=seq(0, 60, 10), minor_breaks=NULL)) 

enter image description here

相關問題