ggplot2手动指定颜色&线型 - 复制图例 [英] ggplot2 manually specifying color & linetype - duplicate legend

查看:155
本文介绍了ggplot2手动指定颜色&线型 - 复制图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含多个线型,颜色和填充区域的绘图。下面的代码产生两个图例(一个显示线条类型,另一个显示线条颜色) - 我需要将它们合并成一个图例,显示线型和线型。 [有一个显示'fill'对象的第三个图例,但没关系]

我遵循以下给出的方法:
控制ggplot图例中的线条颜色和线型
试图获得一个传说 - 但结束了这种双重传说行为 - 我做错了什么?

  library(ggplot2)
library(scale)
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.147
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,
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 (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)),.Names = c(Dates ,CumHVAC_Def_Stoch_Min,
CumHVAC_Def_Stoch_1st,C umHVAC_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 = '绿色'
dtrmndefcolor = '暗红色'
dtrmnoptcolor = '深绿'

eb09< - AES(X =日期,YMAX = CumHVAC_Def_Stoch_3rd,YMIN = CumHVAC_Def_Stoch_1st,填= StochDef)
EB10< - AES(X =日期,YMAX = CumHVAC_Opt_Stoch_3rd,YMIN = CumHVAC_Opt_Stoch_1st,填充= StochOpt)
态= C( 'A', 'b', 'C', 'd', 'E', 'F', 'G', 'H');

ln1 <-aes(x =日期,y = CumHVAC_Def_Stoch_Mean,color = State [1],linetype = State [1])$ ​​b $ b 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 =日期,y = CumHVAC_Opt_Dtrmn,color = State [4],linetype = State [4])

ln5 < - aes(x = Dates, Y = CumHVAC_Def_Stoch_Max,颜色=状态[5],线型=状态[5])#,线型= 2]
LN6< - AES(X =日期,Y = CumHVAC_Def_Stoch_Min,颜色=状态[6],线型(x =日期,y = CumHVAC_Opt_Stoch_Max,color = State [7],linetype = State [7])#,linetype = 2)#,linetype = 3)
ln7<
ln8 <-aes(x =日期,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)+
ge om_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 =表达式('Dummy Title'),
面板。 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-3r (stochdef','stochOpt'),
values = c(stochdefcolor,stochoptcolor),
labels = c('Stoch DEF','Stoch OPT' '))

...因为我是新用户,所以无法发布图片...

解决方案

正如在@joran的评论中所说的那样您需要创建一个变量状态并将其绑定到 color linetype 然后ggplot为你完成剩下的工作。例如,这里的情节很简单,因为你把数据放在正确的形状中。



为了处理你需要的数据,我建议你学习 plyr reshape2

  ## I使用您的数据
##我将我的数据
library(reshape2)
measure.vars< - colnames(dat)[c(1:12)] [ - c(1,3 ,5,10)]
mydata.melted< - melt(mydata,
measure.vars = measure.vars,##我不使用所有变量
States $ b'b','c','d','e','b','b',' ,'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)]])
# #我绘制你唱歌的权利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))


I'm creating a plot with multiple linetypes, colors, and filled regions.The code below produces two legends (one shows line types, the other shows line colors) - and I need them to be combined into one legend that shows both linetype and linecolor. [there is a third legend showing the 'fill' objects, but that's fine]

I followed the methodology given here: Controlling line color and line type in ggplot legend to try to get a single legend - but ended up with this double-legend behavior - what have I done wrong?

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

...since I'm a new user, I can't post an image...

解决方案

As said in the comment by @joran You need to create a variable state and bind it to color and linetype then ggplot do the rest for you. For example ,Here the plot s very simple since you put data in the right shape.

To manipulate data you need I advise you to learn plyr and reshape2 !

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

这篇关于ggplot2手动指定颜色&amp;线型 - 复制图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