我希望使用 ggplot2 库函数为每条线绘制具有自定义颜色的多个模型数据。我可以使用 ggplot 生成的随机颜色来做到这一点,但是如果我尝试放置自定义颜色,则图例正在消失。请帮我解决这个问题。下面是可重现的示例,我想要一条黑色和一条绿色线以及标签。提前致谢。
library(ggplot2)
flux<-c(-4.351645e-11 ,7.724330e-10 ,-1.631623e-10,2.832141e-10,-2.396649e-11,
# first model 5 entries
-7.825169e-10 ,-2.534337e-10,-3.837198e-10 ,-2.765284e-10,-6.515152e-10)
# 2nd model 5 entries
model<-c('SDGVM','SDGVM','SDGVM','SDGVM','SDGVM', # 1st model
'TRIFFID','TRIFFID','TRIFFID','TRIFFID','TRIFFID') # 2nd model
latitude<-c(-34,-36,-39,-41,-44,-34,-36,-39,-41,-44)
color<-c('black','black','black','black','black',
'green','green','green','green','green')
input_df <-structure(list(flux = flux, model = model, lat = latitude,
color_plate=color),
.Names = c("flux","model", "lat","color_assigned"), row.names = c(NA, -10L),
class = "data.frame")
xlims=c(-50,-30) # x axis extent
custom_break<-seq(min(xlims),max(xlims),by=2)
theme_set(theme_bw(12))
chart <-ggplot(input_df, aes(x=lat, group=model, colour=model, fill=model)) +
geom_line(aes(y=flux), size=1.0) +
theme(legend.position='bottom') +
scale_x_continuous('Latitude',limits=xlims,breaks=custom_break) +
#custom breaks_to customize labels in x axis
scale_y_continuous(expression('Flux Difference')) +
scale_colour_discrete(name='', guide=guide_legend(nrow=4)) +
scale_fill_discrete(name='Model') +
geom_hline(aes(yintercept=0)) #to add black line at 0
print(chart)