Changing Line Colors with Ggplot()

Changing line colors with ggplot()

color and fill are separate aesthetics. Since you want to modify the color you need to use the corresponding scale:

d + scale_color_manual(values=c("#CC6666", "#9999CC"))

is what you want.

R - ggplot line color (using geom_line) doesn't change

Your first code should be

ggplot(data=main_data) +
# black plot
geom_line(aes(x=vectors_growth_rate_with_predator,
y=disease_prevalnce_with_predator),
color = "black") +
# blue plot
geom_line(aes(x=vectors_growth_rate_with_predator,
y=disease_prevalnce_without_predator),
color = "blue")

You need to put color outside aes().

For your second code you need to reshape your data from wide to long format. You can do this in many ways, the following should work for you.

library(tidyverse)
main_data <- main_data %>%
gather(key, value, c("disease_prevalnce_with_predator",
"disease_prevalnce_without_predator")
PrevVSGrowth <- ggplot(data=main_data) +
geom_line(aes(x=vectors_growth_rate_with_predator,
y=value,
col = key))

PrevVSGrowth +
scale_color_manual(values = c(disease_prevalnce_with_predator= 'black',
disease_prevalnce_without_predator = 'blue'))

In the first plot we set an aesthetic to a fixed value, in each call to geom_line(). This creates two new variables containing only the value "black" and "blue", respectively. In OP's example the values "black" and "blue" are then scaled to red and lightblue and a legend is added.

In the second plot we map the colour aesthetic to a variable (key in this example). This is usually the preferred way.

change line color in ggplot

Covert your col column to a factor and then add scale_color_manual to your plot function.

library(ggplot2)
dftt$col<-as.factor(dftt$col)

ggplot(dftt, aes(x=x, y=values, group=group, color=col)) + geom_line(size=1.2) +
scale_color_manual(values=c( "black", "red", "blue"))

You may need to rearrange the color scale to match your choice of color (1,2 and 5)

How do I change the color of the regression lines in ggPlot?

One option to achieve your desired result would be to "duplicate" your threshold column with different values, e.g. in the code below I map 0 on 2 and 1 on 3. This duplicated column could then be mapped on the color aes inside geom_smooth and allows to set different colors for the regression lines.

My code below uses R or ggplot2 but TBMK the code could be easily adapted to plotnine:

n <- 1000
df <- data.frame(
relent = c(runif(n, 100, 200), runif(n, 150, 250)),
score = c(runif(n, 764, 766), runif(n, 766, 768)),
threshold = c(rep(0, n), rep(1, n))
)
df$threshold_sm <- c(rep(2, n), rep(3, n))

library(ggplot2)

p <- ggplot(data = df, mapping = aes(x = score, y = relent, color = factor(threshold))) +
scale_color_manual(values = c("darkorange", "purple", "blue", "green")) +
geom_vline(xintercept = 766, color = "red", size = 1, linetype = "dashed") +
labs(
y = "Yield",
x = "Score"
) +
theme_bw()

p +
geom_point() +
geom_smooth(aes(color = factor(threshold_sm)),
method = "lm",
formula = y ~ x + I(x**2), se = FALSE
)

Sample Image

A second option would be to add some transparency to the points so that the lines stand out more clearly and by the way deals with the overplotting of the points:

p +
geom_point(alpha = .3) +
geom_smooth(aes(color = factor(threshold)),
method = "lm",
formula = y ~ x + I(x**2), se = FALSE
) +
guides(color = guide_legend(override.aes = list(alpha = 1)))

Sample Image

Cannot change colors of lines in ggplot

The aes() function is for mapping variables to aesthetic attributes not for changing geoms' properties, you have to specify those outside aes() function, like this:

ggplot(data=SLLN, aes(x=X1, y=X2, group=1)) + 
geom_line(aes(colour = "Variable name A")) +
geom_hline(aes(yintercept=6, colour = "Variable name B"), linetype="dashed") +
scale_color_manual(values = c("black","blue")) +
... (the rest of your code)


Related Topics



Leave a reply



Submit