我很难修改ggplot上的图例以根据自己的喜好进行绘制。
我的数据:
ors_fi = structure(list(col.x = c("Pre-frail", "Frail", "Pre-frail", "Frail",
"Pre-frail", "Frail"), col.estimate = c(0.865018872113144, 1.63924006466768,
0.972589483876898, 1.74300310363782, 0.836325226050668, 1.51301774619739
), col.stderr = c(0.0865502520576455, 0.0991158308454572, 0.0933472713471928,
0.109413945850029, 0.0901760485538073, 0.109136014040399), col.group = c("Wave 1",
"Wave 1", "Wave 2", "Wave 2", "Mean", "Mean")), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
我的代码:
plot_ors_fi = ggplot(data = ors_fi,
aes(x = `col.x`, y = exp(`col.estimate`), group = as.factor(`col.group`))) +
# Plot the point estimates
geom_point(aes(size = 1,
shape = 22,
fill = as.factor(`col.group`)),
stroke = 0.5,
position = position_dodge(width = 0.4)) +
# Plot point estimates text
geom_text(aes(y = exp(`col.estimate`+1.96*`col.stderr`),
label = format(round(exp(`col.estimate`), 2), nsmall = 2)),
vjust = -0.8,
size = 3,
position = position_dodge(width = 0.4)) +
# Set the scale for the size of boxes
scale_radius(guide = "none",
limits = c(0, NA_real_),
range = c(0, 3)) +
# Plot the CIs
geom_linerange(aes(ymin = exp(`col.estimate`-1.96*`col.stderr`),
ymax = exp(`col.estimate`+1.96*`col.stderr`),
colour = "black"),
lwd = 0.5,
position = position_dodge(width = 0.4)) +
# Use identity for aesthetic scales
scale_shape_identity() +
scale_colour_identity() +
# Set the scale for fill colours
scale_fill_grey(start = 0,
end = 1,
limits=c("Wave 1", "Wave 2", "Mean"),
guide="legend",
position = "bottom", ## NOT WORKING?
name=NULL) +
# Set the y-axis scale
scale_y_continuous(trans = "log", breaks = c(1, 2, 4, 8)) +
# Set the x-axis scale
scale_x_discrete(limits = c("Pre-frail", "Frail")) +
# Add titles
xlab("Frailty Index") +
ylab("OR (95% CI)") +
ggtitle("")
您可以看到目前的图例不是很有用。我在SO中找不到类似的问题,其中图例填充色根本不显示?
在此先感谢您的建议和感谢。
答案 0 :(得分:0)
形状需要从aes
标签中移除:
ggplot(data = ors_fi,
aes(x = `col.x`, y = exp(`col.estimate`), group = as.factor(`col.group`))) +
# Plot the point estimates
geom_point(aes(size = 1,
fill = as.factor(`col.group`)),
shape = 22,
stroke = 0.5,
position = position_dodge(width = 0.4))
# etc.
图例位置可以通过主题设置:
# Add titles
xlab("Frailty Index") +
ylab("OR (95% CI)") +
ggtitle("") +
theme(legend.position = "bottom")
希望您正在寻找什么?