1

I have database of ratings with 16937 rows and 4 variables:

1.Subject
2.Distance_from_trap_F
3.type
4.rate

Here are the first 50 rows:

structure(list(Subject = c(110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L
), type = structure(c(1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 
1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L), levels = c("Affective Valence", 
"Semantic Valence"), class = "factor"), rate = c(25L, 14L, 8L, 
0L, 17L, 0L, 16L, 0L, 16L, 3L, 22L, 24L, 24L, 14L, 1L, 0L, 6L, 
22L, 21L, 18L, 13L, 19L, 21L, 5L, 12L, 1L, 22L, 20L, 24L, 0L, 
6L, 3L, 11L, 15L, 15L, 13L, 20L, 15L, 31L, 0L, 20L, 7L, 33L, 
15L, 28L, 28L, 12L, 7L, 0L, 13L), Distance_from_trap_F = structure(c(3L, 
4L, 1L, 1L, 3L, 1L, 2L, 1L, 2L, 1L, 4L, 4L, 2L, 2L, 1L, 1L, 2L, 
2L, 4L, 3L, 4L, 3L, 4L, 2L, 1L, 1L, 4L, 4L, 4L, 1L, 2L, 1L, 2L, 
2L, 3L, 3L, 4L, 3L, 4L, 1L, 3L, 2L, 4L, 3L, 4L, 4L, 2L, 2L, 1L, 
3L), levels = c("0", "1", "2", "3"), class = "factor")), row.names = c(NA, 
50L), class = "data.frame")

I built a graph in ggplot2 with facet_wrap, according to the type variable (two levels: Affective Valence/Semantic Valence). I wanted to add different text to the two parts of the graph.

I tried using:

Graph_data_text <- data.frame(label = c("good","n.s"),  
                        group = names(table(Mid_Maze$type)))

and then in the ggplot2:

geom_text(data=Graph_data_text,x=3.5, y=22, aes(label = label),family="sans",size=3,inherit.aes=FALSE)

The problem is I want "good" to appear in the left panel (Affective Valence) and "n.s" in the right panel (Semantic Valence) but currently both the "good" and the "n.s" appear in both panels.

The full code I used for the graph:

H2_Mid_graph <- Mid_Maze%>%group_by(Subject,Distance_from_trap_F,type )%>%dplyr::summarise(rate=mean(rate))%>% ggplot(aes(x=Distance_from_trap_F,y=rate,fill=Distance_from_trap_F))+facet_wrap(~type)+ geom_violin(trim=TRUE,position=position_dodge(width=0.75),width=1.0,show.legend = FALSE)+geom_text(data=Graph_data_text,x=3.5, y=22, aes(label = label),family="sans",size=3,inherit.aes=FALSE)
  • Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data via `dput()` so that we can run your code and figure out a solution for your issue. – stefan Jan 29 '23 at 21:00
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jan 29 '23 at 21:46
  • 1
    For one, you are missing the '+' after facet_wrap(~type) – stomper Jan 30 '23 at 00:44
  • OritHeimer, I'm closing this as "not reproducible or a typo" (for my vote, others exist) since I believe the error is entirely due to the missing `+` that stomper highlighted. If you fix that typo and it still is producing the same error with no other change, please @ping me and we can discuss options (reopening as needed). For that to happen, though, you'll need to [edit] your question, fix the typo, and add sample data to make this question reproducible so we can see the error in action. Thanks! – r2evans Jan 30 '23 at 01:21
  • 1
    Hello @r2evans, I fixed the issues needed. Thanks in advance, Orit – Orit Heimer Jan 30 '23 at 05:38
  • Still not reproducible. `Mid_maize` and `custom1` are not defined. I suspect `geom_text(x=3.5, y=22, aes(label = Graph_data_text$label), family="sans",size=3)` is problematic. Try `geom_text(data=Graph_data_text, x=3.5, y=22, aes(label = label), family="sans", size=3, inherit.aes=FALSE)`. Commenting out references to `custom1` confirms my suspicion. – Limey Jan 30 '23 at 08:38
  • Thanks, @Limey! Indeed the problem is with: geom_text(x=3.5, y=22, aes(label = Graph_data_text$label), family="sans",size=3). I tried your alternative code. The aes error note disappeared. However, the problem now is that both texts (i.e both the (*) and the "n.s") appear in the two panels (affective valence/semantic valence) of the graph. – Orit Heimer Jan 30 '23 at 09:01
  • Ok. So you are making progress. Now you need to provide a clear and unambiguous description of what text you want to appear where. And then provide a *minimal* reproducible example. Both minimal and reproducible are critical. You can test reproducibility by restarting your R session and then running your demo code, ensuring that it produces the output/error you describe in your text. Also, make sure that you don't reference any files on your local drives. – Limey Jan 30 '23 at 09:29
  • Minimal means that you don't include anything that isn't directly relevant to your problem. Here, you want different text to appear in different facets. So, for example, formatting your `geom_violon` and `geom_boxplot` are irrelevant and a waste of space. – Limey Jan 30 '23 at 09:30
  • Does [this](https://stackoverflow.com/questions/26371307/add-text-to-individual-facets-in-ggplot) answer your question? – Limey Jan 30 '23 at 09:31
  • @Limey, many thanks. I edited the question according to your advise. – Orit Heimer Jan 30 '23 at 10:42
  • Adding to @Limey. As you facet by `type` rename the `group` column to `type` in `Graph_data_text`. Otherwise the labels will be put in both panels. – stefan Jan 30 '23 at 11:09

0 Answers0