problems with line graph

Hello everyone, I hope you're well. I'm trying to make a line graph for my dissertation and after much effort, I've gotten really close. Unfortunately, I can't seem to fix it beyond this point - there are so many extra lines & points when I only need one each learning context per age group (so 2 lines, 4 data points).

I'll attach a screenshot below; if you could let me know how to fix this I'd really appreciate it!

ggplot(data = longdata, mapping = aes(x = age_group, y = score, group = learning_context)) +
  geom_line(size = 1, aes(color = learning_context)) +
  geom_point(size = 3, aes(color = learning_context)) +
  scale_color_manual(values=c("#D4D4D4", "#737373"), labels = c("No distractor", "Distractor")) +
  ylim(0,15) +
  labs(x = "Age Group", y = "Comprehension Score", color = "Learning Context") +
  scale_x_discrete(labels = c('Old','Young')) +
  theme_bw() +
  theme(panel.grid.major      = element_blank(),
        panel.grid.minor      = element_blank(),
        panel.background      = element_blank(),
        panel.border          = element_rect(colour = "black", fill=NA),
        axis.line             = element_blank(),
        axis.title.x          = element_text(size = 14, face = "bold"),
        axis.text.x           = element_text(size = 12, color = "black"),
        axis.title.y          = element_text(size = 14, face = "bold"),
        axis.text.y           = element_text(size = 12, color = "black"),
        legend.title          = element_text(size = 14, face = "bold"),
        legend.background     = element_blank(),
        legend.box.background = element_rect(colour = "black"),
        legend.text           = element_text(size = 12),
        legend.position       = "bottom",
        legend.direction      = "vertical"
  )

So, you want to plot the mean(?) score for each combination of Age Group and Learning Context? Something like

library(tidyverse)
longdata |> group_by(age_group, learning_context) |>
summarize(Avg = mean(score)) |>
gplot(mapping = aes(x = age_group, y = Avg, group = learning_context)) +
  geom_line(size = 1, aes(color = learning_context)) +
  geom_point(size = 3, aes(color = learning_context)) +
  scale_color_manual(values=c("#D4D4D4", "#737373"), labels = c("No distractor", "Distractor")) +
  ylim(0,15) + ...

If you could post some data, someone could use that to suggest some code that had been tested.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.