首页 > 解决方案 > ggplot2:几何区域没有出现

问题描述

几个月前,我使用了一个脚本,使用 ggplot2 构建了一个堆积面积图。我现在正试图用类似的数据重新做,但我面临着没有显示区域的问题。在此之前,我检查了数据集是否可以制作条形图。

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

在此处输入图像描述

如您所见,区域没有出现。任何人都会有建议吗?谢谢!

标签: rggplot2

解决方案


根据 heck1 的评论,我将字符名称替换为数字。但是,我必须更改融化功能才能使其正常工作。

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

在此处输入图像描述


推荐阅读