nouse
nouse

Reputation: 3461

Forcing ggplot to use only one colour for fill/color statement

Consider this dataframe:

library(ggplot2)
library(reshape)
df <- data.frame(A=1:10, B=rnorm(10), C=rnorm(10), D=rnorm(10))
df.melt <- melt(df, id="A")

plotting without specifying the color in aesthestics gives one line:

ggplot() +
  geom_line(data=df.melt, aes(x=A, y=value))

but i want to have the three variables separated:

ggplot() +
  geom_line(data=df.melt, aes(x=A, y=value, colour=variable))

but with one color!

My solution is to define a color palette with all black,

col <- rep("black", 3)

ggplot() +
  geom_line(data=df.melt, aes(x=A, y=value, colour=variable)) +
  scale_color_manual(values=col) +
  guides(color=FALSE)

but i wonder if there is a built-in solution?

Upvotes: 0

Views: 705

Answers (1)

scoa
scoa

Reputation: 19867

Use group=variable rather than colour=variable

library(ggplot2)
library(reshape)
df <- data.frame(A=1:10, B=rnorm(10), C=rnorm(10), D=rnorm(10))
df.melt <- melt(df, id="A")

ggplot() +
  geom_line(data=df.melt, aes(x=A, y=value, group=variable)) +
  guides(color=FALSE)

enter image description here

Upvotes: 2

Related Questions