help with getting colors to work correctly using geom_line()

hi I am trying to personalize the colors of the geom_line() graph but every time I add the aes(color = 'green','blue') argument my graph becomes one straight vertical line with two colors (not green and blue but the default blue and red labelled green and blue)

my data is esentially two insurance groups and I am comparing their rates of coverage through a certain time period.

ggplot(data = mydata aes(x = group, y = rate, group = insurance)) + geom_line(aes(color = insurance)) + geom_point()

any help is greatly appreciated.

If you want to set the colors, you don't do that in the aes() function. That function is for setting the relationship between the values in the data and features on the plot. You can set the colors manually with the function scale_color_manual(). Here is a simple example.

mydata <- data.frame(group =c(1,1,2,2), insurance = c("A","B","A","B"), 
                     rate = c(0.5,0.45,0.60,0.65))
library(ggplot2)
ggplot(data = mydata, aes(x = group, y = rate, group = insurance)) + 
  geom_line(aes(color = insurance)) + geom_point() +
  scale_color_manual(values = c(A = "green", B = "blue"))

thank you so much @FJCC, it worked!

If colours need to be set, this must be done manually scale_color_manual(). Here, this is not done via the aes() function, as this serves a completely different purpose, namely to enable us and allow us to set any relationships between the features created in the graph and the values that have been added in them.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.