0
$\begingroup$

I am using below code to plot multiple box plot but I am still struggling to understand how I can add whisker to box plot and how to change sample order like in this figure SampleType?

library(reshape2)
library(ggplot2)
list.files()
morphData <- read.table("Box.plot.chemical.txt", sep="\t", header=TRUE)
morphData
df.m <- melt(morphData, id.var = "SampleType")


pdf("box.plot.pdf", width = 18, height = 12)


p <- ggplot(data = df.m, aes(x="", y=value)) +

theme_bw() +
theme(
      panel.grid.major=element_blank(),
      panel.grid.minor=element_blank(),
      plot.title = element_text(vjust = -8.5,hjust = 0.1),
      line=element_line(size=1),
     
      axis.text.x = element_text(size = 10, face ="bold", hjust = 0.5, colour = "black"),
      axis.text.y = element_text(size = 12, face ="bold", hjust = 0.5, colour = "black"),
      axis.ticks.x = element_blank()) +
       

theme(strip.background=element_rect(fill="white")) +
theme(strip.text=element_text(color="black",face="bold", size =14)) + 
geom_boxplot(aes(fill=SampleType),outlier.colour = NA) 
p + facet_wrap(.~variable, scales="free") 
dev.off();

update:

I was able to remove the variable name from the bottom by using above updated code but still getting tick mark on x-axis, and also how I can make these label bold?

update:

It will be a great help if you could also guide me how I can add Tukey's posthoc significance letter in above box plot to make this plot more meaningful. I was able to make bold facet wrap text, now please help me with posthoc test letter. I have updated the code again.

update:

Can you please help to change the order of samples; like I want to change the order of SampleType in this box plot and also how we can remove outlier and will it be possible to add whisker so it will look better. I was able to remove outlier by using outlier.colour = NA, Now It just whisker, but I am not able to understand How I can add whisker to box plot? I have updated the code and figure above.

update:

sorry, I wanted to add errorbar and with this command, it is not adding error bar on all box: stat_boxplot(geom ="errorbar")

$\endgroup$
1
  • $\begingroup$ This is a good question and the answer below is very useful. Please could you consider accepting the answer? $\endgroup$
    – M__
    Commented Sep 15, 2023 at 20:18

1 Answer 1

1
$\begingroup$

what do you mean by How I can put the variable name on the top of each box plot like Rice_P at top and at the bottom as well? Do you want to keep the label at both top and bottom or remove one of them? From my opinion it's not necessary to use facet in this case, since you only have one variable on x-axis in each facet now. You can just put variable as x-axis, and color your box as it is now. Boxes with the same variable are going to be grouped together.

You can use axis.ticks.x in theme to remove the ticks on x-axis similar to axis.text. To make label bold maybe you can try fontface="bold"

Adding testing letter may be harder, you could check out ggpubr or ggprism package.

you can use as.factor() to change sample name to factors and plot them in order. Or you can use scale_x_discrete(limit = <your sample order>) to force an order. I don't see outlier in plot. Outliers are usually black dots in box plot. And the box plot you show already has whiskers, so I 'm not sure what you want to add.

I'm not sure whether it's a good idea to plot error bars in boxplot because that is not the convention. But probably you can try using geom_errorbar in addition to geom_boxplot as another layer but it requires more customization.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.