0

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

M--
  • 25,431
  • 8
  • 61
  • 93
  • 1
    Not my downvote. An important aspect of ggplot is that when you find yourself adding the same layer over and over again you are not using the package optimally. Gather columns `X1700` to `X1700A` and then map the resulting value column to `y` and the resulting `key` column to the colour aesthetic. Also specify `data` in `ggplot()`. See: [Plotting two variables as lines using ggplot2 on the same graph](https://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph) – markus May 27 '19 at 21:07

1 Answers1

0

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())  
M--
  • 25,431
  • 8
  • 61
  • 93