LucySHE LucySHE - 3 months ago 19
R Question

Not sure why this subset is not working in ggplot

I have sub-setted my data set so that only three sites are included, as I only want to plot three sites and the following code does not seem to work with ggplot. Anyone have any idea why?

rm(list=ls())
require(ggplot2)
require(reshape2)
require(magrittr)
require(dplyr)
require(tidyr)
setwd("~/Documents/Results")
mydata <- read.csv("Metals sheet R.csv")
L <- subset(mydata, Site =="B1"| Site == "B2"| Site == "B3", select = c(Site,Date,Al))
L$Date <- as.Date(L$Date, "%d/%m/%Y")
ggplot(data=L, aes(x=Date, y=Al, xaxt="n", colour=Site)) +
geom_point() +
labs(title = "Total Al in the Barlwyd and Bowydd
19/03/2015.", x = "Site",
y = "Total concentration (mg/L)") +
scale_x_date(date_breaks = "1 month", labels = date_format("%m"))


It seems to falter after the ggplot line. Thanks in advance. I have double checked it but can't see anything wrong? I might possibly need a way to only plot three of my 21 sites.

The head of my subsetted L data set looks something like this (x58 reps)

Date Site Al
12/08/2015 B1 22.3
12/08/2015 B2 23.4
12/08/2015 B3 203


Thankyou in advance.

Answer

I think xaxt = "n" is wrong. The ggplot aes function is only for matching variables in your data to plot elements. To remove the x-axis text in ggplot, use the theme function e.g. ggplot2 plot without axes legends etc.

On a separate note, the %in% operator provides a quicker way of selecting a subset of values from a column:

subset(mydata, Site %in% c("B1", "B2", B3"))