Wis786
Wis786

Reputation: 53

Categorical Variable subsetting in R

How can I subset my data based on “lake” and then plot the nutrients (P,N,C) versus date for all three lakes on the same plot. Another thing is that I have blanks (NA) in my data and I don’t want to replace it with zero. Because it will show that concentration of chemical is zero which is not true. Is there any way that R don’t plot any point for NA values? Looks like I get strange looking plots in R with NA values. But excel does not have that problem. (excel simply does not plot anything for blanks)

lake                     date    depth        P          N           C
East Long Lake       9/5/1994     0.7      21.9      254.8
East Long Lake       9/5/1994     1.0      30.1     1190.0       257.0
East Long Lake       9/5/1994     1.5      20.5      256.6
East Long Lake       9/5/1994     2.5      22.1      249.0
East Long Lake       9/5/1994    12.0     212.5     2011.6      1090.6
Central Long Lake   6/30/1995     0.6      22.9       91.1
Central Long Lake   6/30/1995     4.0
Peter Lake           7/6/1994     6.1      41.9      527.2        29.6
Peter Lake           7/6/1994    12.0     138.8     1994.0      1409.6
Peter Lake          7/13/1994     0.0      19.1      746.7        22.6 
Peter Lake          7/13/1994     0.7      19.2      21.3

Upvotes: 0

Views: 786

Answers (1)

Kristofersen
Kristofersen

Reputation: 2806

I think this is what you're looking for.

library(data.table)
library(ggplot2)
dt = fread("tmp.csv")
dt$date = as.Date(dt$date, "%m/%d/%Y")
dt[,"depth" := NULL]
d <- melt(dt, id.vars=c("lake","date"))

ggplot(d=subset(d, !is.na(value)), aes(date, value, col = variable)) + geom_point() + facet_wrap(~ lake)

This might be the graph you're looking for:

ggplot(d=subset(d, !is.na(value)), aes(date, value, col = lake)) + geom_point() + facet_wrap(~ variable)

Upvotes: 1

Related Questions