Home > front end >  How to make a barplot with ggplot for species richness and diversity in one frame
How to make a barplot with ggplot for species richness and diversity in one frame

Time:12-01

I'm still a newbie in R, I have some questions and help about using ggplot.

I've been using spadeR for getting species richness and diversity in each sampling location, and I want to make barplot for my data visualization. But I have some trouble getting the right code for ggplot.

This is an example of what I want my data visualization will be. enter image description here

But my barplot just look like this

enter image description here

I want to add a legend on top of the frame, I tried to add it, but it turn out really bad.

Can anyone tell me how to fix this using ggplot, and also for making 2 barplot in one frame like the examples above, how to use parfrow? Hope anyone will teach me how to fix this. Thank you so much!

Here my data set for richness species in 10 sampling locations, includes estimates score of species richness and standard errors.

datalw <- as.matrix(data.frame(Bng = c(8, 0.4),
                               Krs= c(3, 0),
                               Bny= c(3, 0),
                               Kmb= c(9.1, 7.40),
                               Sgk= c(3, 0.3),
                               Lwb= c(6.4, 1.0),
                               Lws= c(4.3, 0.7),
                               Krm= c(3, 0.5),
                               Hrt= c(7, 0.5),
                               Gmb= c(6.5, 1.0)))
rownames(datalw)- c("Estimates", "s.e")
datalw                                                  

barplot(datalw,                                         
        col = c("#1b98e0", "#353436"))
legend("top",                                    
       legend = c("estimates", "s.e"),
       fill = c("#1b98e0", "#353436"))

CodePudding user response:

I had to play around with your data a bit. You didn't have to make datalw a matrix because it ends up causing issues. You data also had multiple columns rather than multiple row so I reformatted your data for you.

datalw <- structure(list(loc = structure(c(1L, 7L, 2L, 5L, 10L, 8L, 9L, 
6L, 4L, 3L), .Label = c("Bng", "Bny", "Gmb", "Hrt", "Kmb", "Krm", 
"Krs", "Lwb", "Lws", "Sgk"), class = "factor"), V1 = c(8, 3, 
3, 9.1, 3, 6.4, 4.3, 3, 7, 6.5), V2 = c(0.4, 0, 0, 7.4, 0.3, 
1, 0.7, 0.5, 0.5, 1)), class = "data.frame", row.names = c(NA, 
-10L))

Since you want to have your bars side by side you can melt that data together to plot your data easier

library(ggplot2)
library(ggpattern)
library(reshape2)

datalw2 <- melt(datalw, id.vars='loc')

There is a way to make patterns with ggplot you can use ggpattern

ggplot(datalw2,aes(x=loc, y=value, fill=variable, group=variable, pattern = variable))  
  geom_bar_pattern(stat='identity', position='dodge')  
  scale_pattern_manual(values = c(V1 = "stripe", V2 = "crosshatch"))

This will produce a plot like this. There are more advanced ways to change the pattern like you have in your picture, however, you will have to create the pattern yourself rather than using default patterns from ggpattern

example1

Your data doesn't have enough information to create the error bars shown in your picture

You can also make the plot black and white like so

ggplot(datalw2,aes(x=loc, y=value, fill=variable, group=variable, pattern = variable))  
  geom_bar_pattern(stat='identity', position='dodge')  
  theme_bw()   
  scale_pattern_manual(values = c(V1 = "stripe", V2 = "crosshatch"))  
  scale_fill_grey(start = .9, end = 0)

example

There is a way to create a side by side plot like in your picture however you also don't have enough data to make a second plot

If you want to add the error bars to your graph. You can use geom_errorbar. Using the data you provided in your comment below

datadv <- structure(list(caves = structure(c(1L, 7L, 2L, 5L, 10L, 8L, 9L,  6L, 4L, 3L),  .Label = c("Bng", "Bny", "Gmb", "Hrt", "Kmb", "Krm", "Krs", "Lwb", "Lws", "Sgk"), class = "factor"), Index = c(1.748, 0.022, 1.066, 1.213, 0.894, 0.863, 1.411, 0.179, 1.611, 1.045),  Std = c(0.078, 0.05, 0.053, 0.062, 0.120, 0.109, 0.143, 0.072, 0.152, 0.171)),  class = "data.frame", row.names = c(NA,-10L))

library(ggpattern)
library(ggplot2)
ggplot(datadv,aes(x=caves, y=Index))  
  geom_bar_pattern(stat='identity', position='dodge')  
  theme_bw()   
  scale_pattern_manual(values = c(V1 = "stripe", V2 = "crosshatch"))  
  scale_fill_grey(start = .9, end = 0)  
  geom_errorbar(aes(ymin=Index-Std, ymax=Index Std), width=.2,
                position=position_dodge(.9))

example3

  • Related