<p>what do you mean by <code>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</code>? 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 <code>variable</code> as x-axis, and color your box as it is now. Boxes with the same <code>variable</code> are going to be grouped together.</p>
<p>You can use <code>axis.ticks.x</code> in <code>theme</code> to remove the ticks on x-axis similar to <code>axis.text</code>. To make label bold maybe you can try <code>fontface=&quot;bold&quot;</code></p>
<p>Adding testing letter may be harder, you could check out ggpubr or ggprism package.</p>
<p>you can use <code>as.factor()</code> to change sample name to factors and plot them in order. Or you can use <code>scale_x_discrete(limit = &lt;your sample order&gt;)</code> 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.</p>
<p>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 <code>geom_errorbar</code> in addition to <code>geom_boxplot</code> as another layer but it requires more customization.</p>

CC BY-SA 4.0
2021-02-12T03:20:14.970Z
2021-06-07T02:28:49.567Z
2021-06-07T02:28:49.567Z
Phoenix Mu
<p>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?</p>
<pre><code>library(reshape2)
library(ggplot2)
list.files()
morphData &lt;- read.table(&quot;Box.plot.chemical.txt&quot;, sep=&quot;\t&quot;, header=TRUE)
morphData
df.m &lt;- melt(morphData, id.var = &quot;SampleType&quot;)


pdf(&quot;box.plot.pdf&quot;, width = 18, height = 12)


p &lt;- ggplot(data = df.m, aes(x=&quot;&quot;, 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 =&quot;bold&quot;, hjust = 0.5, colour = &quot;black&quot;),
      axis.text.y = element_text(size = 12, face =&quot;bold&quot;, hjust = 0.5, colour = &quot;black&quot;),
      axis.ticks.x = element_blank()) +
       

theme(strip.background=element_rect(fill=&quot;white&quot;)) +
theme(strip.text=element_text(color=&quot;black&quot;,face=&quot;bold&quot;, size =14)) + 
geom_boxplot(aes(fill=SampleType),outlier.colour = NA) 
p + facet_wrap(.~variable, scales=&quot;free&quot;) 
dev.off();
</code></pre>
<p>update:</p>
<p>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?</p>
<p>update:</p>
<p>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.</p>
<p>update:</p>
<p>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.</p>
<p>update:</p>
<p>sorry, I wanted to add errorbar and with this command, it is not adding error bar on all box: <code>stat_boxplot(geom =&quot;errorbar&quot;)</code></p>

CC BY-SA 4.0
2023-09-15T20:18:32.367Z
This is a good question and the answer below is very useful. Please could you consider accepting the answer?
M__
<r><ggplot2>
Multiple box plot
# Multiple box plot

(Asked by: username_0 on 2021-02-11)

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

**Tags:** r, ggplot2
> **Comments:**
> **username_1**: This is a good question and the answer below is very useful. Please could you consider accepting the answer?  

---

## 1 answers

### Answer by username_2 on 2021-02-12. Score: 1

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 = )` 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.


---

