TransWikia.com

How to plot entropart Diversity profiles in ggplot2

Stack Overflow Asked by Alejandra Dip on January 5, 2022

I got the diversity profiles of 6 altitudinal bands with DivProfile function of entropart package. Now I want to plot one of the components of the result object, $CommunityAlphaDiversities, using ggplot2

I tried this:

band.div1 <- as.data.frame(banddivs)
ggplot()+
  geom_line(data=band.div1, aes(x = Order, y = X1700))+
  geom_line(data=band.div1, aes(x = Order, y = X1900))+
  geom_line(data=band.div1, aes(x = Order, y = X2100))+
  geom_line(data=band.div1, aes(x = Order, y = X2300))+
  geom_line(data=band.div1, aes(x = Order, y = X2500))+
  geom_line(data=band.div1, aes(x = Order, y = X1700A))+
  theme(axis.title.x = element_text(size=18), # remove x-axis labels
        axis.title.y = element_text(size=18), # remove y-axis labels
        panel.grid.major = element_blank(),  #remove major-grid labels
        panel.grid.minor = element_blank())  #remove minor-grid labels)

But I don’t know how to set different colours for each line. My ggplot2 handling is very limited

One Answer

You need to reshape your data to a long format to have a variable for grouping (to avoid multiple geom_lines) and also use that variable for colour.

band.div1.long <- reshape(band.div1, direction='long', 
        varying=c('X1700', 'X1700A', 'X1900', 'X2100', 'X2300', 'X2500'), 
        timevar='var',
        times=c('X1700', 'X1700A', 'X1900', 'X2100', 'X2300', 'X2500'),
        v.names=c('X'),
        idvar='Order')

library(ggplot2)    
ggplot()+
  geom_line(data=band.div1.long, aes(x = Order, y = X, group=var, colour=var))+
  theme(axis.title.x = element_text(size=18),
        axis.title.y = element_text(size=18),
        panel.grid.major = element_blank(),  
        panel.grid.minor = element_blank())  

You can specify the colour manually in each geom but it is not efficient. Look below:

ggplot()+
  geom_line(data=x, aes(x = Order, y = X1700 , colour="X1700" ))+
  geom_line(data=x, aes(x = Order, y = X1900 , colour="X1900" ))+
  geom_line(data=x, aes(x = Order, y = X2100 , colour="X2100" ))+
  geom_line(data=x, aes(x = Order, y = X2300 , colour="X2300" ))+
  geom_line(data=x, aes(x = Order, y = X2500 , colour="X2500" ))+
  geom_line(data=x, aes(x = Order, y = X1700A, colour="X1700A"))+
  theme(axis.title.x = element_text(size=18),
        axis.title.y = element_text(size=18),
        panel.grid.major = element_blank(),  
        panel.grid.minor = element_blank())  

Answered by M-- on January 5, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP