简体   繁体   中英

scale_fill_discrete with breaks and custom color scale?

I have an issue choosing the custom colouring of my plot, I am trying to add my own values to the plot but I can's seem to understand where should I put the custom color vector:

col <- c("#004d8d", "#cc2701", "#e5b400")

This is the plot code right now:

p1 <- ggplot(data = densdf1, mapping = aes(x = x, y = y)) +
  geom_area(data = densdf1[densdf1$CI,], 
            aes(fill = Electrode, color = Electrode),
            outline.type = "full", alpha = 0.3, size = 1) +
  geom_line(aes(color = Electrode), size = 1) +
  scale_fill_discrete(breaks=c("Fz","Cz","Pz")) +
  guides(colour = "none") +
  geom_vline(xintercept = 0) +
  lims(x = c(-3, 2), y = c(0, 2.25)) +
  labs(title="INTERVAL 225-275ms", x="VALUES", y="DENSITY") +
  theme_bw() +   
  theme(axis.text=element_text(size=10),
        axis.title=element_text(size=12),
        plot.title=element_text(size=14))

This is the plot with default colours and it looks perfectly fine, but I'd like to customise the colours.

使用默认颜色绘图

As you can see there is a contour line and the area from the density that need to be coloured.

I tried changing the aesthetics but I think I am not understanding the logic. I tried also using scale_manual_fill(values = col) removing scale_fill_discrete(breaks=c("Fz","Cz","Pz")) but it only works with the area inside.

Can any ggplot2 expert give me a hint? Thanks!

in the aes(...) section you define scales, and the color must be adjusted for each scale. In your case it would be the "fill" and the "color" scale, hence the following two lines must be added to adjust both scales:

scale_color_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +
scale_fill_manual(values = col, breaks = c("Fz", "Cz", "Pz")) +

If you want to make sure, that a certain category gets a certain color, you have to specify the breaks, and the order the vector corresponds to the order of the color vector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM