Reputation: 177
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
pppp pp pppppp
(ppp)", "Uuuuuuuuu uuuuu", "Iiiiiii iiiii
ii Iiiiii (iiii)")
myplot <- ggplot(data = df, aes(x = mean, y = 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_continuous(breaks = 1:3, labels = yaxis)
My questions are the following:
I want to reduce the distance between 'i', 'u' and 'p' in the y-axis: in other words, the white lines have to be gone (correspondingly I crossed them out in the graph with a red marker), and three black lines (credible/confidence intervals) have to be positioned closer in the y-axis.
I want to center the text in the labels in the y-axis (I circled them with a blue marker). In other words, the text in each label has to be centered.
Guys, could you help me out with this?
Upvotes: 1
Views: 51
Reputation: 124983
Convert the variable mapped on y
to a factor and center the the axis text using hjust= .5
. Additionally I added linebreaks "\n"
to break the labels into multiple lines.
library(ggplot2)
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))
Upvotes: 2