ggplot2: geom areas not appearing

I used a script months ago to build a stacked area chart using ggplot2. I am now trying to re-do it with similar data, but i face the issue where areas are not showing. Prior to that i checked that the dataset could make bar plots.

library(ggplot2)
library(reshape2)
SampleID=c("SiteA","SiteB","SiteC","SiteD")
Species1=c(0.1,0.2,0.3,0.6)
Species2=c(0.15,0.25,0.35,0.4)
Species3=c(0.05,0,0.4,0.3)
Species4=c(0,0.05,0.05,0.9)
data=data.frame(SampleID,Species1,Species2,Species3,Species4)
mdata=melt(data)
ggplot(mdata, aes(x=SampleID, y=value,fill=variable,order=SampleID))+
geom_area(stat = 'identity',colour='black')

enter image description here

As you see, areas are not appearing. Any would have advices? Thanks!

Upvotes: 0

Views: 326

Answers (2)

Following comment of heck1, I replaced character names to numeric. I had to change the melt function to make it work however.

library(ggplot2)
library(reshape2)
SampleID=c(1,2,3,4)
Species1=c(0.1,0.2,0.3,0.6)
Species2=c(0.15,0.25,0.35,0.4)
Species3=c(0.05,0,0.4,0.3)
Species4=c(0,0.05,0.05,0.9)
data=data.frame(SampleID,Species1,Species2,Species3,Species4)
mdata=melt(data,id.vars = "SampleID", measure.vars = c("Species1","Species2","Species3","Species4"))
mdata=as.data.frame(mdata)
ggplot(mdata, aes(x=SampleID, y=value,fill=variable,order=SampleID))+
geom_area(stat = 'identity',colour='black')

enter image description here

Upvotes: 1

thothal
thothal

Reputation: 20409

Why not using geom_bar instead of geom_area

ggplot(mdata, aes(x=SampleID, y=value,fill=variable,order=SampleID))+
  geom_bar(stat = 'identity',colour='black')

produces

GGplot Barchart

Upvotes: 0

Related Questions