0

In the following plot:

enter image description here

The values of the Y axis are 1.9999999925 (for the 99 steps) and 1.9999999882 (for the rest of steps). As you can see, the plot only shows the value 2 because it rounds the decimal of both values to 2. How can I avoid that and show the two values as they are?

This is the R script that I have written to generate the plot:

dataset <- readr::read_csv("/data.csv")
dataset <- dataset %>% melt(id.vars = c("Class"))
dataset <- transform(dataset, value = value)
YaxisTitle <- "Fitness"
class <- "Steamer"
p2_data <- dataset %>% filter(Class == class)
pp2 <- p2_data %>% ggplot(aes(x=factor(variable), y=value, group=Class, colour=Class)) + geom_line() + scale_x_discrete(breaks = seq(0, 1000, 100)) + labs(x = "Steps", y = YaxisTitle) + theme(legend.position="none")
Adam Amin
  • 1,406
  • 2
  • 11
  • 23
  • 1
    @MKBakker none of the two solutions worked !! – Adam Amin Aug 08 '19 at 12:31
  • Without having access to a sample of your data (no one else has the "data.csv" on your computer) so we can run any of your code, it's hard to suggest what would or wouldn't work exactly. The link flagged is a good guess without actually seeing any data – camille Aug 08 '19 at 16:21

1 Answers1

0

If I adjust the solution from the accepted answer in How do I change the number of decimal places on axis labels in ggplot2? I do manage to change the labels.

The key is to change the scale function scaleFUN to allow more than the 2 decimals in the original post to 9 decimals (or any number you need). So from "%.2f" to "%.9f".

df <- data.frame(Steps=1:1000, Fitness=c(rep(1.9999999925,99),rep(1.9999999882,901)))

library(ggplot2)

scaleFUN <- function(x) sprintf("%.9f", x)

ggplot(data=df,aes(x=Steps,y=Fitness)) +
  geom_line() +
  scale_y_continuous(labels=scaleFUN)
P1storius
  • 917
  • 5
  • 12