英文:
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("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()
Therefore, I defined a color palette.
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)
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("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"))
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")
)
<!-- -->
英文:
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("A", "B", "C", "D", "E")
)
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论