Home > other >  ggplot - how to set the color of lines using qplot without mapping colors to a variable?
ggplot - how to set the color of lines using qplot without mapping colors to a variable?

Time:05-09

let's say that I have a very simple piece of code like this:

qplot(1:5, 1:5, geom = "line", color = "blue")

Unfortunately, this code will not produce a blue line, but, instead, it will try to create mapping for the color and thus you will see that the legend has appeared on the right side of the graph with the value "blue" being shown in red.

When using normal ggplot you can do something like this:

ggplot(data, aes(x = x, y = y)) 
  geom_line(color = "blue")

So I thought that it would also work in qplot like that. But it doesn't. How can I create a simple blue line without mapping the color to a variable?

CodePudding user response:

We can use scale_color_identity

qplot(1:5, 1:5, geom = "line", color = "blue")   
      scale_color_identity()

-output

enter image description here

CodePudding user response:

Another way would be to use the inhibit function I() like this:

qplot(1:5, 1:5, geom = "line", colour = I("blue"))
  • Related