Create a Grouped Barchart 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 &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


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