Lodyk Vovchak
Lodyk Vovchak

Reputation: 133

Change one of the line styles in ggplot2 graph with long data format

I have data in the melted form

b <- data.table(q = seq(5), 
                A = rnorm(5, mean=1, sd=1), 
                B = rnorm(5, mean=1.5, sd=1), 
                C = rnorm(5, mean=2, sd=1))
longb <- melt.data.table(b, id.vars = 'q', measure.vars = c(names(b)[-1]))

The to draw three lines one has to do:

ggplot(data = longb, aes(x = q, y = value, colour = variable)) +
  geom_line()

I wander if it is possible to change one of the series (say C) from being line to being dotted graph (like geom_point())?

Upvotes: 0

Views: 203

Answers (1)

iago
iago

Reputation: 3266

Something like this?

ggplot(data = longb, aes(x = q, y = value, colour = variable)) +
  geom_line(aes(linetype = (variable == "C"))) +
  guides(linetype = "none")

enter image description here

Or better:

ggplot(data = longb, aes(x = q, y = value, colour = variable)) +
  geom_line(aes(linetype = variable)) +
  scale_linetype_manual(values = c(1,1,3))

enter image description here

Or well?:

ggplot(data = longb[variable != "C"], aes(x = q, y = value, colour = variable)) +
  geom_line() +
  geom_point(data = longb[variable == "C"], aes(x = q, y = value))

enter image description here

Upvotes: 2

Related Questions