brt381 brt381 - 7 days ago 6
R Question

R ggplot2 reduce space between categories in line plot

I have the following data:

Name Value Type
One 500 Confirmed
One 1000 Total
Two 550 Confirmed
Two 900 Total
Three 550 Confirmed
Three 800 Total


And the following R script:

#!/usr/bin/env Rscript

library(ggplot2)

data = structure(list(Name = c("One", "One", "Two", "Two", "Three",
"Three"), Value = c(500L, 1000L, 550L, 900L, 550L, 800L), Type = c("Confirmed",
"Total", "Confirmed", "Total", "Confirmed", "Total")), .Names = c("Name",
"Value", "Type"), class = "data.frame", row.names = c(NA, -6L
))

ggplot(data=data, aes(x=Value, y=Name, group=Name, colour=Type)) + geom_line(color="black") + geom_point()


Which produces the following plot:

Generated image

My question is, how can I reduce the vertical space between the three categories (One, Two, Three)? They seem needlessly far apart.

Answer

Change the aspect-ratio/size of your plot output.

Here is an example that uses your same code:

enter image description here

Would it be better to have the lines close together, and lots of space to the outside? I doubt it, but if you add

coord_cartesian(ylim = c(-9,13))

to your plot, you can get this:

enter image description here