在试图回答这个问题时:
比较巴尔的摩市机动车源排放量与加利福尼亚州洛杉矶县机动车源排放量(
fips == "06037"
)。随着时间的推移,哪个城市的机动车排放变化更大?
我能够回答查询,但无法使用 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)
我收到以下错误:
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"
有人可以帮助解决错误吗?
上述代码的数据位于:https ://d396qusza40orc.cloudfront.net/exdata%2Fdata%2FNEI_data.zip