scale_manual question in ggplot2

fcyl <- factor(mtcars$cyl)
fam <- factor(mtcars$am)

palette <- c(automatic = "#377EB8", manual = "#E41A1C")

ggplot(mtcars, aes(fcyl, fill = fam)) +
  geom_bar() +
  labs(x = "Number of Cylinders", y = "Count") +
  # Set the fill color scale
  scale_fill_manual("Transmission", values = palette)

I don't understand the last line. What is Transmission? What is values = palette?

fcyl <- factor(mtcars$cyl)
fam <- factor(mtcars$am,
              levels = c(0,1),
              labels =c("automatic","manual"))

palette <- c(automatic = "#377EB8", manual = "#E41A1C")

ggplot(mapping = aes(fcyl, fill = fam)) +
  geom_bar() +
  labs(x = "Number of Cylinders", y = "Count") +
  # Set the fill color scale
  scale_fill_manual("Transmission is a legend title", values = palette)

1 Like

Have a look at https://ggplot2.tidyverse.org/reference/scale_manual.html

For some reason I cannot get that code to run but for the last line,

"Transmission" is the title for the index. values specifies the plotting code that has been defined above in "palette".

library(ggplot2)

Try:
p <- ggplot(mtcars, aes(mpg, wt)) +
geom_point(aes(colour = factor(cyl)))
p + scale_colour_manual("Cylinder", values = c("red", "blue", "green"))

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