数据为:
df <- structure(list(country = c("Italy", "Switzerland", "Sweden",
"Netherlands", "England", "Poland", "Russia", "Czech", "Hungary",
"Turkey"), `0.025quant` = c(-2.98, 2.02, 1.02, 0.02, 3.02, -2.98,
-0.98, -1.98, -1.98, -0.98), mean = c(-2, 3, 2, 1, 4, -2, 0,
-1, -1, 0), `0.975quant` = c(-1.02, 3.98, 2.98, 1.98, 4.98, -1.02,
0.98, -0.02, -0.02, 0.98)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L))
我将其绘制为:
plot <- ggplot(data = df, aes(x = mean, y = country)) + geom_point() + geom_errorbarh(aes(xmin = 0.025quant, xmax = 0.975quant), height = .1) + geom_vline(xintercept = 0, linetype = 2) + scale_x_continuous(breaks = seq(-3, 5, 1))
我试图在绘图内移动 y 轴上的标签(国家名称,下图中用蓝色标记圈出),因此它将位于其原始默认位置的右侧和上方,略高于白线。在下图中,我用红色标记写下了想要的结果,表示所有国家的名称都要去哪里。
你们能帮我解决这个问题吗,伙计们?
我认为最简单的方法是将默认主题更改为“最小”,以便标签看起来就像在绘图区域内一样。
ggplot(data = df, aes(x = mean, y = country)) +
geom_point() +
geom_errorbarh(aes(xmin = `0.025quant`, xmax = `0.975quant`), height = .1) +
geom_vline(xintercept = 0, linetype = 2) +
scale_x_continuous(breaks = seq(-3, 5, 1)) +
theme_minimal()
如果你真的想将y轴刻度标签放在绘图区域内,我想你可以关闭轴并使用
geom_text
:
ggplot(data = df, aes(x = mean, y = country)) +
geom_point() +
geom_errorbarh(aes(xmin = `0.025quant`, xmax = `0.975quant`), height = .1) +
geom_vline(xintercept = 0, linetype = 2) +
scale_x_continuous(breaks = seq(-3, 5, 1)) +
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
xlim(c(-5,5)) +
geom_text(x=-5, y=1:10, label=df$country, hjust=0)