0

I did my analysis with a bootstrap approach. Now I would like to plot the "significance" in a box plot. But I get all the time the same error message: Error: Aesthetics must be either length 1 or the same as the data (18): colour Can someone help me? DataPlot_7 is an example data frame:

df1 <- data.frame(a = c(1, 1:2,2), b = c(60, 61, 61, 60))

DataPlot_7 <- data.frame(DeltaFace = c(48.58, 0.70, -1.54, 1.29, 64.29, 12.00, 60.87, 4.83), Manipulation = as.factor(c("positive", "neutral", "negative","negative","positive", "positive", "neutral","negativ")), Condition = as.factor(c("1", "2", "1", "2", "2", "1", "2", "1")))


Plot.Mean.Diff.Face.Manipulition_4 <- ggplot(data = DataPlot_7, aes(x = DataPlot_7$Condition, y = DataPlot_7$DeltaFace, color=DataPlot_7$Condition)) +
geom_boxplot(alpha = 0.6,outlier.colour = "#1F3552", outlier.shape = 20, width = 30)+
  facet_wrap(~Manipulation, ncol = 2)+
  scale_x_discrete(name = "X" ) +
  scale_y_continuous(name = "Y", breaks = seq(-100, 100, 25), limits=c(-100, 100)) + 
  ggtitle("title") +
  theme_set(theme_apa())+ 
  theme(plot.caption = element_text(hjust = 0.2),
    axis.title.x=element_blank(),
    axis.text.x=element_blank())+   
  scale_fill_brewer(palette = "Accent") + 
theme(legend.position="right")+ 
geom_line(data = df1, aes(x = a, y = b)) + annotate("text", x = 2.5, y = 62, label = "*", size = 8)


Plot1 <- Plot.Mean.Diff.Face.Manipulition_4

The goal would be to get some horizontal lines from condition to condition and if it was significant, that I could manually set a star on the line

I found this approach on StackOverflow for excample:

pp <- ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot()
df1 <- data.frame(a = c(1, 1:3,3), b = c(39, 40, 40, 40, 39))
pp + geom_line(data = df1, aes(x = a, y = b)) + annotate("text", x = 2, y = 42, label = "*", size = 8) 

But every time I'm trying to change it to my dataset I get the described Error message.

severin
  • 111
  • 1
  • 1
  • 6
  • Is `DataPlot_4` really `df1`? – r2evans Jul 26 '19 at 22:05
  • no, those are tow different data frames. In DataPlot_4 are all the experimental data. df1 is a data frame for the area where the line should be in the plot. Should it be the same data frame? I really don't get it how I can draw in the boxplot. And furthermore all I do get in the polt appears in all the subplots at the same time, but it should be only in one of them. – severin Jul 26 '19 at 22:14
  • 2
    Then could you spend a moment to make your question more *reproducible*?: this includes sample data for everything you use in the question. Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Jul 26 '19 at 22:18
  • 2
    [Don't put `$` inside `aes`](https://stackoverflow.com/questions/32543340/issue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio) – camille Jul 26 '19 at 23:55
  • @camille the $ was an attempt to specify two data frames in one ggpolt. If I remove it the Error message is: Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = c(2L, 3L, 2L, 3L, : Replacement has 381 lines, data has 5... Thanks for your help – severin Jul 27 '19 at 07:48
  • 1
    Each layer can have its own data supplied to its data argument – camille Jul 27 '19 at 21:13
  • If you are using two data frames in a single layer, then join or `cbind` them into one data frame. If you are using data frames in different layers, you can use the `data` argument. Using `$` inside `aes()` will work for the simplest of plots, but will break if there are more complex features like facets, calculated stats, bins, etc. – Gregor Thomas Jul 30 '19 at 13:38

1 Answers1

0

When you set aesthetics in an initial ggplot() call, they will be inherited by subsequent layers by default. In this case, your color = Condition is inherited by the geom_line layer, but that uses a data frame without a Condition column. The solution is to set inherit.aes = FALSE in the geom_line layer.

I've made that change, removed $ from inside aes(), formatted code, removed a few theme options that didn't seem relevant and required extra packages. I think that fixes everything.

ggplot(data = DataPlot_7, aes(x = Condition, y = DeltaFace, color = Condition)) +
  geom_boxplot(
    alpha = 0.6,
    outlier.colour = "#1F3552",
    outlier.shape = 20,
    width = 30
  ) +
  facet_wrap( ~ Manipulation, ncol = 2) +
  scale_x_discrete(name = "X") +
  scale_y_continuous(
    name = "Y",
    breaks = seq(-100, 100, 25),
    limits = c(-100, 100)
  ) +
  theme(
    plot.caption = element_text(hjust = 0.2),
    axis.title.x = element_blank(),
    axis.text.x = element_blank()
  ) +
  scale_fill_brewer(palette = "Accent") +
  geom_line(data = df1, aes(x = a, y = b), inherit.aes = FALSE) +
  annotate(
    "text",
    x = 2.5,
    y = 62,
    label = "*",
    size = 8
  )

enter image description here

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294