My code is a little different from the codes that people usually use when addressing this problem. I am trying to make some formatting adjustments to my code, including adding points to my line graph, and changing the color of the lines and error bars. Thanks in advance!
Reproducible sample:
set.seed(42)
n <- 100
dat <- data.frame(Participant=1:n,
Environment=rep(LETTERS[1:2], n/2),
Condition=rep(LETTERS[25:26], n/2),
Gate= sample(1:5, n, replace=TRUE),
Block = sample(1:2, n, replace=TRUE),
Sound=rep(LETTERS[3:4], n/2),
Correct=sample(0:1, n, replace=TRUE)
)
Code:
dat %>%
group_by(Condition, Gate = fct_inorder(Gate), Sound) %>% #these relevant columns
summarize(Accuracy = mean(Correct), #Defining y axis title
sd = sd(Correct)/sqrt(length(Correct))) %>% #and SD
ggplot(aes(x = Gate, y = Accuracy, color = Sound, group = Sound))
geom_line()
geom_errorbar(aes(ymin = Accuracy - sd, ymax = Accuracy sd,))
scale_y_continuous(labels = scales::percent)
facet_wrap(~Condition)
theme_minimal()
CodePudding user response:
Sounds straightforward; does this solve your problem?
library(tidyverse)
set.seed(42)
n <- 100
dat <- data.frame(Participant = 1:n,
Environment = rep(LETTERS[1:2], n/2),
Condition = rep(LETTERS[25:26], n/2),
Gate = sample(1:5, n, replace = TRUE),
Block = sample(1:2, n, replace = TRUE),
Sound = rep(LETTERS[3:4], n/2),
Correct = sample(0:1, n, replace = TRUE)
)
dat %>%
mutate(Gate = factor(Gate),
Sound = factor(Sound)) %>%
group_by(Condition, Gate, Sound) %>%
summarize(Accuracy = mean(Correct),
sd = sd(Correct)/sqrt(length(Correct))) %>%
ggplot(aes(x = Gate, y = Accuracy, color = Sound, group = Sound))
geom_line()
geom_errorbar(aes(ymin = Accuracy - sd, ymax = Accuracy sd,))
scale_y_continuous(labels = scales::percent)
facet_wrap(~Condition)
theme_minimal()
#> `summarise()` has grouped output by 'Condition', 'Gate'. You can override using the `.groups` argument.
# Change colour of lines
dat %>%
mutate(Gate = factor(Gate),
Sound = factor(Sound)) %>%
group_by(Condition, Gate, Sound) %>%
summarize(Accuracy = mean(Correct),
sd = sd(Correct)/sqrt(length(Correct))) %>%
ggplot(aes(x = Gate, y = Accuracy, color = Sound, group = Sound))
geom_line()
geom_errorbar(aes(ymin = Accuracy - sd, ymax = Accuracy sd,))
scale_y_continuous(labels = scales::percent)
facet_wrap(~Condition)
theme_minimal()
scale_color_brewer(palette = "Set1")
#> `summarise()` has grouped output by 'Condition', 'Gate'. You can override using the `.groups` argument.
# Add points
dat %>%
mutate(Gate = factor(Gate),
Sound = factor(Sound)) %>%
group_by(Condition, Gate, Sound) %>%
summarize(Accuracy = mean(Correct),
sd = sd(Correct)/sqrt(length(Correct))) %>%
ggplot(aes(x = Gate, y = Accuracy, color = Sound, group = Sound))
geom_line()
geom_errorbar(aes(ymin = Accuracy - sd, ymax = Accuracy sd))
geom_point()
scale_y_continuous(labels = scales::percent)
facet_wrap(~Condition)
theme_minimal()
scale_color_brewer(palette = "Set1")
#> `summarise()` has grouped output by 'Condition', 'Gate'. You can override using the `.groups` argument.
# different coloured lines, larger points
dat %>%
mutate(Gate = factor(Gate),
Sound = factor(Sound)) %>%
group_by(Condition, Gate, Sound) %>%
summarize(Accuracy = mean(Correct),
sd = sd(Correct)/sqrt(length(Correct))) %>%
ggplot(aes(x = Gate, y = Accuracy, color = Sound, group = Sound))
geom_line()
geom_errorbar(aes(ymin = Accuracy - sd, ymax = Accuracy sd), color = "grey75")
geom_point(size = 3)
scale_y_continuous(labels = scales::percent)
facet_wrap(~Condition)
theme_minimal()
scale_color_brewer(palette = "Set1")
#> `summarise()` has grouped output by 'Condition', 'Gate'. You can override using the `.groups` argument.
Created on 2022-02-09 by the reprex package (v2.0.1)
I highly recommend the books ggplot2: Elegant Graphics for Data Analysis and the R Graphics Cookbook for further information/examples (both free to read online)