0

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"))

boxplot

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)      

boxplot alternative

  • It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Apr 20 '22 at 17:23
  • This said: You could set the labels of axes and legends via the `scale_...`, e.g. in case of the x axis you could try `+ scale_x_discrete(labels = c(1 = "Pfizer-BioNTech", 2 = "Moderna", 3 = "Oxford-AstraZeneca"))`. To change the title of your legend you could do `+ labs(color = "types of vaccines")`. – stefan Apr 20 '22 at 17:26
  • Thanks stefan, I found an alternative because the function + scale_x_discrete was not working. – Charlotte Juton Apr 21 '22 at 10:46
  • Hi Charlotte. Great. Always the best way to learn. But for the future keep in mind, that this is the reason why people on SO insist on a reproducible example. From images one can only guess what's going wrong and what might be a solution. – stefan Apr 21 '22 at 10:54
  • You're absolutely right, I will add a sample to make it reproducible next time. thanks you!! – Charlotte Juton Apr 22 '22 at 10:18

0 Answers0