0

I am currently trying to produce some graphs using geom_step(). Here is my code:

ggplot(usage_data,aes(x = to_regulate_up)) +
  theme_minimal() + 
  geom_step(aes(y = 1 - after_stat(y)), stat = "ecdf", linewidth=0.5, color="black") +
  scale_y_continuous(labels = percent_format()) +
  labs(title = "1 - CDF of used aFRR capacity", x="Used capacity", y="Probability")

When i the output of this plot into an RShiny Application via ggplotly i get what i expected:

correct plot enter image description here

When trying to save the plot to a svg (ggsave(file="/tmp/curve2.svg", plot=plot, width=8, height=4) or when plotting the same thing in RStudio i however get the following result:

incorrect plot enter image description here

As in the curve starts at 100% and then drops down, which is not what i want.

I already tried setting different xlim parameters to make the curve start more to the right but still got the same result.

Thanks for any insight to what might be the problem.

P.S. i dont really think it has anything to do with exporting the plot to a svg, because in the RStudio Plot pane it also looks the same.

stefan
  • 90,330
  • 6
  • 25
  • 51
wejn
  • 3
  • 3
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Jul 18 '23 at 16:53

1 Answers1

0

You need to specify pad = Fin the geom_step()

df <- data.frame(a = runif(200))

ggplot(df,aes(x = a)) + geom_step(aes(y = 1 - after_stat(y)), stat = "ecdf", pad = T) # default value adds point at Inf and -Inf
ggplot(df,aes(x = a)) + geom_step(aes(y = 1 - after_stat(y)), stat = "ecdf", pad = F)

enter image description here

enter image description here

see ?stat_ecdf:

pad: If TRUE, pad the ecdf with additional points (-Inf, 0) and (Inf, 1)

f.lechleitner
  • 3,554
  • 1
  • 17
  • 35