leoncio
February 17, 2022, 9:08pm
1
Hello,
is there a way to add legend to such a plot, with shape of circle noted as mean and diamond as median? Moreover, why median cannot be moved a bit to a side with hjust?
library(tidyverse)
ggplot(starwars,aes(sex, height)) +
stat_summary(fun.data = "mean_cl_boot", colour = "grey36", size = 1) +
stat_summary(fun=median, color="black", size=4, geom="point", shape=5, alpha=1) +
labs(title = paste0("test"), x = NULL, y = NULL, shape = "Legend") +
theme_classic()
joels
February 17, 2022, 10:19pm
2
Even though you're not mapping a data column to the colour or shape aesthetics, you can create "dummy" aesthetics for "mean", and "median" that will generate a legend. To dodge the points, you can use position_nudge()
. For example:
library(tidyverse)
ggplot(starwars,aes(sex, height)) +
stat_summary(fun.data = "mean_cl_boot", aes(colour="mean", shape="mean"),
size = 1, position=position_nudge(x=-0.1)) +
stat_summary(fun=median, aes(colour="median", shape="median"),
size=4, geom="point", alpha=1, position=position_nudge(x=0.1)) +
labs(title = paste0("test"), x = NULL, y = NULL, colour=NULL, shape=NULL) +
theme_classic() +
scale_colour_manual(values=c("blue", "red")) +
scale_shape_manual(values=c(16, 5))
Created on 2022-02-17 by the reprex package (v2.0.1)
leoncio
February 18, 2022, 10:10am
3
Many thanks! I was just about to give up and go for preparing a data frame with calculated statistics to simply plot them ;).
leoncio
February 18, 2022, 1:27pm
5
BTW why is that line going through the diamond and dot in legend?
leoncio
February 18, 2022, 2:29pm
6
Ok, with google I found some solution - adding show.legend = FALSE to the stat_summary with meanCI
system
Closed
February 25, 2022, 2:29pm
7
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.