简体   繁体   中英

scale_color_manual not working properly with geom_pointrange ggplot2

scale_color_manual when given colors do not generate the output expected. Minimum reproducible code is provided below:

statsSomeSet <- read.table(header=TRUE, text="
 methods    min max mean
<fct>   <dbl>   <dbl>   <dbl>
    Method1_LR  40  80  60
    Method2_LR  50  90  70
    Method3_LR  30  70  50
    Method1_RF  50  90  70
    Method2_RF  40  80  60
    Method3_RF  30  70  50
")

statsSomeSet$methods <- factor(statsSomeSet$methods, levels = statsSomeSet$methods)
p <- ggplot(data = statsSomeSet, mapping = aes(x = methods, y = mean)) +
    geom_pointrange(mapping = aes(ymin = min, ymax = max)) + 
    scale_color_manual(values = c("green", "red", "blue","green", "red", "blue" ))
p + labs(title = "Methods")

Building on @stefan's comment you can use the separate function from tidyr to split your methods column into two columns and then map the first to the color aesthetic.

library(tidyverse)

statsSomeSet <- read.table(header=TRUE, text="
 methods    min max mean
    Method1_LR  40  80  60
    Method2_LR  50  90  70
    Method3_LR  30  70  50
    Method1_RF  50  90  70
    Method2_RF  40  80  60
    Method3_RF  30  70  50
")

statsSomeSet_separated <- statsSomeSet %>%
  separate(methods, c('method_a', 'method_b'), sep="_")

p <- ggplot(data = statsSomeSet_separated, mapping = aes(x = paste(method_a, method_b), y = mean, color = method_a)) +
  geom_pointrange(mapping = aes(ymin = min, ymax = max))
p + labs(x = 'Methods', title = "Methods")

在此处输入图像描述

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