在R中的ggplot2中控制多个geom_line的颜色 [英] control color of multiple geom_line in ggplot2 in R

查看:89
本文介绍了在R中的ggplot2中控制多个geom_line的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制3条回归线,每条tau值对应一条,而每条色线均带有其颜色,如数据集中所指定.互联网上说您是根据一般情节美学提供颜色变量的(

I want to plot 3 regression lines, one for each value of tau, and each one with its colour, as specified in the dataset. The internet says you provide the color variable at the general plot aesthetics (http://ggplot.yhathq.com/docs/geom_line.html), but that does not seem to work. Any body would konw what to do?

Thanks in advance,

here is the dataframe https://www.dropbox.com/s/fqmayg6asf5e3eu/tautest.csv?dl=0 here is the code

 ddl=read.csv("tautest.csv")
 ggplot(ddl, aes(x = predictor, y = value, color=col)) + 
 geom_line(aes(group = tau),size = 0.8)

ggplot2 gives some colors, but I want the colors in my dataframe, why would ggplot2 give other colors?

解决方案

ggplot 'maps' aesthetics to scales, meaning that it tries to fit a variable onto a scale. The default scale for categorical variables is scale_colour_discrete(), which matches a colour to every unique entry. Since ggplot doesn't know that the values in your col column are colour names, you have to let ggplot know that these colours are to be interpreted literally. You can do that with scale_colour_identity().

ggplot(ddl, aes(x = predictor, y = value, color = col)) + 
  geom_line(aes(group = tau),size = 0.8) +
  scale_colour_identity()

这篇关于在R中的ggplot2中控制多个geom_line的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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