Jul*_*iaK 1 plot r colors ggplot2 line-plot
我想使用ggplot2用5条不同的线绘制线图。我用下面的代码。
plot <- ggplot() +
geom_line(data=MS, aes(x=date, y=MSCI.World.PI, color='MS')) +
geom_line(data=S, aes(x=date, y=SandP.TR, color='S')) +
geom_line(data=BR, aes(x=date, y=MSCI.BRIC.PI, color='BR')) +
geom_line(data=HF, aes(x=date, y=HFRX, color='HF')) +
geom_line(data=LP, aes(x=date, y=LPX50.TR, color='LP')) +
scale_color_manual(values = c("red", "blue", "green", "yellow", "violet" )) +
labs(color="Indices") +
xlab('Time') +
ylab('Price')
plot
Run Code Online (Sandbox Code Playgroud)
“错误”部分是,未按预期对颜色进行排序,这意味着第一行(“ MS”)未分配给第一颜色(“红色”)。似乎该行已按字母顺序分配给了颜色。
我有什么办法可以更改分配,以便在scale_color_manuel语句中将第一行设置为第一种颜色,将第二行设置为第二种颜色,依此类推?
将颜色指定为命名矢量可以解决问题,并使显示方式明确:
scale_color_manual(
values = c(
MS="red",
S="blue",
BR="green",
HF="yellow",
LP="violet" ))
Run Code Online (Sandbox Code Playgroud)