[英]plotting customized color lines and legends in R using ggplot2
我希望使用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)
您設置了錯誤的屬性/屬性。 您想提供可以使用scale_colour_manual
設置的手動顏色。
替換此行:
scale_colour_discrete(name='', guide=guide_legend(nrow=4)) +
與:
scale_colour_manual(values=c("black", "green"), name="",
guide=guide_legend(nrow=4)) +
然后再試一次。
您應該得到如下圖:
這有幫助嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.