<p><strike>To coerce your data into a form interpretable by <code>ggplot2</code>, you can use the <code>gather</code> function from <code>tidyr</code>.</strike></p>
<p><code>gather</code> has been superseded by <code>pivot_longer</code>.</p>
<pre><code>library(tidyverse)
df &lt;- tibble(A = c(-1, 25, 3, 4, 50),
             B = c(23, -41, 32, 68, 26),
             C = c(63, 71, 22, -8, 2),
             D = c(16, -49, 2, 56, 6))

tidyr::pivot_longer(data = df, cols = c(A,B,C,D)) |&gt;
  ggplot(mapping = aes(x = name, y = value)) +
  geom_boxplot() +
  geom_dotplot(binaxis = &quot;y&quot;, stackdir = &quot;center&quot;) +
  theme_classic()
</code></pre>
<p><a href="https://i.sstatic.net/knu0J.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/knu0J.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2022-05-05T13:21:24.303Z
2023-01-10T14:08:40.707Z
2023-01-10T14:08:40.707Z
acvill
<p>I have got a fold-change dataset and I simply would like to plot something like this:</p>
<p><a href="https://i.sstatic.net/DbhJG.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/DbhJG.jpg" alt="enter image description here" /></a></p>
<p>The problem is that I have only numbers in my columns and when I use a boxplot() function I can plot only boxes but not dots, jitter, etc.</p>
<p>I use this code to build a df and then proceed to a boxplot</p>
<pre><code>A &lt;- c(-1, 25, 3, 4, 50)
B &lt;- c(23, -41, 32, 68, 26)
C &lt;- c(63, 71, 22, -8, 2)
D &lt;- c(16, -49, 2, 56, 6)
df &lt;- data.frame(A, B, C, D)
print (df)
boxplot(df) 
</code></pre>
<p>How can I plot something that looks like the above picture? Thanks</p>

CC BY-SA 4.0
2022-05-04T21:19:17.947Z
You want `ggplot2`, specifically the `geom_boxplot` and `geom_dotplot` functions. Check out the [cheat sheet](https://raw.githubusercontent.com/rstudio/cheatsheets/main/data-visualization.pdf) (warning: PDF download) to see the full functionality of the package.
acvill
CC BY-SA 4.0
2022-05-05T10:57:29.967Z
Thank you. The problem is that I don't have ```geom``` data - if you check the dataset I have proposed - there is no ```"y"``` data, I have only 4 columns (A,B,C,D) and rows with numbers. It is ok for default boxplot() while I cannot add geom, can you specify what should the ggplot2 script look like for the above-mentioned dataset? Thanks
Mark
<r>
How to build advanced boxplots from datasets which columns contain only numbers?
# How to build advanced boxplots from datasets which columns contain only numbers?

(Asked by: username_0 on 2022-05-04)

I have got a fold-change dataset and I simply would like to plot something like this:

The problem is that I have only numbers in my columns and when I use a boxplot() function I can plot only boxes but not dots, jitter, etc.
I use this code to build a df and then proceed to a boxplot
A <- c(-1, 25, 3, 4, 50)
B <- c(23, -41, 32, 68, 26)
C <- c(63, 71, 22, -8, 2)
D <- c(16, -49, 2, 56, 6)
df <- data.frame(A, B, C, D)
print (df)
boxplot(df) 

How can I plot something that looks like the above picture? Thanks

**Tags:** r
> **Comments:**
> **username_1**: You want `ggplot2`, specifically the `geom_boxplot` and `geom_dotplot` functions. Check out the [cheat sheet](https://raw.githubusercontent.com/rstudio/cheatsheets/main/data-visualization.pdf) (warning: PDF download) to see the full functionality of the package.  
> **username_0**: Thank you. The problem is that I don't have ```geom``` data - if you check the dataset I have proposed - there is no ```"y"``` data, I have only 4 columns (A,B,C,D) and rows with numbers. It is ok for default boxplot() while I cannot add geom, can you specify what should the ggplot2 script look like for the above-mentioned dataset? Thanks  

---

## 1 answers

### Answer by username_1 on 2022-05-05. Score: 2 | Accepted answer

To coerce your data into a form interpretable by ggplot2, you can use the gather function from tidyr.
gather has been superseded by pivot_longer.
library(tidyverse)
df <- tibble(A = c(-1, 25, 3, 4, 50),
             B = c(23, -41, 32, 68, 26),
             C = c(63, 71, 22, -8, 2),
             D = c(16, -49, 2, 56, 6))

tidyr::pivot_longer(data = df, cols = c(A,B,C,D)) |>
  ggplot(mapping = aes(x = name, y = value)) +
  geom_boxplot() +
  geom_dotplot(binaxis = "y", stackdir = "center") +
  theme_classic()


---

