2
$\begingroup$

I’m trying to create a Group Chart like this: aim somthing similar to this chart

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 ?

$\endgroup$
  • $\begingroup$ I'm voting to close this question as off-topic because it is not about bioinformatics. It is general plotting $\endgroup$ – Bioathlete Aug 25 at 23:05
4
$\begingroup$

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

enter image description here

$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – emaz Ad Aug 25 at 12:34
  • $\begingroup$ 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: ggplot2.tidyverse.org/reference/ggtheme.html . Moreover the ggthemes package offers many more themes. $\endgroup$ – haci Aug 25 at 13:22
  • $\begingroup$ ggplot(data_long, aes(fill=Class, y=Value_, x=Species_)) + geom_bar(HERE i ADD MY THEM????") $\endgroup$ – emaz Ad Aug 25 at 13:45
  • $\begingroup$ 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 +. $\endgroup$ – haci Aug 25 at 14:21
  • $\begingroup$ 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) $\endgroup$ – emaz Ad Sep 11 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.