No color legend in ggplot

Hello,
I am attempting (unsuccessfully) to show the legend for colors in my R ggplot.
Here is my code:

ggplot(figctldata, aes( x = as.factor(Ecdysone), y = Disc_Size, color = as.factor(Age))) +
plot_format +
scale_color_manual(values = c("24" = "black", "36" = "#4575b4"), name = "Age (HAL3E)") +
labs(x = "Ecdysone Concentration (ng/ml)",
y = "Disc Area (µm²)",
color = "Age (in HAL3E)") +
geom_violin() +
geom_point(position = position_dodge(width = 0.9), size = 2, alpha = 0.5) +
scale_x_discrete(labels = c("0" = "Control", "5" = "5", "10" = "10", "25" = "25", "50" = "50", "100" = "100", "200" = "200"))

I am very confused as it works for a different plot...

Thank you!

If I invent some data and remove the code plot_format +, your code works for me. I limited my invented data to three values of Ecdysone. Is plot_format and object you made?

It would be more convenient in the future if you provided some data to plot.

library(ggplot2)
figctldata <- data.frame(Ecdysone = rep(c(0, 5, 10), 30),
                         Disc_Size = rnorm(90),
                         Age = rep(c(24,36), each = 45))
ggplot(figctldata, aes( x = as.factor(Ecdysone), y = Disc_Size, color = as.factor(Age))) +
  #plot_format +
  scale_color_manual(values = c("24" = "black", "36" = "#4575b4"), name = "Age (HAL3E)") +
  labs(x = "Ecdysone Concentration (ng/ml)",
       y = "Disc Area (µm²)",
       color = "Age (in HAL3E)") +
  geom_violin() +
  geom_point(position = position_dodge(width = 0.9), size = 2, alpha = 0.5) +
  scale_x_discrete(labels = c("0" = "Control", "5" = "5", "10" = "10", "25" = "25", "50" = "50", "100" = "100", "200" = "200"))

Created on 2024-02-18 with reprex v2.0.2

It works now, thank you!

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.