2012-12-28 42 views
6

我創建了一個包含多個線型,顏色和填充區域的繪圖。下面的代碼產生兩個圖例(一個顯示線條類型,另一個顯示線條顏色) - 我需要將它們合併爲一個顯示線條和線條顏色的圖例。 [有顯示「填充」對象第三個傳說,但是這很好]ggplot2手動指定顏色和線型 - 複製圖例

我跟着這裏給出的方法: Controlling line color and line type in ggplot legend ,試圖得到一個傳奇人物 - 但最終這種雙重傳奇行爲 - 什麼我做錯了嗎?

library(ggplot2) 
library(scales) 
data = structure(list(Dates = structure(c(1351713600, 1351717200, 1351720800, 
    1351724400, 1351728000, 1351731600), class = c("POSIXct", "POSIXt" 
), tzone = "MST"), CumHVAC_Def_Stoch_Min = c(146.4006, 146.6673, 
    146.9336, 147.1996, 147.4648, 147.5964), CumHVAC_Def_Stoch_1st = c(188.0087, 
    188.2753, 188.5416, 188.8077, 189.0729, 189.2045), 
    CumHVAC_Def_Stoch_Mean = c(204.7234, 204.9901, 205.2564, 205.5225, 205.7876, 205.9193), 
    CumHVAC_Def_Stoch_3rd = c(228.8813, 229.1476, 229.4135, 229.6793, 229.9442, 230.0757), 
    CumHVAC_Def_Stoch_Max = c(295.145, 295.4117, 295.6779, 295.944, 296.2092, 296.3408), 
    CumHVAC_Opt_Stoch_Min = c(112.4095, 112.6761, 112.9424, 113.2085, 113.4737, 113.6053), 
    CumHVAC_Opt_Stoch_1st = c(134.8893,135.156, 135.4223, 135.6883, 135.9535, 136.0851), 
    CumHVAC_Opt_Stoch_Mean = c(156.8854, 157.1521, 157.4184, 157.6845, 157.9496, 158.0813), 
    CumHVAC_Opt_Stoch_3rd = c(168.7301, 168.9971, 169.2636, 169.5299, 169.7953, 169.927), 
    CumHVAC_Opt_Stoch_Max = c(241.2483, 241.5151, 241.7814, 242.0476, 242.3128, 242.4444), 
    CumHVAC_Def_Dtrmn = c(188.7523, 189.0189, 189.2852, 189.5513, 189.8165, 189.9481), 

    CumHVAC_Opt_Dtrmn = c(86.8116, 87.0782, 87.3445, 87.6105, 87.8757, 88.0073), 
    CS_Opt_Stoch = c(0, 0, 0, 0, 0, 0), CS_Opt_Dtrmn = c(0, 0, 0, 0, 0, 0), 
    CS_Default = c(0, 0, 0, 0, 0, 0)), .Names = c("Dates", "CumHVAC_Def_Stoch_Min", 
    "CumHVAC_Def_Stoch_1st", "CumHVAC_Def_Stoch_Mean", "CumHVAC_Def_Stoch_3rd", 
    "CumHVAC_Def_Stoch_Max", "CumHVAC_Opt_Stoch_Min", 
    "CumHVAC_Opt_Stoch_1st","CumHVAC_Opt_Stoch_Mean", "CumHVAC_Opt_Stoch_3rd", 
    "CumHVAC_Opt_Stoch_Max", "CumHVAC_Def_Dtrmn", "CumHVAC_Opt_Dtrmn", "CS_Opt_Stoch", 
    "CS_Opt_Dtrmn", "CS_Default"), row.names = 691:696, class = "data.frame") 

stochdefcolor = 'red' 
stochoptcolor = 'green' 
dtrmndefcolor = 'darkred' 
dtrmnoptcolor = 'darkgreen' 

eb09 <- aes(x = Dates, ymax = CumHVAC_Def_Stoch_3rd, ymin = CumHVAC_Def_Stoch_1st, fill="StochDef") 
eb10 <- aes(x = Dates, ymax = CumHVAC_Opt_Stoch_3rd, ymin = CumHVAC_Opt_Stoch_1st, fill="StochOpt") 
State = c('a','b','c','d','e','f','g','h'); 

ln1 <- aes(x=Dates,y=CumHVAC_Def_Stoch_Mean, color=State[1],linetype=State[1]) 
ln2 <- aes(x=Dates,y=CumHVAC_Opt_Stoch_Mean, color=State[2],linetype=State[2]) 
ln3 <- aes(x=Dates,y=CumHVAC_Def_Dtrmn,color=State[3],linetype=State[3]) 
ln4 <- aes(x=Dates,y=CumHVAC_Opt_Dtrmn,color=State[4],linetype=State[4]) 

