How to color only certain variables in ggplot

63 views Asked by At

I have this data

Score<-c(-2, 3, 4, -1, 3, 4, 5, -1, 3, 5, -3, 3, 5, 1, -4, 5, -2, 
         1, 3, 4, -4, 2, -1, 3, 4, -2, 3, 4, -1, 3, 4, 5, -1, 3, 5, -3, 3, 5, 1, -4, 5, -2, 
         1, 3, 4, -4, 2, -1, 3, 4)

Group<-c( "S", "S", "A", "S", "A", "S", "A", "S", "S", "A", "S", "A", "S", "A", 
          "S", "S", "A", "S", "A", "S", "A", "S", "S", "A", "S", "S", "S", "A", "S", "A", "S", "A", "S", "S", "A", "S", "A", "S", "A", 
          "S", "S", "A", "S", "A", "S", "A", "S", "S", "A", "S"
           )

Scenerio_ID <-c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 
                6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 
                6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)


CombinedTable<-data.frame(Score,Group, Scenerio_ID) 

and I have created the following plot where I have means and confidence intervals per group for each scenerio.

library(dplyr)
library(ggplot2)

CombinedTable %>%
  mutate(Scenerio_ID = as.factor(Scenerio_ID)) %>%
  group_by(Group, Scenerio_ID) %>%
  summarize(avg_Score = mean(Score),
            lci_Score = mean(Score) - 1.96 * sd(Score)/sqrt(n()),
            uci_Score = mean(Score) +  1.96 * sd(Score)/sqrt(n()),
            col = ifelse(avg_Score > 0, "U", "T")) %>%
  ggplot(aes(x = Group, y = avg_Score, color = ifelse(avg_Score > 0, "U", "T")))  +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lci_Score, ymax = uci_Score), position = "dodge") +
  labs(x = "A or S Per Scenerio", y = "Confidence Score", color = "") +
  facet_wrap(~ Scenerio_ID, nrow = 1L, strip.position = "bottom") +
  theme_classic() +
  scale_color_manual(values = c("U" = "blue", "T" = "red")) 

I am trying to color only the line responses, so for A=Blue and for S=Blue, and have no colour code for U or T.

This is my attempt and it is not working.

CombinedTable %>%
    mutate(Scenerio_ID = as.factor(Scenerio_ID)) %>%
    group_by(Group, Scenerio_ID) %>%
    summarize(avg_Score = mean(Score),
              lci_Score = mean(Score) - 1.96 * sd(Score)/sqrt(n()),
              uci_Score = mean(Score) +  1.96 * sd(Score)/sqrt(n()),
              col = Group) %>%
    ggplot(aes(x = Group, y = avg_Score, color = Group))  +
    geom_point(size = 2) +
    geom_errorbar(aes(ymin = lci_Score, ymax = uci_Score), position = "dodge") +
    labs(x = "A or S Per Scenerio", y = "Confidence Score", color = "") +
    facet_wrap(~ Scenerio_ID, nrow = 1L, strip.position = "bottom") +
    theme_classic() +
    scale_color_manual(values = c("S" = "blue", "A" = "red"))

I have attached an example of the plot and the code for the plot. I want to color the lines for each A and S for each scenerio so A is blue and S is Green.

CombinedTable %>%
  mutate(Scenerio_ID = as.factor(Scenerio_ID)) %>%
  group_by(Group, Scenerio_ID) %>%
  summarize(avg_Score = mean(Score),
            lci_Score = mean(Score) - 1.96 * sd(Score)/sqrt(n()),
            uci_Score = mean(Score) +  1.96 * sd(Score)/sqrt(n())) %>%
  ggplot(aes(x = Group, y = avg_Score))  +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lci_Score, ymax = uci_Score), position = "dodge") +
  labs(x = "A or S Per Scenerio", y = "Score", color = "") +
  facet_wrap(~ Scenerio_ID, nrow = 1L, strip.position = "bottom") +
  theme_classic()

enter image description here

1

There are 1 answers

3
M-- On

You need to add color = Group to your aes:

library(ggplot2)
library(dplyr)

CombinedTable %>%
  mutate(Scenerio_ID = as.factor(Scenerio_ID)) %>%
  group_by(Group, Scenerio_ID) %>%
  summarize(avg_Score = mean(Score),
            lci_Score = mean(Score) - 1.96 * sd(Score)/sqrt(n()),
            uci_Score = mean(Score) +  1.96 * sd(Score)/sqrt(n())) %>%
  ggplot(aes(x = Group, y = avg_Score, color = Group))  +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lci_Score, ymax = uci_Score), position = "dodge") +
  labs(x = "A or S Per Scenerio", y = "Score", color = "") +
  scale_color_manual(values = c("darkred", "green"), guide = "none") + 
  facet_wrap(~ Scenerio_ID, nrow = 1L, strip.position = "bottom") +
  theme_classic()