How to position lines in the y-axis closer to each other as well as to the x-axis

Advertisements

This question concerns the same data and graph as in my previous question: How to position lines in the y-axis closer to each other, but the question itself is a bit different.

The data is:

df <- structure(list(vec = c("PC", "DEFAULT", "INFORMED"), mean = c(1.34944359241928, 
                                                      1.36249329506777, 1.34671188869646), sd = c(0.57779881866326, 
                                                                                                  0.537279303541924, 1.53585580464849), min = c(0.196903771571785, 
                                                                                                                                                0.28781509871908, -1.66860228474139), `0.5quant` = c(1.35295469982876, 
                                                                                                                                                                                                     1.36643973099316, 1.34687516700723), max = c(2.48177705326348, 
                                                                                                                                                                                                                                                  2.41483607230639, 4.36109982194179), mode = c(NA_real_, NA_real_, 
                                                                                                                                                                                                                                                                                                NA_real_), kld = c(1.9807860333589e-08, 2.97333113261951e-08, 
                                                                                                                                                                                                                                                                                                                   3.91753938449056e-10), ID = 1:3), row.names = c(NA, -3L), class = "data.frame")

Which I want to plot as:

yaxis <- c(
  "Ppppppppp ppppppp\npppp pp pppppp\n(ppp)",
  "Uuuuuuuuu uuuuu","Iiiiiii iiiii\nii Iiiiii (iiii)") 



ggplot(data = df, aes(x = mean, y = factor(ID))) + geom_point() + geom_errorbarh(aes(xmin = min, xmax = max), height = .1) + geom_vline(xintercept = 0, linetype = 2) + scale_x_continuous(breaks = seq(-4, 4.5, 0.5)) + scale_y_discrete(labels = yaxis) + theme(axis.text.y = element_text(hjust = .5))

As I have written in the picture, I want to position three lines closer to each other, as well as to the x-axis.

In the previous question, Limey suggested:

    + scale_y_discrete(labels = yaxis, expand = expansion(add = 2))

But this increases the distance between the lines and the x-axis.

>Solution :

Either resize the R plot window before you export it to a file, or specify the width and height in ggsave.

ggplot(data = df, aes(x = mean, y = factor(ID))) + 
  geom_point() + 
  geom_errorbarh(aes(xmin = min, xmax = max), height = .1) + 
  geom_vline(xintercept = 0, linetype = 2) + 
  scale_x_continuous(breaks = seq(-4, 4.5, 0.5)) + 
  scale_y_discrete(labels = yaxis) + 
  theme(axis.text.y = element_text(hjust = .5))

ggsave(height=1.5, width=5, file="RPlot.jpg")

Leave a ReplyCancel reply