ln5 <- aes(x=Dates,y=CumHVAC_Def_Stoch_Max,color=State[5],linetype=State[5])#,linetype = 2] 
ln6 <- aes(x=Dates,y=CumHVAC_Def_Stoch_Min,color=State[6],linetype=State[6])#,linetype = 3) 
ln7 <- aes(x=Dates,y=CumHVAC_Opt_Stoch_Max,color=State[7],linetype=State[7])#,linetype = 2) 
ln8 <- aes(x=Dates,y=CumHVAC_Opt_Stoch_Min,color=State[8],linetype=State[8])#,linetype = 3) 

quartz() 
ggplot(data) + 
    geom_ribbon(eb09, alpha=0.4) + 
    geom_ribbon(eb10, alpha=0.4) + 
    geom_line(ln1,size=1) + 
    geom_line(ln2,size=1) + 
    geom_line(ln3,size=1) + 
    geom_line(ln4,size=1) + 
    geom_line(ln5,size=.7) + 
    geom_line(ln6,size=.7) + 
    geom_line(ln7,size=.7) + 
    geom_line(ln8,size=.7) + 
    xlab("X-lab") + 
    ylab("Y-Lab") + 
    opts(title = expression('Dummy Title'), 
     panel.background = theme_rect(fill = "transparent"), 
     panel.grid.minor = theme_blank(), 
     panel.grid.major = theme_blank(), 
     plot.background = theme_rect(fill = "transparent")) + 
    scale_linetype_manual(values=c(1,1,1,1,2,3,2,3)) +  
    scale_colour_manual(name=c("Lines"), 
         values=c(stochdefcolor, 
           stochoptcolor, 
           dtrmndefcolor, 
           dtrmnoptcolor, 
           stochdefcolor, 
           stochdefcolor, 
           stochoptcolor, 
           stochoptcolor)) + 
    scale_fill_manual(name='1st-3rd Quartiles', 
        breaks=c('StochDef','StochOpt'), 
        values=c(stochdefcolor,stochoptcolor), 
        labels=c('Stoch DEF','Stoch OPT')) 

...因爲我是一個新用戶,我不能張貼圖片...

+2

歡迎SO並感謝您發佈您的代碼。鑑於這不是一個簡單的問題,如果你可以包含你的數據(上面的'data'對象)或其子集,或許使用'dput'作爲開始,這將是有幫助的。如果你這樣做了,其他用戶將能夠在自己的R設置中複製,粘貼和試驗你的代碼。 – SlowLearner

+0

這需要很多努力,但如果我不得不猜測,你的問題在於你迫使ggplot創建一堆手動縮放,而不是簡單地將State添加爲變量並將顏色和線型映射到它。這將涉及到一些「熔化」和重新排列你的數據,但我99%肯定你只能通過一個geom_line調用來做到這一點。 – joran

+0

@SlowLearner - 感謝您的提示,我將數據和足夠的代碼添加到發佈中,以便它可以爲任何人運行,就像它爲我所做的一樣。 – RyanStochastic

回答

13

爲在@joran你需要創建一個可變的狀態,並將其綁定的評論稱到colorlinetype然後ggplot爲你做其餘的。例如,這裏的圖很簡單,因爲你把數據放在正確的形狀中。

要操縱您需要的數據,我建議您學習plyrreshape2

## I use your data 
## I melt my data 
library(reshape2) 
measure.vars <- colnames(dat)[c(1:12)][-c(1,3,5,10)] 
mydata.melted <- melt(mydata, 
         measure.vars= measure.vars, ## I don't use all the variables only 
                     States ones 
         id.vars='Dates') 


states.list <- list('a','b','c','d','e','f','g','h') 
names(states.list) <- measure.vars 
mydata.melted$State <- NA 
library(plyr) 
mydata.melted <- ddply(mydata.melted, 
         .(variable),transform, 
         State=states.list[[unique(variable)]]) 
## I plot using the rights aes  

stochdefcolor = 'red' 
stochoptcolor = 'green' 
dtrmndefcolor = 'darkred' 
dtrmnoptcolor = 'darkgreen' 
library(ggplot2) 
ggplot(subset(mydata.melted)) + 
    geom_line(aes(x=Dates,y=value, 
       color=State,linetype=State))+ 
    scale_linetype_manual(values=c(1,1,1,1,2,3,2,3)) + 
    scale_size_manual(values =rep(c(1,0.7),each=4))+ 
    scale_color_manual(values=c(stochdefcolor,stochoptcolor, 
           dtrmndefcolor, dtrmnoptcolor, 
           stochdefcolor,stochdefcolor, 
           stochoptcolor,stochoptcolor)) 

enter image description here

+0

感謝您的代碼和建議;我最初試圖融化我的數據,但'日期'列是POSIXct(日期)格式(請參閱我的數據結構的更新發布) - 並且我解決了融合結構的問題。我會花一些時間與你的代碼,plyr和reshape2再次作出反應。 – RyanStochastic

+0

@RyanStochastic我更新我的答案以包含您的數據。希望這對你有所幫助? – agstudy

+0

非常感謝!雖然我不能承認完全理解重塑和plyr操作(還),但這些代碼完成我所需要的。 – RyanStochastic