r-使用ggplot的多个绘图:打印绘图时出错



在试图回答这个问题时:

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

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

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

(您需要"tidyr"软件包才能使用"gathe"功能)

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

否则,使用"多批次"功能

此更改应该有效:

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)

相关内容

  • 没有找到相关文章

最新更新