<p>The ggplot2 function <code>stat_summary</code> or <code>stat_summary_bin</code> should get you most of the way there, something like this [not tested]:</p>
<pre><code>library(tidyverse)
library(ggplot2)

df %&gt;%
  ggplot() +
  aes(x=value, y=StudyID, colour=type) +
  stat_summary() +
  facet_wrap(~variable)
</code></pre>
<p>See <code>?stat_summary</code> for more information.</p>

CC BY-SA 4.0
2021-06-20T04:10:25.393Z
2021-06-20T04:10:25.393Z
gringer
<p>If you're not wedded to a dot and whiskers plot as the one show, I'd probably plot this dataset as a boxplot with ggplot:</p>
<pre><code>ggplot(df,aes(x=value,y=StudyID,colour=type)) +
geom_boxplot() +
facet_wrap(~variable)
</code></pre>

CC BY-SA 4.0
2021-05-20T10:19:30.693Z
2021-05-20T12:46:07.467Z
2021-05-20T12:46:07.467Z
Greg
<p>I have data from different countries from two groups of samples (control and test). And, calculated three different indices alpha, beta and gamma. I have a large dataframe containing all the data. Here is a reproducible example of my dataframe (df):</p>
<pre><code>df &lt;- structure(list(
    SampleID = c(&quot;SRR490_profile&quot;, &quot;SRR467268_profile&quot;,
                 &quot;SRR467265_profile&quot;, &quot;SRR467264_profile&quot;,
                 &quot;SRR467263_profile&quot;, &quot;SRR467258_profile&quot;,
                 &quot;SRR467250_profile&quot;, &quot;SRR467244_profile&quot;,
                 &quot;SRR467242_profile&quot;, &quot;SRR467227_profile&quot;,
                 &quot;ERR710432_profile&quot;, &quot;ERR710430_profile&quot;,
                 &quot;ERR710429_profile&quot;, &quot;SRR232646_profile&quot;,
                 &quot;SRR232622_profile&quot;, &quot;SRR232619_profile&quot;,
                 &quot;SRR232617_profile&quot;, &quot;SRR232616_profile&quot;,
                 &quot;SRR232614_profile&quot;, &quot;SRR232612_profile&quot;,
                 &quot;HJ4386_profile&quot;, &quot;HJ4382_profile&quot;,
                 &quot;HJ4380_profile&quot;, &quot;HJ4379_profile&quot;,
                 &quot;HJ4377_profile&quot;, &quot;HJ4375_profile&quot;,
                 &quot;HJ4374_profile&quot;, &quot;HJ4373_profile&quot;,
                 &quot;HJ4372_profile&quot;, &quot;HJ4370_profile&quot;,
                 &quot;KLM3246730_profile&quot;, &quot;KLM3246729_profile&quot;,
                 &quot;KLM3246725_profile&quot;, &quot;KLM3246722_profile&quot;,
                 &quot;KLM3246721_profile&quot;, &quot;KLM3246714_profile&quot;,
                 &quot;KLM3246713_profile&quot;, &quot;KLM3246712_profile&quot;,
                 &quot;KLM3246711_profile&quot;, &quot;KLM3246705_profile&quot;),
    type = c(&quot;test&quot;, &quot;control&quot;, &quot;control&quot;, &quot;control&quot;,
             &quot;control&quot;, &quot;test&quot;, &quot;test&quot;, &quot;test&quot;, &quot;control&quot;,
             &quot;control&quot;, &quot;control&quot;, &quot;test&quot;, &quot;control&quot;,
             &quot;control&quot;, &quot;control&quot;, &quot;control&quot;, &quot;control&quot;,
             &quot;test&quot;, &quot;control&quot;, &quot;test&quot;, &quot;control&quot;, &quot;control&quot;,
             &quot;control&quot;, &quot;control&quot;, &quot;control&quot;, &quot;test&quot;,
             &quot;control&quot;, &quot;test&quot;, &quot;test&quot;, &quot;control&quot;, &quot;control&quot;,
             &quot;test&quot;, &quot;test&quot;, &quot;test&quot;, &quot;control&quot;, &quot;test&quot;,
             &quot;control&quot;, &quot;test&quot;, &quot;test&quot;, &quot;test&quot;),
    variable = c(&quot;gamma&quot;, &quot;alpha&quot;, &quot;beta&quot;, &quot;gamma&quot;, &quot;alpha&quot;, &quot;beta&quot;,
                 &quot;gamma&quot;, &quot;alpha&quot;, &quot;beta&quot;, &quot;gamma&quot;, &quot;alpha&quot;, &quot;alpha&quot;,
                 &quot;beta&quot;, &quot;gamma&quot;, &quot;alpha&quot;, &quot;beta&quot;, &quot;gamma&quot;, &quot;alpha&quot;,
                 &quot;beta&quot;, &quot;gamma&quot;, &quot;alpha&quot;, &quot;alpha&quot;, &quot;alpha&quot;, &quot;beta&quot;,
                 &quot;beta&quot;, &quot;gamma&quot;, &quot;alpha&quot;, &quot;beta&quot;, &quot;gamma&quot;, &quot;alpha&quot;,
                 &quot;beta&quot;, &quot;gamma&quot;, &quot;alpha&quot;, &quot;beta&quot;, &quot;gamma&quot;, &quot;alpha&quot;,
                 &quot;beta&quot;, &quot;gamma&quot;, &quot;alpha&quot;, &quot;beta&quot;),
    value = c(95, 90, 109, 112, 128, 109, 90, 87, 90, 99, 129, 101, 113,
              100, 111, 102, 99, 105, 85, 94, 133, 117, 116, 83, 113, 126,
              90, 128, 107, 121, 88, 109, 82, 42, 67, 100, 88, 101, 127,
              115),
    StudyID = c(&quot;swiss&quot;, &quot;swiss&quot;, &quot;swiss&quot;, &quot;swiss&quot;, &quot;swiss&quot;, &quot;swiss&quot;,
                &quot;swiss&quot;, &quot;swiss&quot;, &quot;swiss&quot;, &quot;swiss&quot;, &quot;australian&quot;,
                &quot;australian&quot;, &quot;australian&quot;, &quot;australian&quot;, &quot;australian&quot;,
                &quot;australian&quot;, &quot;australian&quot;, &quot;australian&quot;, &quot;australian&quot;,
                &quot;australian&quot;, &quot;thai&quot;, &quot;thai&quot;, &quot;thai&quot;, &quot;thai&quot;, &quot;thai&quot;,
                &quot;thai&quot;, &quot;thai&quot;, &quot;thai&quot;, &quot;thai&quot;, &quot;thai&quot;, &quot;english&quot;,
                &quot;english&quot;, &quot;english&quot;, &quot;english&quot;, &quot;english&quot;, &quot;english&quot;,
                &quot;english&quot;, &quot;english&quot;, &quot;english&quot;, &quot;english&quot;)),
    row.names = c(NA, -40L),
    class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
</code></pre>
<p>Now, I want an image for each index containing barplots for two groups (control and test) and Y-axis showing the StudyID names (i.e., country names) like the following (this image shows plots for two indices alpha and beta):
<a href="https://i.sstatic.net/AeKTY.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/AeKTY.jpg" alt="enter image description here" /></a>
Is this possible from my data? Can anyone give me a solution?</p>
<p>Thanks in advance</p>

<r>
How can I create a combined barplot like this image?
# How can I create a combined barplot like this image?

(Asked by: username_0 on 2021-05-19)

I have data from different countries from two groups of samples (control and test). And, calculated three different indices alpha, beta and gamma. I have a large dataframe containing all the data. Here is a reproducible example of my dataframe (df):
df <- structure(list(
    SampleID = c("SRR490_profile", "SRR467268_profile",
                 "SRR467265_profile", "SRR467264_profile",
                 "SRR467263_profile", "SRR467258_profile",
                 "SRR467250_profile", "SRR467244_profile",
                 "SRR467242_profile", "SRR467227_profile",
                 "ERR710432_profile", "ERR710430_profile",
                 "ERR710429_profile", "SRR232646_profile",
                 "SRR232622_profile", "SRR232619_profile",
                 "SRR232617_profile", "SRR232616_profile",
                 "SRR232614_profile", "SRR232612_profile",
                 "HJ4386_profile", "HJ4382_profile",
                 "HJ4380_profile", "HJ4379_profile",
                 "HJ4377_profile", "HJ4375_profile",
                 "HJ4374_profile", "HJ4373_profile",
                 "HJ4372_profile", "HJ4370_profile",
                 "KLM3246730_profile", "KLM3246729_profile",
                 "KLM3246725_profile", "KLM3246722_profile",
                 "KLM3246721_profile", "KLM3246714_profile",
                 "KLM3246713_profile", "KLM3246712_profile",
                 "KLM3246711_profile", "KLM3246705_profile"),
    type = c("test", "control", "control", "control",
             "control", "test", "test", "test", "control",
             "control", "control", "test", "control",
             "control", "control", "control", "control",
             "test", "control", "test", "control", "control",
             "control", "control", "control", "test",
             "control", "test", "test", "control", "control",
             "test", "test", "test", "control", "test",
             "control", "test", "test", "test"),
    variable = c("gamma", "alpha", "beta", "gamma", "alpha", "beta",
                 "gamma", "alpha", "beta", "gamma", "alpha", "alpha",
                 "beta", "gamma", "alpha", "beta", "gamma", "alpha",
                 "beta", "gamma", "alpha", "alpha", "alpha", "beta",
                 "beta", "gamma", "alpha", "beta", "gamma", "alpha",
                 "beta", "gamma", "alpha", "beta", "gamma", "alpha",
                 "beta", "gamma", "alpha", "beta"),
    value = c(95, 90, 109, 112, 128, 109, 90, 87, 90, 99, 129, 101, 113,
              100, 111, 102, 99, 105, 85, 94, 133, 117, 116, 83, 113, 126,
              90, 128, 107, 121, 88, 109, 82, 42, 67, 100, 88, 101, 127,
              115),
    StudyID = c("swiss", "swiss", "swiss", "swiss", "swiss", "swiss",
                "swiss", "swiss", "swiss", "swiss", "australian",
                "australian", "australian", "australian", "australian",
                "australian", "australian", "australian", "australian",
                "australian", "thai", "thai", "thai", "thai", "thai",
                "thai", "thai", "thai", "thai", "thai", "english",
                "english", "english", "english", "english", "english",
                "english", "english", "english", "english")),
    row.names = c(NA, -40L),
    class = c("tbl_df", "tbl", "data.frame"))

Now, I want an image for each index containing barplots for two groups (control and test) and Y-axis showing the StudyID names (i.e., country names) like the following (this image shows plots for two indices alpha and beta):

Is this possible from my data? Can anyone give me a solution?
Thanks in advance

**Tags:** r

---

## 2 answers

### Answer by username_1 on 2021-06-20. Score: 2

The ggplot2 function stat_summary or stat_summary_bin should get you most of the way there, something like this [not tested]:
library(tidyverse)
library(ggplot2)

df %>%
  ggplot() +
  aes(x=value, y=StudyID, colour=type) +
  stat_summary() +
  facet_wrap(~variable)

See ?stat_summary for more information.


---

### Answer by username_2 on 2021-05-20. Score: 2

If you're not wedded to a dot and whiskers plot as the one show, I'd probably plot this dataset as a boxplot with ggplot:
ggplot(df,aes(x=value,y=StudyID,colour=type)) +
geom_boxplot() +
facet_wrap(~variable)


---

