小编Jul*_*iaK的帖子

为行ggplot2分配颜色

我想使用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语句中将第一行设置为第一种颜色,将第二行设置为第二种颜色,依此类推?

plot r colors ggplot2 line-plot

1
推荐指数
1
解决办法
6036
查看次数

标签 统计

colors ×1

ggplot2 ×1

line-plot ×1

plot ×1

r ×1