带数据点的 ggplot 箱线图:如何控制图例外观?

ggplot box plot with data points: how to control legend appearance?

我尝试了各种选项,但找不到实现自定义图例外观的方法(除非我将图形导出到 power point 并在那里进行编辑...)。我希望图例看起来像下图所示,想知道这是否完全可行:

我不想对图本身做任何改动:

这是我的示例数据和代码:

df = data.frame(sex = c(1,1,1,1,1, 2,2,2,2,2),
                age_cat = c(1,1,1, 2,2,2,  1,1,1, 2),
                score_type = c(1,2, 1,2, 1,2, 1,2, 1,2),
                score = c(25,28,18,20,30, 37,40,35,43,45))


df$sex <- factor((df$sex))
df$age_cat <- factor((df$age_cat))
df$score_type <- factor((df$score_type))

windows(width=7, height=7)

library(ggplot2)

df %>%
  ggplot( aes(x=score_type, y=score)) +
  geom_boxplot(aes(color=sex),outlier.shape = NA, size=1.5, show.legend=T) +
  geom_point(aes(color=sex, shape = age_cat, group = sex),
             position=position_jitterdodge(dodge.width=0.9), size=3, show.legend=F) +
  scale_color_manual(values=c("#0072B2", "#CC79A7"), name="",
                     labels=c("Male", "Female")) +
  scale_shape_manual(name="", labels=c('Younger', 'Older'),
                     values=c(16, 17)) +
  theme_bw()+
  theme(panel.border = element_blank(), axis.ticks = element_blank(),
        legend.position=c(0.9, 0.65), legend.text=element_text(size=11),
        legend.title=element_text(size=11.5),
        panel.grid.major.x = element_blank() ,
        plot.title = element_text(size=11, face = "bold"),
        axis.title=element_text(size=13),
        axis.text.y = element_text(size=11),
        axis.text.x = element_text(size=11),
        plot.margin = unit(c(0.5,0.2,0,0.2), "cm")) +
  labs(title= "", x = "",y = "Score") +
  scale_y_continuous(breaks=c(0, 20, 40, 60, 80, 100),
                     labels=c('0', '20', '40', '60', '80', '100')) +
  expand_limits(x=5, y=70) +
  scale_x_discrete(labels = c("A", "B")) + 
  coord_cartesian(clip = "off")

您可以通过

达到您想要的结果
  1. geom_point
  2. 中删除 show.legend=FALSE
  3. 使用 guides(shape = guide_legend(override.aes = list(shape = c(1, 2))))
  4. 覆盖要在图例中显示的形状
library(ggplot2)

ggplot(df, aes(x = score_type, y = score)) +
  geom_boxplot(aes(color = sex), outlier.shape = NA, size = 1.5) +
  geom_point(aes(color = sex, shape = age_cat, group = sex),
    position = position_jitterdodge(dodge.width = 0.9), size = 3
  ) +
  scale_color_manual(
    values = c("#0072B2", "#CC79A7"), name = "",
    labels = c("Male", "Female")
  ) +
  scale_shape_manual(
    name = "", labels = c("Younger", "Older"),
    values = c(16, 17)
  ) +
  theme_bw() +
  theme(
    panel.border = element_blank(), axis.ticks = element_blank(),
    legend.position = c(0.9, 0.65), legend.text = element_text(size = 11),
    legend.title = element_text(size = 11.5),
    panel.grid.major.x = element_blank(),
    plot.title = element_text(size = 11, face = "bold"),
    axis.title = element_text(size = 13),
    axis.text.y = element_text(size = 11),
    axis.text.x = element_text(size = 11),
    plot.margin = unit(c(0.5, 0.2, 0, 0.2), "cm")
  ) +
  labs(title = "", x = "", y = "Score") +
  scale_y_continuous(
    breaks = c(0, 20, 40, 60, 80, 100),
    labels = c("0", "20", "40", "60", "80", "100")
  ) +
  expand_limits(x = 5, y = 70) +
  scale_x_discrete(labels = c("A", "B")) +
  coord_cartesian(clip = "off") +
  guides(shape = guide_legend(override.aes = list(shape = c(1, 2))))