Home > Back-end >  how to set different color for each cluster
how to set different color for each cluster

Time:06-07

my data looks like this

df<- structure(list(Name = c("ScoreBIO", "ScoreBIO", "ScoreBIO"), 
    group = c("Class1", "Class2", "Class3"), median = c(18.46044132, 
    10.75826578, 0), SD = c(0.531335266, 15.21448537, 0)), class = "data.frame", row.names = c(NA, 
-3L))

I plot it like this

ggplot(df, aes(x = group, y = median)) 
  geom_point(size = 3)  
  geom_errorbar(aes(ymin = median - SD, ymax = median   SD), width = 0.2, colour = "gray25")

but it gives me all the same color as gray25

I tried to add this

scale_fill_manual(values = c("grey80", "grey30","grey50"))

or different color but still cannot get this to be different color with the legend position

theme(legend.position = "cornertop")

CodePudding user response:

Update: To get the same color in the geom_errorbar put the color aesthetics inside of aes:

library(ggplot)

ggplot(df, aes(x = group, y = median, color=group)) 
  geom_point(size = 3)  
  geom_errorbar(aes( color = df$group, ymin = median - SD, ymax = median   SD), width = 0.2) 
  scale_color_manual(values = c("grey80", "grey30","grey50"))

enter image description here

First answer:

library(ggplot2)

ggplot(df, aes(x = group, y = median, color=group)) 
  geom_point(size = 3)  
  geom_errorbar(aes(ymin = median - SD, ymax = median   SD), width = 0.2, colour = "gray25") 
  scale_color_manual(values = c("grey80", "grey30","grey50"))

enter image description here

  • Related