0

I have a technical issue with my attempts to plot group differences whilst accounting for 3 variables. This all works fine until I attempt to plot the line of best fit for each group; which results in a plot that makes it difficult to distinguish between groups (as seen below)

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color = Petal.Length, shape = Species)) + geom_point() +
scale_color_viridis_c() +
geom_smooth(method = "lm", se = FALSE, show.legend = TRUE)

enter image description here

I would like to provide a manual discrete colour for each best fit line, so that readers can distinguish between groups easier (for example; something like having a red line for setosa, a white line for versicolor and black line for virginica). Below are the examples of what I have tried so far with their associated error messages.

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color = Petal.Length, shape = Species)) + geom_point() +
scale_color_viridis_c() +
geom_smooth(method = "lm", se = FALSE, show.legend = TRUE, aes(color = Species))

"Error: Discrete value supplied to continuous scale"

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color = Petal.Length, shape = Species)) + geom_point() +
scale_color_viridis_c() +
geom_smooth(method = "lm", se = FALSE, show.legend = TRUE , aes(color = Species)) +
scale_color_discrete()

"Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale. geom_smooth() using formula 'y ~ x' Error: Continuous value supplied to discrete scale"

Any recommendations on how to manually assign a colour to each line (whilst leaving the scatter plot colours unchanged) would be very appreciated.

Many thanks in advance,

Rhys

0 Answers0