0
df1 <-data.frame(Mean = as.numeric(c("45.71329", "38.162915")), Std = as.numeric(c("7.366905257", "9.597423074")), Samples=c("C", "T"))

df2 <- data.frame(Samples=c("C","C","C","C", "T", "T","T","T"), Values=c(38.37464, 40.84156, 49.50789,54.12907, 26.72173, 34.76061,42.14296,49.02636 ))

ggplot(df1, aes(Samples, Mean)) + 
 geom_col(color = "black", width = 0.4) + 
 scale_fill_grey() + 
 theme_classic() + 
 geom_errorbar(aes(ymin = Mean - Std, ymax = Mean + Std),color = "black",width = .1) + 
 ylim(0,60) + 
 geom_point(df2, mapping = aes(x=Samples, y= Values))

Hi everyone, I have some problems plotting this data. After the run of this code I obtain this barplot: enter image description here

I would need to color the points in C and T in the same way. The lower one in C with the same color as the lower one in T, the higher one in C with the same color as the higher one in T (the same for the other in the middle).

How can I do to color the points of my choice?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
marduk
  • 37
  • 5

1 Answers1

1

You can use this code:

library(tidyverse)
  
  df1 <-data.frame(Mean = as.numeric(c("45.71329", "38.162915")), Std = as.numeric(c("7.366905257", "9.597423074")), Samples=c("C", "T")) 
  
  df2 <- data.frame(Samples=c("C","C","C","C", "T", "T","T","T"), Values=c(38.37464, 40.84156, 49.50789,54.12907, 26.72173, 34.76061,42.14296,49.02636 ))
  
  df2 <- df2 %>%
    group_by(Samples) %>%
    mutate(points = ifelse(Samples == "C" & Values == min(Values), "lower",
                    ifelse(Samples == "C" & Values == max(Values), "higher",
                    ifelse(Samples == "C" & Values < max(Values) & Values > mean(Values), "middle high",
                    ifelse(Samples == "C" & Values > min(Values) & Values < mean(Values), "middle low",
                    ifelse(Samples == "T" & Values < max(Values) & Values > mean(Values), "middle high",
                    ifelse(Samples == "T" & Values> min(Values) & Values < mean(Values), "middle low",
                    ifelse(Samples == "T" & Values == max(Values), "higher",
                    ifelse(Samples == "T" & Values == min(Values), "lower", "")))))))))
  
  ggplot(df1, aes(Samples, Mean)) + 
    geom_col(color = "black", width = 0.4) + 
    scale_fill_grey() + 
    theme_classic() + 
    geom_errorbar(aes(ymin = Mean - Std, ymax = Mean + Std),color = "black",width = .1) + 
    ylim(0,60) + 
    geom_point(df2, mapping = aes(x=Samples, y= Values, color = points))

Output:

enter image description here

Quinten
  • 35,235
  • 5
  • 20
  • 53
  • I would like 4 different colors for the points. But equal colors between C and T. Okay for higher and lower but also the middle ones I would like them with different but equal colors between C and T. – marduk Mar 09 '22 at 18:26
  • @LucaCorda I just added code which classifies the data in 4 different colors. – Quinten Mar 09 '22 at 18:56