<p>I have noticed a few errors:</p>

<p>1) You define a bunch of variables (Species, Class, ...) but then instead of creating your data frame data with these you create it by reading from text. I don't think this is good practice, you should do so like in your example code.</p>

<p>2) With the way you define, your data frame does not contain columns like Class or Species but columns V1 to V5, you should use argument <code>header = TRUE</code> in your <code>read.table()</code> call.</p>

<p>3) Even when you read your data as in step 2, you would need to transform your data table into "long format", please see below.</p>

<p>4) In your <code>ggplot()</code> call, you assign 7 to your y aesthetic, this does not make sense. You should either provide a vector of length equal to your other aesthetics (x and fill) or better a column name of values of interest.</p>

<pre><code># This is your code minus the redundant variables that you have created but are
# not used

library(tidyr)
library(ggplot2)

# Pay attention to the header argument
data &lt;- read.table(
  text = "Class Cames1 Came2 Cams3 cams4
     EVV 2194 5435 2706 2902
     MaLR 503 108 184 231
     psy 284 187 413 295
     Nargo 274 1977 413 2195
     Pao 66 29 78 97
     Unknown 286 283 273 377
     Undefined 76 77 80 95",
  header = TRUE
)

# data in wide format
&gt; data
      Class Cames1 Came2 Cams3 cams4
1       EVV   2194  5435  2706  2902
2      MaLR    503   108   184   231
3       psy    284   187   413   295
4     Nargo    274  1977   413  2195
5       Pao     66    29    78    97
6   Unknown    286   283   273   377
7 Undefined     76    77    80    95

# Making the data wider: Keeping Class column as it is but condensing the data
# within the rest of the columns into two newly created columns, Species_ and
# Value_

data_long &lt;- gather(data,
                    key = "Species_",
                    value = "Value_",
                    -Class)

# First few rows of your data in long format
&gt; head(data_long)
    Class Species_ Value_
1     EVV   Cames1   2194
2    MaLR   Cames1    503
3     psy   Cames1    284
4   Nargo   Cames1    274
5     Pao   Cames1     66
6 Unknown   Cames1    286

# x and y aesthetics are the newly created columns Species_ and Value_
ggplot(data_long, aes(fill=Class, y=Value_, x=Species_)) + geom_bar(position="dodge", stat="identity")
</code></pre>

<p><a href="https://i.sstatic.net/JXyOh.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/JXyOh.png" alt="enter image description here"></a></p>

CC BY-SA 4.0
2019-08-25T12:34:03.840Z
oh thank you a lot i really appreciate you help, one more thing am i able to edit the colours or perhaps make it looks better than this , to report it in a journal
M.Bioinfo
CC BY-SA 4.0
2019-08-25T13:22:45.657Z
ggplot2 offers many options for customized plots but the easiest would be to use a built-in ggplot2 theme (just do "+ theme_void() after the geom_* line for example). Here is the link for all ggplot2 themes: https://ggplot2.tidyverse.org/reference/ggtheme.html . Moreover the ggthemes package offers many more themes.
haci
CC BY-SA 4.0
2019-08-25T13:45:02.940Z
ggplot(data_long, aes(fill=Class, y=Value_, x=Species_)) + geom_bar(HERE i ADD MY THEM????")
M.Bioinfo
CC BY-SA 4.0
2019-08-25T14:21:20.397Z
Please go through the link I have provided in my previous comment, if you do so you will see that a basic ggplot2 plot consists of a call to ggplot() function and this is followed by a call to your geom function (for example geom_bar() ) and then you can add other other ggplot2 functions (such as theme_void(), etc). All of these functions are piped with +.
haci
CC BY-SA 4.0
2019-09-11T10:44:46.420Z
dear sorry to disturb again but im wondering if im able to change the font style to italic for the species name ( Came2 , Cames3 ...etc)
M.Bioinfo
CC BY-SA 4.0
2019-08-25T11:23:15.043Z
2019-08-25T11:29:55.740Z
2019-08-25T11:29:55.740Z
haci
<p>I’m trying to create a Group Chart like this:
<a href="https://i.sstatic.net/oVbX3.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/oVbX3.png" alt="aim somthing similar to this chart"></a></p>

<p>I have 7 classes for each of 4 species, not 5 like in the diagram. I want to build i diagram to show the distributions of the classes in each species</p>

<p>Here’s example provided online:</p>

<pre><code>  library(ggplot2)

    # create a dataset
    specie &lt;- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
    condition &lt;- rep(c("normal" , "stress" , "Nitrogen") , 4)
    value &lt;- abs(rnorm(12 , 0 , 15))
    data &lt;- data.frame(specie,condition,value) 
    # Grouped
    ggplot(data, aes(fill=condition, y=value, x=specie)) + 
        geom_bar(position="dodge", stat="identity")



------------------------------------------
    #Here’s code:


    &gt; Species &lt;- c(rep("Cames1" , 5) , rep("Came2" , 5) , rep("Cams3" , 5) , rep("cams4" , 5) )
    &gt; Class &lt;- rep(c("EVV" , "MaLR" , "psy" , "Nargo" , "Pao" , "Unknown" , "Undefiend") , 8)
    &gt; value &lt;- abs(rnorm(20 , 0 , 15 ))
    data &lt;- read.table(text="Class Cames1 Came2 Cams3 cams4
    + EVV 2194 5435 2706 2902 
    + MaLR 503 108 184 231
    + psy 284 187 413 295
    + Nargo 274 1977 413 2195
    + Pao 66 29 78 97
    + Unknown 286 283 273 377
    + Undefined 76 77 80 95")
   &gt; ggplot(data, aes(fill=Class, y=7, x=Species)) + geom_bar(position="dodge", stat="identity")

Error: Aesthetics must be either length 1 or the same as the data (7): x, fill
</code></pre>

<p>i am able to build basics chart, but to group them all into one like the example provided challenging for me , could someone help out ? </p>

CC BY-SA 4.0
2019-08-25T23:05:30.997Z
I'm voting to close this question as off-topic because it is not about bioinformatics.  It is general plotting
Bioathlete
<r><ggplot2>
Create a Grouped Barchart
# Create a Grouped Barchart

(Asked by: username_0 on 2019-08-25)

I’m trying to create a Group Chart like this:

I have 7 classes for each of 4 species, not 5 like in the diagram. I want to build i diagram to show the distributions of the classes in each species
Here’s example provided online:
  library(ggplot2)

    # create a dataset
    specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
    condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
    value <- abs(rnorm(12 , 0 , 15))
    data <- data.frame(specie,condition,value) 
    # Grouped
    ggplot(data, aes(fill=condition, y=value, x=specie)) + 
        geom_bar(position="dodge", stat="identity")



------------------------------------------
    #Here’s code:


    > Species <- c(rep("Cames1" , 5) , rep("Came2" , 5) , rep("Cams3" , 5) , rep("cams4" , 5) )
    > Class <- rep(c("EVV" , "MaLR" , "psy" , "Nargo" , "Pao" , "Unknown" , "Undefiend") , 8)
    > value <- abs(rnorm(20 , 0 , 15 ))
    data <- read.table(text="Class Cames1 Came2 Cams3 cams4
    + EVV 2194 5435 2706 2902 
    + MaLR 503 108 184 231
    + psy 284 187 413 295
    + Nargo 274 1977 413 2195
    + Pao 66 29 78 97
    + Unknown 286 283 273 377
    + Undefined 76 77 80 95")
   > ggplot(data, aes(fill=Class, y=7, x=Species)) + geom_bar(position="dodge", stat="identity")

Error: Aesthetics must be either length 1 or the same as the data (7): x, fill

i am able to build basics chart, but to group them all into one like the example provided challenging for me , could someone help out ?

**Tags:** r, ggplot2
> **Comments:**
> **username_1**: I'm voting to close this question as off-topic because it is not about bioinformatics.  It is general plotting  

---

## 1 answers

### Answer by username_2 on 2019-08-25. Score: 4 | Accepted answer

I have noticed a few errors:
1) You define a bunch of variables (Species, Class, ...) but then instead of creating your data frame data with these you create it by reading from text. I don't think this is good practice, you should do so like in your example code.
2) With the way you define, your data frame does not contain columns like Class or Species but columns V1 to V5, you should use argument header = TRUE in your read.table() call.
3) Even when you read your data as in step 2, you would need to transform your data table into "long format", please see below.
4) In your ggplot() call, you assign 7 to your y aesthetic, this does not make sense. You should either provide a vector of length equal to your other aesthetics (x and fill) or better a column name of values of interest.
# This is your code minus the redundant variables that you have created but are
# not used

