简体   繁体   中英

How to remove NA for multivariate time series in order to plot boxplot using ggplot?

i have a data frame containing "Dissolved Oxygen (a time series)" as shown below:

head(DO):
Month Season       site1   site2  site3   site4     site5 site6
1   Dec Winter       NA      3.0   0.00     5.60    4.1   3.8
2   Jan Winter       0.0     0.0   NA       5.70    4.0   6.0
3   Feb Winter       0.0     NA    0.00     3.30    1.6   5.5
4   Mar Spring       0.2     1.9   0.00     6.20    NA    8.5
5   Apr Spring       0.0     0.5   3.95     5.00    NA    7.7
6   May Spring       0.0     1.5   1.18     6.50    NA    5.9

I want to plot box plot using ggplot so as to see the variation across different sites. Note that Month and Seasons are treated as factors. i used:

library(reshape2) 
DO.m <- melt(DO, id=c("Month", "Season"))

when i run the above code i got warning as below:

Warning message:
attributes are not identical across measure variables; they will be dropped.

however i ignored the warning and tried to plot the data (as suggested by the forum) using:

ggplot(subset(DO.m, !is.na(value)), aes(x=variable,y=value, col=variable)) +
    geom_boxplot(outlier.colour = "red", outlier.size = 1) + 
    labs(title = "DO", x = "Station", y = "DO(mg/l)", color = "Station") +
  geom_jitter(alpha= 0.4, shape=16, position=position_jitter(0.2)) 

the above code plots very abstract image which is not a box plot. so how can i remove NA for all the sites from the melt data? your help would be appreciaated

Try passing na.rm = TRUE to melt:

library(reshape2)
DO.m <- melt(DO, id=c("Month", "Season"), na.rm = TRUE)

As far as removing attributes and whether that might cause any problems, a dput of a subset of your data would be helpful. But regardless, letting melt pull out the NA values is probably the best place to do this.

I'd add a question as to whether you've inspected the different layers of your ggplot2, to see what they produce one at a time. For example, are you getting something when you just use your ggplot layer + geom_boxplot layer WITHOUT the geom_jitter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM