使用ggplot的多个图:打印图中出错

时间:2015-02-21 12:20:00

标签: r ggplot2 legend aesthetics

试图回答这个问题:

  

比较巴尔的摩市机动车排放源与加利福尼亚州洛杉矶县机动车排放源的排放量(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

2 个答案:

答案 0 :(得分:0)

此更改应该有效:

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)

修改 如果需要包含图例,则将数据集融合为一个更简单:

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)

答案 1 :(得分:0)

您可以通过修改数据框来实现

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

(你需要“tidyr”套餐才能使用'gathe'r功能)

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"))

否则,使用'multiplot'功能