我的离散 y 轴的标题不会在 ggplot2 中移动
Posted
技术标签:
【中文标题】我的离散 y 轴的标题不会在 ggplot2 中移动【英文标题】:The title of my discrete y axis won't move in ggplot2 【发布时间】:2022-01-06 03:20:41 【问题描述】:我有以下数据:
library(ggplot2)
library(magrittr)
library(tidyverse)
library(scales)
mydata <-
structure(list(group = c("All participants", "Subgroup 1", "Subgroup 2", "Subgroup 3"),
group_factor = structure(4:1,
.Label = c("Subgroup 3", "Subgroup 2", "Subgroup 1", "All participants"),
class = "factor"),
estimate = c(0.81, 0.74, 0.88, 0.83),
conf_low = c(0.55, 0.35, 0.53, 0.54),
conf_high = c(1.2, 1.58, 1.44, 1.27),
p_value = c(0.3, 0.43, 0.62, 0.38),
label = structure(c(2L, 1L, 4L, 3L),
.Label = c("0.74 (0.35-1.58)", "0.81 (0.55-1.20)", "0.83 (0.54-1.27)", "0.88 (0.53-1.44)"),
class = "factor")),
row.names = c(NA, -4L),
class = c("tbl_df", "tbl", "data.frame"))
# Take a look at data.
head(mydata)
# A tibble: 4 x 7
group group_factor estimate conf_low conf_high p_value label
<chr> <fct> <dbl> <dbl> <dbl> <dbl> <fct>
1 All participants All participants 0.81 0.55 1.2 0.3 0.81 (0.55-1.20)
2 Subgroup 1 Subgroup 1 0.74 0.35 1.58 0.43 0.74 (0.35-1.58)
3 Subgroup 2 Subgroup 2 0.88 0.53 1.44 0.62 0.88 (0.53-1.44)
4 Subgroup 3 Subgroup 3 0.83 0.54 1.27 0.38 0.83 (0.54-1.27)
使用 ggplot 我设法制作了下图,但我似乎无法水平调整 y 轴标题的位置...我希望它位于 y 轴标签上方的中心:
ggplot(data=mydata,
aes(x=estimate,
y=group_factor,
color=group)) +
geom_point(size=4, shape=c(0, 1, 2, 3)) + # Set different shapes for the groups
geom_errorbarh(aes(xmin=conf_low, xmax=conf_high, height=0.2)) +
scale_colour_manual(values=c("black", "black", "black", "black")) +
scale_y_discrete(name="IRR (95%CI)",
labels=c("0.83 (0.54-1.27)", # Apply the correct lables on the Y axis
"0.88 (0.53-1.44)",
"0.74 (0.35-1.58)",
"0.81 (0.55-1.20)")) +
scale_x_continuous(trans=log_trans(), # Set the x axis to log scale
breaks=c(0.3678794, 0.6065307, 1.00, 1.6487213),
labels=c("0.37", "0.60", "1.00", "1.65"),
name="Incidence rate ratio") +
theme(text=element_text(family="Calibri", size=12),
legend.title=element_blank(),
axis.title.y=element_text(angle=0, face="bold", hjust=0.2)) + # Here adjusting hjust does not do anything
guides(color=guide_legend(override.aes=list(shape=c(0, 1, 2, 3))))
【问题讨论】:
【参考方案1】:margin
会解决这个问题
查看最后一行之前的行。
ggplot(data=mydata,
aes(x=estimate,
y=group_factor,
color=group)) +
geom_point(size=4, shape=c(0, 1, 2, 3)) +
geom_errorbarh(aes(xmin=conf_low, xmax=conf_high, height=0.2)) +
scale_colour_manual(values=c("black", "black", "black", "black")) +
scale_y_discrete(name="IRR (95%CI)",
labels=c("0.83 (0.54-1.27)",
"0.88 (0.53-1.44)",
"0.74 (0.35-1.58)",
"0.81 (0.55-1.20)")) +
scale_x_continuous(trans=log_trans(),
breaks=c(0.3678794, 0.6065307, 1.00, 1.6487213),
labels=c("0.37", "0.60", "1.00", "1.65"),
name="Incidence rate ratio") +
theme(text=element_text(family="Calibri", size=12),
legend.title=element_blank(),
axis.title.y=element_text(angle=0, face="bold", hjust=0.2,
margin = margin(r = -70))) + # !! <-- !!
guides(color=guide_legend(override.aes=list(shape=c(0, 1, 2, 3))))
【讨论】:
谢谢!知道为什么它不允许我通过 hjust 进行更改吗?... vjust 工作正常:/【参考方案2】:另一种解决方案是使用将 y 轴标签放在绘图标题 (ggtitle
) 中并使用 hjust
参数。请注意,您还需要删除 y 轴标签 (name=""
)。
ggplot(data=mydata,
aes(x=estimate,
y=group_factor,
color=group)) +
geom_point(size=4, shape=c(0, 1, 2, 3)) +
geom_errorbarh(aes(xmin=conf_low, xmax=conf_high, height=0.2)) +
scale_colour_manual(values=c("black", "black", "black", "black")) +
scale_y_discrete(name="", # HERE
labels=c("0.83 (0.54-1.27)",
"0.88 (0.53-1.44)",
"0.74 (0.35-1.58)",
"0.81 (0.55-1.20)")) +
scale_x_continuous(trans=log_trans(),
breaks=c(0.3678794, 0.6065307, 1.00, 1.6487213),
labels=c("0.37", "0.60", "1.00", "1.65"),
name="Incidence rate ratio") +
ggtitle("IRR (95% CI)") + # HERE
theme(text=element_text(family="Calibri", size=12),
legend.title=element_blank()
plot.title = element_text(hjust = -0.28, face="bold")) + # HERE
guides(color=guide_legend(override.aes=list(shape=c(0, 1, 2, 3))))
这给了你这个:
【讨论】:
以上是关于我的离散 y 轴的标题不会在 ggplot2 中移动的主要内容,如果未能解决你的问题,请参考以下文章
HDU 5862 Counting Intersections (离散化+扫描线+树状数组)
R语言ggplot2包和lattice包可视化改变x轴和y轴的显示位置实战
R语言ggplot2可视化:使用element_text函数设置轴标题文本为粗体字体(Bold Font,只设置Y轴的标题文本使用粗体字体)
R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,使x轴和Y轴的标签文本都使用粗体字体)注意是轴标签而非轴标题
R语言可视化包ggplot2包设置轴断点位置实战(Axis Breaks)即自定义X轴和Y轴的数值标记位置
R语言ggplot2可视化:ggplot2中使用element_text函数设置轴标签文本粗体字体(bold text,只设置y轴的标签文本使用粗体字体)