Andrew K.
Andrew K.

Reputation: 21

Is there a way to have a ggplot graph without an unused margin

I've built a simple line plot in ggplot:

library(ggplot2)

mortProb <- c(0,    0.01327,    0.00885,    0.03524,    0.01402,    0.07983,    0.09483,    0.16742,    0.40265)

vs <- c(0.0,    20.7855,    25.138625,  30.8095,    36.221375,  41.250375,  48.75,  58.527875,  100)

VS <- data.frame(vs, mortProb)

ggplot(VS) + geom_line(aes(vs, mortProb))

The graph I get is : enter image description here enter image description here

What I want to get is just the plot inside of the black lines here (lines wouldn't actually show up in the graph. enter image description here enter image description here

Any ideas?

2:

Upvotes: 2

Views: 53

Answers (2)

windyvation
windyvation

Reputation: 551

If you'd like to choose which axis to do this to and/or edit the scale of the axis at the same time, scale_x_continuous() and scale_y_continuous work.

library(ggplot2)
mortProb <- c(0,    0.01327,    0.00885,    0.03524,    0.01402,    0.07983,    0.09483,    0.16742,    0.40265)
vs <- c(0.0,    20.7855,    25.138625,  30.8095,    36.221375,  41.250375,  48.75,  58.527875,  100)
VS <- data.frame(vs, mortProb)
ggplot(VS) + 
  geom_line(aes(vs, mortProb)) + 
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0))

Upvotes: 2

Peter
Peter

Reputation: 12699

Is this what you were looking for?

library(ggplot2)

ggplot(VS) + 
  geom_line(aes(vs, mortProb))+
  coord_cartesian(expand = expansion(mult = 0))


Created on 2021-04-08 by the reprex package (v1.0.0)

Upvotes: 2

Related Questions