Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-05T13:21:24.303Z
Answers.LastActivityDate: 2023-01-10T14:08:40.707Z
Answers.LastEditDate: 2023-01-10T14:08:40.707Z
Answers.UserDisplayName: acvill
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-05-04T21:19:17.947Z
Comments.Text: 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.
Comments.UserDisplayName: acvill
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-05-05T10:57:29.967Z
Comments.Text: 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
Comments.UserDisplayName: Mark
Tags: <r>
Title: How to build advanced boxplots from datasets which columns contain only numbers?
ThreadText: # 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:
  
  [![enter image description here](https://i.sstatic.net/DbhJG.jpg)](https://i.sstatic.net/DbhJG.jpg)
  
  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()
  
  ```
  
  [![enter image description here](https://i.sstatic.net/knu0J.png)](https://i.sstatic.net/knu0J.png)
  
  
  ---
  
  