Home > front end >  I want to plot multiple histogram per site
I want to plot multiple histogram per site

Time:11-05

I want 4 histograms on the x axis; one for each Site with bins for each group of individuals. The groups are ; "mol","an","crust" and "other". the y axis would be the mean abundance. the data frame can be called DF.

mol an crust other Site
1 1.00000 0.0000000 12.000000 2 1
2 0.00000 0.6666667 28.000000 0 2
3 0.00000 0.3333333 2.000000 0 3
4 4.00000 1.3333333 3.000000 0 4
5 12.66667 3.3333333 1.666667 0 5

ggplot(DF, aes(fill=?, y=?, x=Site)) geom_bar(position="dodge", stat="identity")

I am lost in want to put in sections fill= and y=, I have been doing it seperetly with vectors and it worked but would like to group all. DF$Site differs from another "Site" I have been using for my simpler histograms: Site <- c(rep("1" , 1) , rep("2" , 1) , rep("3", 1) , rep("4" , 1), rep("5" , 1) )

And I would "fill" with: fill = rep("whatever",5)

for my means I would creat a vector and plug into y= like so: mol = c(1,0,0,4,12.66666667) I would also use the mean vector with a standard deviation vector for the error bars:

sdmol = c(0.471404521,0,0,5.656854249,16.51934892)

geom_errorbar( aes(x=Site, ymin=mol-sdmol, ymax= mol sdmol), width=0.4, colour="orange", alpha=0.9, size=1) theme(text=element_text(size=20))

I can regroup all my data if it helps. Thank you!

CodePudding user response:

Something like this?

library(tidyverse)
DF %>% 
  pivot_longer(
    cols = c(mol, an, crust)
  ) %>% 
  ggplot(aes(x=factor(Site), y=value, fill=name)) 
  geom_col(position = position_dodge()) 

enter image description here

OR

library(tidyverse)
DF %>% 
  pivot_longer(
    cols = c(mol, an, crust)
  ) %>% 
  ggplot(aes(x=Site, y=value, fill=name)) 
  geom_col(position = position_dodge())  
  facet_wrap(.~Site, scales = "free")

enter image description here

  • Related