AnswerBun.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!

Related Questions

Can i show all variable results in a div in jQuery?

0  Asked on December 5, 2021 by nippledisaster

   

Comparing two generics in compareTo()

2  Asked on December 5, 2021 by chalupabatmac

       

arranging the json file base on key value

1  Asked on December 5, 2021 by prateek-singh

     

Vue watch method not filtering object

3  Asked on December 5, 2021

   

Passing a variable between HTML pages using JavaScript

2  Asked on December 5, 2021 by robert-little

   

How to avoid code redundancy with JAVA generics

1  Asked on December 5, 2021 by cosimoth

   

Why is my project no longer updating when I write new code?

1  Asked on December 5, 2021 by stephen-horton

       

node https ssl your connection is not private error

2  Asked on December 5, 2021 by andrea-dattero

       

Nested for loop using dict

9  Asked on December 5, 2021 by purwu

     

React Hooks – Input loses focus when 1 character is typed in

4  Asked on December 5, 2021 by dragos-strugar

   

How do I build a C Python Extension for Windows using Cygwin64?

2  Asked on December 5, 2021 by zenith2198

       

Wificonfiguration is deprecated Android 10

1  Asked on December 5, 2021 by ahmad-idrees

         

Wrong tootip in chartjs

1  Asked on December 5, 2021 by thiago-scher

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir