How to set the color and size manually in Box plot using R studio?

Dear all,
I want to change the color and the size of my box plot manually.
I used this command " scale_fill_manual" but it did not work.
Here is my script,

library(datasets)
library(ggplot2)
library(multcompView)
library(dplyr)
library(datasets)

Data = read.csv("datastrain.csv", h= TRUE)


#anova analysis
anova <- aov(Ratio ~ Strain, data = Data)
summary(anova)

# Tukey's test
tukey <- TukeyHSD(anova)
print(tukey)


# compact letter display
coba = multcompLetters4(anova, tukey)
print(coba)


# table with factors and 3rd quantile
Tk <- group_by(Data, Strain) %>%
  summarise(mean=mean(Ratio), quant = quantile(Ratio, probs = 0.75)) %>%
  arrange(desc(mean))

# extracting the compact letter display and adding to the Tk table
coba <- as.data.frame.list(coba$Strain)
Tk$coba <- coba$Letters
print(Tk)

p = ggplot(Data, aes(Strain, Ratio, col= Strain)) + 
  geom_boxplot(aes(fill = Strain), show.legend = FALSE) +labs(x="Strain", y="Ratio") +
  theme_bw() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  geom_text(data = Tk, aes(x = Strain, y = quant, label = coba), size = 5, vjust=-1, hjust =-2)  + scale_color_manual(values=c("purple", "#4CBB17","#0096FF","#FFC300", "grey", "black", "red"))

here is my data data

I hope someone can help me.

Thank!

Is this close to what you want? I do not have the package mutcompView so I skipped that part of the code.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
Data = read.csv("~/R/Play/dsatastrain.csv", h= TRUE)
Tk <- group_by(Data, Strain)  |> 
  summarise(mean=mean(Ratio), quant = quantile(Ratio, probs = 0.75)) |> 
  arrange(desc(mean))

ggplot(Data, aes(Strain, Ratio )) + #col= Strain
  geom_boxplot(aes(fill = Strain), show.legend = FALSE) +
  labs(x="Strain", y="Ratio") +
  theme_bw() + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank())+ 
  scale_fill_manual(values=c("purple", "#4CBB17","#0096FF",
                             "#FFC300", "grey", "black", "red"))

Created on 2022-07-23 by the reprex package (v2.0.1)

4 Likes

THANK YOU SO MUCH SIR

1 Like

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.