R do not show y values but keep y axis

Viewed 89

I am trying to reproduce a plot. This is original plot that I want to reproduce:
enter image description here

And This is what I am getting:
enter image description here

Here is the code I use in R:

library(ggplot2)

df = read.table(sep=",",  
                header=T,   
                text="time,bits      
                    1,1
                    2,1
                    2,1
                    2,1
                    2,1
                    5,1
                    5,1
                    5,1
                    6,1
                    6,1
                    6,1
                    6,1
                    6,1
                    6,1
                    7,1
                    7,1
                    7,1
                    7,1
                    7,1
                    7,1
                    7,1
                    7,1")

mapping <- aes(
  x = (time)
)
(ggplot(data=df, mapping=mapping)
  + stat_ecdf(geom = "line")
  + geom_vline(xintercept=df$time, linetype="dotted")
  + theme_bw()
  + labs(x = "time t", y = "bits")
  + scale_x_continuous(breaks = c(1,2,5,6,7))
)

I don't know how to remove the values from the y-axis? Most of the posts that I have found are about not showing the y-axis at all, I just want to hide the values.

2 Answers

You can do some modification in the theme() like so:

mapping <- aes(
        x = (time)
)
(ggplot(data=df, mapping=mapping)
        + stat_ecdf(geom = "line")
        + geom_vline(xintercept=df$time, linetype="dotted")
        + theme_bw()
        + labs(x = "time t", y = "bits")
        + scale_x_continuous(breaks = c(1,2,5,6,7))
)       + theme(axis.ticks.y = element_blank(),
                axis.text.y = element_blank())

enter image description here

Here is a way to have the lines go up to the ECDF line. Compute the line ends in a dplyr pipe.

library(ggplot2)
library(dplyr)

df %>%
  mutate(y = cumsum(bits)/sum(bits)) %>%
  ggplot(mapping = mapping) +
  stat_ecdf(geom = "line", color = "#6400ff", size = 2) +
  geom_segment(aes(x = time, xend = time, y = 0, yend = y), linetype="dotted") +
  geom_text(
    x = 7, y = 1, 
    hjust = 0.5, vjust = -0.5, 
    size = 8,
    label = expression(italic(R)[1]*(t)), 
    family = "serif",
    inherit.aes = FALSE) +
  labs(x = "time", y = "bits") +
  scale_x_continuous(breaks = c(1, 2, 5, 6, 7), limits = c(0, 10)) +
  ylim(0, 1.5) +
  theme_classic() +
  theme(
    axis.title = element_text(size = 20),
    axis.ticks.y = element_blank(),
    axis.text.y = element_blank()
  )

enter image description here

Related