0

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

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
Ganesh Bhat
  • 295
  • 7
  • 20

2 Answers2

0

This change should work:

plot_x <- ggplot(NULL, aes(year, Emissions)) +
          geom_line(data = emissionsBaltimore, colour = "green") + 
          geom_point(data = emissionsBaltimore, colour = "blue") + 
          geom_line(data = emissionsLA, colour = "red") + 
          geom_point(data = emissionsLA, colour = "black")
print(plot_x)

Edit: If a legend needs to be included, it would be simpler to melt the datasets into one:

library(reshape2)
emissionsAll <- melt(list(BA=emissionsBaltimore, LA=emissionsLA), id.vars="year")

plot_x2 <- ggplot(emissionsAll, aes(year, value, group=L1, color=L1)) + 
           geom_line() + 
           geom_point()
print(plot_x2)
StrikeR
  • 1,598
  • 5
  • 18
  • 35
  • This does work but does not show the legends which is critical and hence I had used the scale_colour_manual function – Ganesh Bhat Feb 21 '15 at 13:23
0

You can make it by modifying your data frame

x <- cbind(emissionsBaltimore, emissionsLA$Emissions)
colnames(x) <- c("year", "Baltimore", "LA")
y <- x %>% gather(City, Emissions, -year)

(you need "tidyr" packages for using 'gathe'r function)

ggplot(y, aes(x=year, y=Emissions, group=City))+
    geom_point()+
    geom_line(aes(colour=City))+
    scale_colour_manual(name="Line Color", values=c("green", "red"))

otherwise, using 'multiplot' function

Jm S.
  • 1
  • 1