ggplot中的特定可变颜色 [英] Specific Variable Color in ggplot

查看:79
本文介绍了ggplot中的特定可变颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我具有以下数据集:

Let's say I have the following data set:

set.seed(1)
df <- data.frame(
  Index = 1:10,
  Heat = rnorm(10),
  Cool = rnorm(10),
  Other = rnorm(10),
  a = rnorm(10),
  b = rnorm(10)
)

现在,我想针对Index制作每个列的折线图.我通过以下方式执行此操作:

Now I want to make a line graph of each of the columns against Index. I do this the following way:

df.plot <- ggplot(
  data = tidyr::gather(df, key = 'Variable', value = 'Component', -Index),
  aes(x = Index, y = Component, color = Variable)
) +
  geom_line()

但是现在我要更改它,以使变量Heat,Cool和Other分别为红色,蓝色和绿色.所以我尝试了类似的东西:

but now I want to change it so that the variables Heat, Cool, and Other are red, blue, and green respectively. So I tried something like:

set.colors <- c(Heat = 'red', Cool = 'blue', Other = 'green')
df.plot + scale_color_manual(values = set.colors)

这里的问题是set.colors变量没有足够的颜色(没有表示a和b),但我只是想让ggplot自动为这两个变量分配颜色,因为在我的实际代码中,没有告诉这些列中有多少列的方法.因此,基本上,我希望ggplot进行常规的颜色分配,然后搜索名称为Heat,Cool或Other的任何变量(无法保证这三个变量中的任何一个或全部都会出现),然后将其颜色更改为红色,蓝色和绿色,而不会更改任何其他变量的颜色.

The problem here is that the set.colors variable doesn't have enough colors (a and b aren't represented) but I just want ggplot to automatically assign colors to both of these variables because in my actual code, there's no way of telling how many of these columns there will be. So basically I want ggplot to do it's normal color assignment and then search for any variables that are names Heat, Cool, or Other (there's no guarantee that any or all of these three will be present) and then change their colors to red, blue, and green respectively without changing the colors of any other variable.

推荐答案

使用默认调色板混合您自己的颜色是一个令人震惊的坏主意.不过,这是一种解决方法-与其他答案类似,但可能更通用,并且按照您的要求将ggplot的默认调色板用于其他所有内容.

Mixing your own colors in with a default color palette is a breathtakingly bad idea. Nevertheless, here is one way to do it - similar to the other answer but perhaps a bit more general, and uses ggplot's default color palette for everything else as you asked.

library(ggplot2)
library(reshape2)
gg.df <- melt(df, id="Index", value.name="Component")
ggp <- ggplot(gg.df, aes(x = Index, y = Component, color = variable)) +
  geom_line()

lvls  <- levels(gg.df$variable)
cols  <- setNames(hcl(h=seq(15, 375, length=length(lvls)+1), l=65, c=100),lvls)
cols[c("Heat","Cool","Other")] <- c("#FF0000","#0000FF","#00FF00")

ggp + scale_color_manual(values=cols)

编辑:刚刚意识到我从来没有说过为什么这是一个坏主意. 这篇文章有点儿涉及到一些非常好的参考.要点是,选择默认颜色是有充分理由的,而不仅仅是使绘图看起来漂亮".因此,除非有迫切的需求,否则您真的不应该惹他们.

Edit: Just realized that I never said why this is a bad idea. This post gets into it a bit, and has a few really good references. The main point is that the default colors are chosen for a very good reason, not just to make the plot "look pretty". So you really shouldn't mess with them unless there's an overwhelming need.

这篇关于ggplot中的特定可变颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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