FranciscoC FranciscoC - 1 month ago 18
R Question

building a area plot in R

I´m trying to build a graph that plots relative abundance against depth variation.

I have the following table

test X1m X2m X3m X4m X5m X6m X7m
1 Example1 1 10 10 1 1 5 1
2 Example2 2 5 5 5 2 2 5


and I have tried the following using ggplot2()

Example.class.melt<-melt(Example.df)
colnames(Example.class.melt)[1] = "Class"
colnames(Example.class.melt)[2] = "Depth"
colnames(Example.class.melt)[3] = "Relative_abundance"

Example.class.melt<-as.data.frame(Example.class.melt)

ggplot(Example.class.melt, aes(x=Depth, y=Relative_abundance, fill=as.factor(Class))) + geom_area()


For some reason, that I don´t understand, it isn´t working. Any suggestion to correct this or any alternative?

thanks

Answer

enter image description hereIs this what you are looking for?This was my interpretation based on the way you asked the question. The code is as follows:

install.packages("ggplot2")
install.packages("reshape")
library(ggplot2)
library(reshape)

Example1<-c(1,10,10,1,1,5,1)
Example2<-c(2,5,5,5,2,2,5)
data<-rbind(Example1,Example2)

Example.class.melt<-melt(data)
colnames(Example.class.melt)[1] = "Class"
colnames(Example.class.melt)[2] = "Depth"
colnames(Example.class.melt)[3] = "Relative_abundance"

Example.class.melt<-as.data.frame(Example.class.melt)

ggplot(data = Example.class.melt, aes(x = Depth, y = Relative_abundance, fill=Class)) + geom_area()

You don't require to say as.factor in fill to class.