# How do I make a simple graph with subset factor and two variables? (help pls)

My data contains the Years of sampling / Site (with 6 different sites)/ and 3 different chemical parameters

I want to create a simple graph with the year on the X and Nitrate on the Y for one site in particular ...

I don't know what code to use, I tried this
plot(Allsites_data\$Nitrate~Allsites_data\$Year) but it doesn't classify the site I want... Thanks!

Try subsetting the data.

``````     dat1  <-  subset(Allsites_data, Site == "Brownstown")

``````

Then graph dat1

Rince and repeat for each value of Site
There are other ways and probably better ones but this should work in base R

For future reference, here are some hints on asking questions in the forum.

1 Like

Thanks a mil for that!

Building on @jkrideau's response a little bit, to get the plot in a single command using base R (plot and subset):

``````plot(x = subset(Allsites_data\$Year, Allsites_data\$Site == "Brownstone"),
y = subset(Allsites_data\$Nitrate, Allsites_data\$Site == "Brownstone"))
``````

The kicker is this may not give you quite what you want depending on the data type of `Year` in the data frame (which is one of the reasons a reproducible example is nice to have). If `Year` is numeric, then you may see things like "2011.5" in the x-axis of a dot plot.

If, instead, `Year` is a factor, you may get a y-axis that is more what you expected, and you will likely get a boxplot instead of a dot plot.

Depending on what you're looking for, specifically, you may need to do a little bit of light work on the data frame or do some additional formatting work within `plot()` (or, potentially, consider switching over and using `ggplot()`, but my sense is that you're not looking to dive into that world).

Good luck!

1 Like

Thanks so much, really helped !!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.