我正在构建一些代码来绘制能源供应与用户定义的觅食行会对鸟类群体的能量需求。我事先并不知道用户将指定多少个公会,我需要创建公会特定的情节。我希望能够为能源供应和需求指定线路的颜色。这是我的数据:
guilds <- c("ducks","geese") #I have 2 foraging guilds
dayz <- c(1,2,3,4,5,6) #The plot will span 6 days
guild1_supply <- c(100,120,150,130,110,70) #energy available to guild 1
guild2_supply <- c(70,90,110,120,100,80) #energy available to guild 2
supply_by_guild <- cbind(guild1_supply,guild2_supply)
guild1_demand <- c(10,80,120,130,70,20) #energy demand for guild 1
guild2_demand <- c(5,45,75,60,30,0) #energy demand for guild 2
demand_by_guild <- cbind(guild1_demand,guild2_demand)
setwd("C:/Users/XXX) #Set working directory for figure output
现在我在for循环中创建了特定于公会的图。
for (i in 1:length(guilds)) { # use a for loop to create data frames
temp <- data.frame(cbind(dayz,supply_by_guild[,i],demand_by_guild[,i]))
names(temp)[2] <- "Food Energy Supply" # rename variable
names(temp)[3] <- "Food Energy Demand" # rename variable
temp <- melt(temp, id.vars = c("dayz")) # melt data frame to long format
names(temp)[2] <- "Legend" # rename variable
names(temp)[3] <- "energy" # rename variable
jpeg(paste("Fig",i+4,".jpg",sep=""),width=2000, height=1000, res=300)
print(ggplot(data=temp, aes(x=dayz, y=energy,group=Legend, color=Legend)) + geom_line() + xlab("Day") + ylab(paste("Energy (",energy_unit,")",sep="")) + ggtitle(paste("Figure ",i+4,". Daily Food Energy Supply and Demand: ",guilds[i],sep="")))
dev.off()
}
以上代码有效,但是当我尝试通过添加+ scale_color_manual(values=c("cyan4", "orangered1"))
手动指定行的颜色时,我得到错误“二进制运算符的非数字参数”。任何帮助非常感谢。