如何使用ggplot2绘制entropart分集配置文件

时间:2019-05-27 21:01:39

标签: r ggplot2

我通过entropart包的DivProfile功能获得了6个垂直带的多样性图。现在,我想使用ggplot2绘制结果对象的组件之一,$ CommunityAlphaDiversities

我尝试过:

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)

但是我不知道如何为每行设置不同的颜色。我的ggplot2处理非常有限

1 个答案:

答案 0 :(得分:0)

您需要将数据重整为长格式,以具有用于分组的变量(避免使用多个geom_lines),并将变量用于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())  

您可以在每个几何区域中手动指定colour,但效率不高。看下面:

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())