In trying to answer this question:
Compare emissions from motor vehicle sources in Baltimore City with emissions from motor vehicle sources in Los Angeles County, California (
fips == "06037"
). Which city has seen greater changes over time in motor vehicle emissions?
I am able to answer the query but not able to create legends using ggplot:
#load library
library(data.table)
library(ggplot2)
#import rds files
NEI <- readRDS("summarySCC_PM25.rds")
SCC <- readRDS("Source_Classification_Code.rds")
#convert to data tables
NEI.DT <- data.table(NEI)
SCC.DT <- data.table(SCC)
#Get data for Baltimore
NEI.DT.Baltimore <- NEI.DT[fips=="24510", ]
NEI.DT.LA <- NEI.DT[fips=="06037", ]
#Check for Motor & Vehicle in SCC data using EI.Sector
motorVehicelSrcs <- SCC.DT[grep("[Mm]obile | [Vv]ehicles", EI.Sector), SCC]
emissionsBaltimore <- NEI.DT.Baltimore[SCC %in% motorVehicelSrcs, sum(Emissions), by="year"]
emissionsLA <- NEI.DT.LA[SCC %in% motorVehicelSrcs, sum(Emissions), by="year"]
View(emissionsBaltimore)
View(emissionsLA)
setnames(emissionsBaltimore , "V1" , "Emissions")
setnames(emissionsLA , "V1" , "Emissions")
#plot data
plot_x <- ggplot(emissionsBaltimore, aes(year, Emissions)) + geom_line(aes(data=emissionsBaltimore, x=year, y= Emissions, colour = "MyLine1")) + geom_point(colour = "blue") + geom_line(aes(data = emissionsLA, x= year, y= Emissions, colour = "MyLine2")) + geom_point(data= emissionsLA, colour = "black") + scale_colour_manual(name="Line Color", values=c(MyLine1="green", MyLine2="red"))
print(plot_x)
I am getting the following error:
Error: "Don't know how to automatically pick scale for object of type data.table/data.frame. Defaulting to continuous
Error: Aesthetics must either be length one, or the same length as the dataProblems:emissionsBaltimore"
Can someone help in resolving the error?
The data for the above code is available at: https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2FNEI_data.zip