我要翻译的内容: 如何匹配ggplot2中线条和图例条目的颜色?

huangapple go评论54阅读模式
英文:

How can I match the colors of lines and legend entries in ggplot2?

问题

我有一个包含一些光谱数据的数据框(一个x值,多个y值),想要将它们绘制在一个图中。为此,我想使用ggplot。经过一些研究,我找到了一种方法来实现这个目标,但我想将线条的颜色从黑色更改为灰色,以表示浓度的减少。

if (!require("ggplot2")) install.packages("ggplot2")

set.seed(230527)

df.tmp <- data.frame(x = runif(25), 
                     y = cbind(runif(25),
                               runif(25),
                               runif(25),
                               runif(25),
                               runif(25))
                     )
                     
if (!require("reshape")) install.packages("reshape")

df.spec <- data.frame(x = df.tmp[,1], y = df.tmp[,2:6])
df.spec <- melt(df.spec, id.vars = "x")

ggplot(df.spec, aes(x = x, y = value, color = variable)) +
  geom_line()

因此,我定义了一个颜色调色板。

palette.spec <- fields::designer.colors(col = c("black", "lightgrey"), n = NCOL(df.tmp)-1)
if (!require("ggplot2")) install.packages("ggplot2")

set.seed(230527)

df.tmp <- data.frame(x = runif(25), 
                     y = cbind(runif(25),
                               runif(25),
                               runif(25),
                               runif(25),
                               runif(25))
                     )
                     
palette.spec <- fields::designer.colors(col = c("black", "lightgrey"), n = NCOL(df.tmp)-1)

if (!require("reshape")) install.packages("reshape")

df.spec <- data.frame(x = df.tmp[,1], y = df.tmp[,2:6])
df.spec <- melt(df.spec, id.vars = "x")

ggplot(df.spec, aes(x = x, y = value, color = variable)) +
  geom_line() + 
  scale_color_manual(values = palette.spec)

现在,线条的颜色和图例相匹配,但我还必须更改图例条目的名称。

在这样做时,名称是正确的,但图中线条的颜色也发生了变化。

if (!require("ggplot2")) install.packages("ggplot2")

set.seed(230527)

df.tmp <- data.frame(x = runif(25), 
                     y = cbind(runif(25),
                               runif(25),
                               runif(25),
                               runif(25),
                               runif(25))
                     )
                     
palette.spec <- fields::designer.colors(col = c("black", "lightgrey"), n = NCOL(df.tmp)-1)

if (!require("reshape")) install.packages("reshape")

df.spec <- data.frame(x = df.tmp[,1], y = df.tmp[,2:6])
df.spec <- melt(df.spec, id.vars = "x")

ggplot(df.spec, aes(x = x, y = value, color = variable)) +
  geom_line() + 
  scale_color_manual(values = palette.spec,
                     limits = c("A", "B", "C", "D", "E"))

现在图例条目的名称是正确的,但线条的颜色也变了。

是否可能使线条的颜色和图例条目相匹配?

额外问题:是否也可以在不同线之间插入手动偏移量?

英文:

I have a dataframe of some spectra (one x-value, several y-values) and want to plot them in one graph. For this purpose, I want to use ggplot. After some research, I found a way to do so, but I want to change the color of the lines from black to grey, representing a dcrease in concentration.

if (!require(&quot;ggplot2&quot;)) install.packages(&quot;ggplot2&quot;)

set.seed(230527)

df.tmp &lt;- data.frame(x = runif(25), 
                     y = cbind(runif(25),
                               runif(25),
                               runif(25),
                               runif(25),
                               runif(25))
                     )
                     
if (!require(&quot;reshape&quot;)) install.packages(&quot;reshape&quot;)

df.spec &lt;- data.frame(x = df.tmp[,1], y = df.tmp[,2:6])
df.spec &lt;- melt(df.spec, id.vars = &quot;x&quot;)

ggplot(df.spec, aes(x = x, y = value, color = variable)) +
  geom_line()

我要翻译的内容:
如何匹配ggplot2中线条和图例条目的颜色?

Therefore, I defined a color palette.

palette.spec &lt;- fields::designer.colors(col = c(&quot;black&quot;, &quot;lightgrey&quot;), n = NCOL(df.tmp)-1)

if (!require(&quot;ggplot2&quot;)) install.packages(&quot;ggplot2&quot;)

set.seed(230527)

df.tmp &lt;- data.frame(x = runif(25), 
                     y = cbind(runif(25),
                               runif(25),
                               runif(25),
                               runif(25),
                               runif(25))
                     )
                     
palette.spec &lt;- fields::designer.colors(col = c(&quot;black&quot;, &quot;lightgrey&quot;), n = NCOL(df.tmp)-1)

if (!require(&quot;reshape&quot;)) install.packages(&quot;reshape&quot;)

df.spec &lt;- data.frame(x = df.tmp[,1], y = df.tmp[,2:6])
df.spec &lt;- melt(df.spec, id.vars = &quot;x&quot;)

ggplot(df.spec, aes(x = x, y = value, color = variable)) +
  geom_line() + 
  scale_color_manual(values = palette.spec)

我要翻译的内容:
如何匹配ggplot2中线条和图例条目的颜色?

Now, the color of the lines and the legend fits, but I also have to change the names of the legend entries.

When doing so, the names are correct, but also the color of the lines in the graph changes.

if (!require(&quot;ggplot2&quot;)) install.packages(&quot;ggplot2&quot;)

set.seed(230527)

df.tmp &lt;- data.frame(x = runif(25), 
                     y = cbind(runif(25),
                               runif(25),
                               runif(25),
                               runif(25),
                               runif(25))
                     )
                     
palette.spec &lt;- fields::designer.colors(col = c(&quot;black&quot;, &quot;lightgrey&quot;), n = NCOL(df.tmp)-1)

if (!require(&quot;reshape&quot;)) install.packages(&quot;reshape&quot;)

df.spec &lt;- data.frame(x = df.tmp[,1], y = df.tmp[,2:6])
df.spec &lt;- melt(df.spec, id.vars = &quot;x&quot;)

ggplot(df.spec, aes(x = x, y = value, color = variable)) +
  geom_line() + 
  scale_color_manual(values = palette.spec,
                     limits = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;))

我要翻译的内容:
如何匹配ggplot2中线条和图例条目的颜色?

Is it possible to match the colors of the lines and the legend entries?

Bonus: Is it also possible to insert a manual offset between the different lines?

答案1

得分: 1

通过设置limits,您定义了颜色比例尺的有效值。因为variable中的值都不匹配它们,所以它们被映射为NA,并且对应的颜色是灰色。

不要指定limits,可以在您的数据中定义变量标签,或者使用labels

ggplot(df.spec, aes(x = x, y = value, color = variable)) +
  geom_line() + 
  scale_color_manual(
    values = palette.spec,
    labels = c("A", "B", "C", "D", "E")
  )

我要翻译的内容:
如何匹配ggplot2中线条和图例条目的颜色?<!-- -->

英文:

By setting limits you are defining the valid values for the colour scale.
Because none of the values in variable match them, they get mapped to NA
and a corresponding grey colour.

Instead of specifying limits, either define the variable labes in your
data, or use labels:

ggplot(df.spec, aes(x = x, y = value, color = variable)) +
  geom_line() + 
  scale_color_manual(
    values = palette.spec,
    labels = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;)
  )

我要翻译的内容:
如何匹配ggplot2中线条和图例条目的颜色?<!-- -->

huangapple
  • 本文由 发表于 2023年5月28日 06:01:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定