1

Following a former question I opened few weeks ago: Slope Chart - ggplot2 I face another issue, concerning the numeric values reported in the graph. Even specifying the decimal digits I need (exactly 3) with any of the two commands:

y=round(y, digit = 3) at the endof the code

or

options(digits=3) at the beginning of the whole code

The graphical output, doesn't give me the desired number of digits but only concerning 0. In the graph, I wanted to have 0.800 (not 0.8) and 0.940 (not 0.94). It looks like it removes 0. Below the graphical output from R, I circled in red the number I intended to change.

Graphical output

Below the whole code:

library(dplyr)
library(ggplot2)
#options(digits=3)


theme_set(theme_classic())


#### Data
df <- structure(list(group = c("Ups", "Ups", "Ups", "Ups", "Ups"), 
  yshift = c(0, 0, 0, 0, 0), x = structure(1:5, .Label = c("1 day", 
  "2 days", "3 days", "5 days", "7 days"), class = "factor"), 
  y = c(0.108, 0.8, 0.94, 1.511, 1.905), ypos = c(0.10754145, 
  0.8, 0.94, 1.5111111, 1.90544651164516)), row.names = c(1L, 
  3L, 5L, 7L, 9L), class = "data.frame")    


# Define functions. Source: https://github.com/jkeirstead/r-slopegraph

plot_slopegraph <- function(df) {
    ylabs <- subset(df, x==head(x,1))$group
    yvals <- subset(df, x==head(x,1))$ypos
    fontSize <- 3
    gg <- ggplot(df,aes(x=x,y=ypos)) +
        geom_line(aes(group=group),colour="grey80") +
        geom_point(colour="white",size=8) +
        geom_text(aes(label=y), size=fontSize, family="American Typewriter") +
        scale_y_continuous(name="", breaks=yvals, labels=ylabs)
    return(gg)
}    

    
## Plot
plot_slopegraph(df) + labs(title="Monomer content after days of heating")

I am making any mistake or missing something? Is there any other way to force 0 digits?

Thank you in advance for every eventual reply or comment.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
GiacomoDB
  • 369
  • 1
  • 10
  • 2
    I appreciate your reproducible example, but I would encourage you to make **minimal** examples. Surely you don't need to share almost 100 lines of code creating and transforming data to illustrate this problem. Nor do all your `theme()` options matter for this issue. Next time skip all the transformation and just give us `dput(df)` for the data right before you plot it. You can also skip the `tufte_sort` function by giving us the transformed data directly, instead of giving us data and then transforming it. – Gregor Thomas Jan 10 '22 at 14:27
  • 1
    (Note that I modified your question based on the above advice, if you click the "Edited X mins ago" link you can see a side-by-side comparison of how much code could be cut. Shorter questions are simpler to read and get faster help.) – Gregor Thomas Jan 10 '22 at 14:41

1 Answers1

4

I like the scales package functions for things like this (though you could certainly use formatC or sprintf instead).

I've modified plot_slopegraph to use label=scales::label_number(accuracy = 0.001)(y)) in the geom_text():

plot_slopegraph <- function(df) {
    ylabs <- subset(df, x==head(x,1))$group
    yvals <- subset(df, x==head(x,1))$ypos
    fontSize <- 3
    gg <- ggplot(df,aes(x=x,y=ypos)) +
        geom_line(aes(group=group),colour="grey80") +
        geom_point(colour="white",size=8) +
        geom_text(aes(label=scales::label_number(accuracy = 0.001)(y)), size=fontSize, family="American Typewriter") +
        scale_y_continuous(name="", breaks=yvals, labels=ylabs)
    return(gg)
}    
plot_slopegraph(df)

enter image description here

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • 2
    Or just `geom_text(aes(label=sprintf("%.3f", y)), size=fontSize, family="American Typewriter")`. – Roland Jan 10 '22 at 14:42
  • Thank you roland, I read the guide, this is `sprintf` package, Double precision value, “fixed point”. – GiacomoDB Jan 10 '22 at 15:46
  • The solution you both gave me are working. I didn't know a lot about `sprintf` package but on `scales` one it was easier to me. I used in the past to break axis scale. Thank you for your solutions. Next time, I will add the dataframe needed right before calling `ggplot`. Thank you again for your help. – GiacomoDB Jan 10 '22 at 15:51
  • 1
    Just a minor point, `sprintf` is part of base R, it is not a separate package. That's a good thing for using it. The `scales` package is installed automatically because `ggplot2` uses it. I find it easier to remember how to use `scales` functions without looking them up every time, which is why I prefer it - but `sprintf` is probably a bit more efficient. – Gregor Thomas Jan 10 '22 at 15:57