For this you can use geom_step
:
df = data.frame(x = 1:10,
y = sample(c(1,2), 10, replace = TRUE))
ggplot(df, aes(x = x, y = y)) + geom_step()
Question
How can I use ggplot2's geom_line
to plot a square function, where x,y coordinates are connected by squares rather than by lines?
Something that looks like:
Is this possible?
Solution
For this you can use geom_step
:
df = data.frame(x = 1:10,
y = sample(c(1,2), 10, replace = TRUE))
ggplot(df, aes(x = x, y = y)) + geom_step()