绘图上的值 - 一个小数位,一个逗号而不是一个点

时间:2016-05-02 10:52:30

标签: r ggplot2

如何让ggplot将值舍入到一个小数位(我的代码中为freq)和逗号而不是点? (例如52.13 - > 52,1)decimal.mark = ","label=comma(freq)library(scales))以更加惯用的方式设置逗号而不是点应该非常简单。问题是我的图表非常复杂(对我来说),我不能自己做。

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

1 个答案:

答案 0 :(得分:1)

您可以使用round中的point?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