1

I'm trying to make a boxplot for some data I have and I've entered the following code:

rating_mean_plot <- data %>% 
  mutate(GROUP = recode(GROUP, "ontrol" = "control"), na.rm = T) %>% 
  ggplot(aes(x = GROUP, y = ITEM_2, ITEM_1, ITEM_3)) + 
  labs(x = "Condition", y = "Mean Attractiveness Rating") +
  stat_summary(fun.data = "mean_cl_boot", geom = "errorbar", width = .25) +
  geom_point(stat = "summary", fun.y = "mean", size = 4, shape = 20, color = "red", fill = "grey") +
  ylim(0,7) +
  theme_cowplot()
rating_mean_plot

However it has come up with the following errors

Duplicated aesthetics after name standardisation: Ignoring unknown parameters: fun.y

No summary function supplied, defaulting to `mean_se()
Removed 17 rows containing non-finite values (stat_summary)

any help on this would be greatly appreciated :)

Axeman
  • 32,068
  • 8
  • 81
  • 94
Tabs302
  • 23
  • 1
  • 4
  • Please provide a minimal reproducible example. See: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Axeman Mar 30 '20 at 18:36
  • Sorry. But to say it frankly: This code is the messiest ggplot code I've ever seen. (; And the reason for the warning messages, e.g. geom_point has no argument "fun.y". The "Duplicated aesthetics after name standardisation" results from the fact that your don't tell ggplot2 waht to do with ITEM_1 and ITEM_3. What kind of plot are your trying to achieve? From your code I would guess that you want groups on the x-axis, and for each group you want to have three errobars for your three items. Maybe you could clarify what kind of plot you are trying to achieve with your code. – stefan Mar 30 '20 at 19:09
  • @stefan Sorry about the messiness, I don't really know what I'm doing :), I'm trying to show the difference between two means. I have 2 groups which is the "condition" and I ran an experiment three times which is the "item_1,2,3 " I was using their overall mean of this to see if there was difference between the groups. The graph has to show the means of the 2 groups and their confidence intervals :) – Tabs302 Mar 30 '20 at 20:14

1 Answers1

0

If I got you right and using a toy dataset with some random numbers, the kind of plot could be achieved like so. First you have to tidy the dataset, i.e. convert from wide to long format which gives one column with the results of the three experiments instead of three different columns. This allows to compute the overall mean and confidence intervals by group.

library(ggplot2)
library(dplyr)
library(tidyr)
library(cowplot)

# example data with some random numbers
set.seed(42)

data <- data.frame(
  GROUP = c(rep("ontrol", 10), rep("group 2", 10)),
  ITEM_1 = runif(20) * 7,
  ITEM_2 = runif(20) * 7,
  ITEM_3 = runif(20) * 7,
  stringsAsFactors = FALSE
)

# Tidy the dataset: Convert from wide to long format
# item = which trial of the experiment
# value = result of the experiment
data_long <- data %>% 
  pivot_longer(-GROUP, names_to = "item", values_to = "value") %>% 
  mutate(GROUP = recode(GROUP, "ontrol" = "control"))

# Plot
rating_mean_plot <- data_long %>% 
  ggplot(aes(x = GROUP, y = value)) + 
  # Confidence Intervals as errorbars
  stat_summary(fun.data = "mean_cl_boot", geom = "errorbar", width = .25) +
  # Mean as points
  stat_summary(fun = "mean", geom = "point", size = 4, shape = 20, color = "red", fill = "grey") +
  labs(x = "Condition", y = "Mean Attractiveness Rating") +
  ylim(0,7) +
  theme_cowplot()
rating_mean_plot

Created on 2020-03-30 by the reprex package (v0.3.0)

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Oh wow thank you so much!!!!! That completely fixed my issues, honestly you helped me so much :) :) – Tabs302 Mar 30 '20 at 21:06