I have this boxplot but I would like to change the legend for the actual name: 1 = "Pfizer-BioNTech", 2 = "Moderna", 3 = "Oxford-AstraZeneca". This variable is coded as a factor this way so I don't understand why it shows the numbers instead of the name.
Also instead of having tipo_vacuna I would like to put: types of vaccines.
Any hep? thanks in advance
meansbx1<-aggregate (zBMI ~ tipo_vacuna, data = df2, mean)
print (meansbx1)
means1 <- meansbx1 %>%
mutate_if(is.numeric,
round,
digits = 2)
## boxplot 1 zBMI by types of vaccines
bx01<-ggplot(df2, aes(x=tipo_vacuna, y=zBMI)) + geom_boxplot(aes(color=tipo_vacuna), show.legend = TRUE, notch = TRUE) +
stat_summary(fun = mean, colour="red", geom="point", shape=18, size=3, show.legend=TRUE) + geom_text(data = means1, aes(label = zBMI, y = zBMI), nudge_y = 0.4)
bx11<-print(ggplot2.customize(bx01,
mainTitle = "BMI z-score by types of vaccines",
xtitle = "Types of vaccines", ytitle = "zBMI"))
Solution alternative: no need of the legend because it's redundant with x scale
## calculate mean zBMI by types of vaccines
meansbx1<-aggregate (zBMI ~ tipo_vacuna, data = df2, mean)
print (meansbx1)
## Round the mean to 2 decimals
means1 <- meansbx1 %>%
mutate_if(is.numeric,
round,
digits = 2)
## boxplot 1 zBMI by types of vaccines
bx01<-ggplot(df2, aes(x= tipo_vacuna, y=zBMI)) + geom_boxplot(aes(color=tipo_vacuna), show.legend = TRUE, notch = TRUE) +
stat_summary(fun = mean, colour="red", geom="point", shape=18, size=3, show.legend=FALSE) + geom_text(data = means1, aes(label = zBMI, y = zBMI), nudge_y = 0.4)
bx11<-bx01 + labs(title="BMI z-score by types of vaccines", x="Types of vaccines", y="BMI z-score",col="Types of vaccines")
bx111<-bx11 + scale_x_discrete (labels=c('1'='Pfizer-BioNTech', '2'='Moderna','3'='Oxford-AstraZeneca'))
bx1111<- bx111 + theme(legend.position="none")
print(bx1111)