Correct for missing values in a Stacked area plot using ggplot2

僤鯓⒐⒋嵵緔 提交于 2019-12-08 04:39:02

问题


I've been trying to recreate this post on a combination of stacked bar/area plot. I have some problems with missing values though.

Here's my data: https://www.dropbox.com/sh/pnkspwnn1qslm6u/JapTKCwqMS

What I run is;

    wa=read.table('wa_class.txt', sep="", header=F, na.string="0")
    names(wa)=c("Class","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
    wam=melt(wa)
wam$variablen=as.numeric(wam$variable)

How it looks like

    > head(wam)
                   Class variable     value variablen
1         Actinobacteria      Jan 38.115163         1
2          Flavobacteria      Jan        NA         1
3        Sphingobacteria      Jan  3.640469         1
4    Alphaproteobacteria      Jan 13.631663         1
5 Betaproteobacteria_b28      Jan  3.718671         1
6     Betaproteobacteria      Jan 14.732354         1

ggplot(na.omit(wam[,c("Class","value","variablen")]), aes(wam,x=variablen, y=value, fill=Class)) + geom_area(color="black") + geom_linerange(aes(ymax=value), position="stack") + scale_x_continuous(breaks=1:max(wam$variablen)) + labs(title="Water", x="Month", y="Relative abundance (%)")

...so I have tried to correct for the missing values with use na.omit on the variables I plot. However, I get layers in the plot which e.g. is overriding each other (see dropbox folder).

I found this post (see dropbox folder) which corrected for it, but seemed only to have one. And I couldn't reproduce it since the link to the data is dead.

Any help would really be appreciated!

Thanks,

Jo


回答1:


One solution is to read zeros as zero values. If you don't use na.string="0", the plot will look like this:



来源:https://stackoverflow.com/questions/12849175/correct-for-missing-values-in-a-stacked-area-plot-using-ggplot2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!