library(tidyr)
library(ggplot2)

# Pay attention to the header argument
data <- read.table(
  text = "Class Cames1 Came2 Cams3 cams4
     EVV 2194 5435 2706 2902
     MaLR 503 108 184 231
     psy 284 187 413 295
     Nargo 274 1977 413 2195
     Pao 66 29 78 97
     Unknown 286 283 273 377
     Undefined 76 77 80 95",
  header = TRUE
)

# data in wide format
> data
      Class Cames1 Came2 Cams3 cams4
1       EVV   2194  5435  2706  2902
2      MaLR    503   108   184   231
3       psy    284   187   413   295
4     Nargo    274  1977   413  2195
5       Pao     66    29    78    97
6   Unknown    286   283   273   377
7 Undefined     76    77    80    95

# Making the data wider: Keeping Class column as it is but condensing the data
# within the rest of the columns into two newly created columns, Species_ and
# Value_

data_long <- gather(data,
                    key = "Species_",
                    value = "Value_",
                    -Class)

# First few rows of your data in long format
> head(data_long)
    Class Species_ Value_
1     EVV   Cames1   2194
2    MaLR   Cames1    503
3     psy   Cames1    284
4   Nargo   Cames1    274
5     Pao   Cames1     66
6 Unknown   Cames1    286

# x and y aesthetics are the newly created columns Species_ and Value_
ggplot(data_long, aes(fill=Class, y=Value_, x=Species_)) + geom_bar(position="dodge", stat="identity")

> **Comments:**
> **username_0**: oh thank you a lot i really appreciate you help, one more thing am i able to edit the colours or perhaps make it looks better than this , to report it in a journal  
> **username_2**: ggplot2 offers many options for customized plots but the easiest would be to use a built-in ggplot2 theme (just do "+ theme_void() after the geom_* line for example). Here is the link for all ggplot2 themes: https://ggplot2.tidyverse.org/reference/ggtheme.html . Moreover the ggthemes package offers many more themes.  
> **username_0**: ggplot(data_long, aes(fill=Class, y=Value_, x=Species_)) + geom_bar(HERE i ADD MY THEM????")  
> **username_2**: Please go through the link I have provided in my previous comment, if you do so you will see that a basic ggplot2 plot consists of a call to ggplot() function and this is followed by a call to your geom function (for example geom_bar() ) and then you can add other other ggplot2 functions (such as theme_void(), etc). All of these functions are piped with +.  
> **username_0**: dear sorry to disturb again but im wondering if im able to change the font style to italic for the species name ( Came2 , Cames3 ...etc)  

---

