3

Can I set mean values to 2 decimal places?

fun_mean <- function(x){return(data.frame(y=mean(x),label=mean(x,na.rm=T)))}

goo = ggplot(dataset1, aes(x=Pleace, y=Scored.Probabilities)) +
      geom_boxplot(aes(fill=Pleace)) +
      stat_summary(fun.y = mean, geom="point",colour="darkred", size=3) +
      stat_summary(fun.data = fun_mean, geom="text", vjust=-0.7) 
print (goo)
Jeffery Chen
  • 323
  • 2
  • 4
  • 13
  • 3
    Not reproducible. Please add the extra packages you used, as well as a dataset. –  Apr 24 '15 at 03:36

1 Answers1

4

use round(x, digits = 2)

fun_mean <- function(x){return(round(data.frame(y=mean(x),label=mean(x,na.rm=T)),digit=2))}
goo <- ggplot(dataset1, aes(x=Pleace, y=Scored.Probabilities)) +
geom_boxplot(aes(fill=Pleace)) +
stat_summary(fun.y = mean, geom="point",colour="darkred", size=3) +
stat_summary(fun.data = fun_mean, geom="text", vjust=-0.7)

goo

Reference: https://stat.ethz.ch/R-manual/R-devel/library/base/html/Round.html

enter image description here

Ashvin Meena
  • 309
  • 1
  • 13
  • It occurs an error:'fun.data = round(fun_mean,digit = 2)' -> non-numeric argument to mathematical function – Jeffery Chen Apr 24 '15 at 04:11
  • @JefferyChen Post some reproducible example and try using `round` in `ggplot(dataset1, aes(x=round(Pleace,digit = 2), y=Scored.Probabilities))` Until you give me a sample dataset, it will be hit and trail. – Ashvin Meena Apr 24 '15 at 04:16
  • Sample dataset :[link](https://drive.google.com/file/d/0B0zK9xcdOi1sdTBjMzZhV3Q4X2c/view?usp=sharing) THX – Jeffery Chen Apr 24 '15 at 04:31
  • 1
    @JefferyChen Please check now and accept the answer if it solves your problem :) – Ashvin Meena Apr 24 '15 at 04:54
  • THX. I have another question. If I change column "Pleace" to "Persons", it will occur error"'x' and 'units' must have length > 0". – Jeffery Chen Apr 24 '15 at 05:50
  • @JefferyChen Because `Persons` data is numeric. use `+ geom_boxplot(aes(fill=as.factor(Persons)))` Refer: http://stackoverflow.com/questions/16569489/when-using-geom-histogram-there-is-error-unittic-pos-c-mm-x-and-uni – Ashvin Meena Apr 24 '15 at 06:01