0

I've just conducted a simple validation experiment whereby participants rated the pleasantness of photos of different scenes.

I'm currently building a bar chart in ggplot which has a pleasantness rating as the y variable, and the specific scene type (e.g. park, beach, rubbish tip, derelict building, etc) as the x variable. However, I've also categorised each sceneType as either "positive" or "negative" within the variable sceneValence. Both of these variables are in my sceneTable.

So far, I've managed to produce a relatively decent bar graph which presents all of the different sceneTypes along the x-axis, and colours them according to their sceneValence. See the code below:

sceneGraph <- ggplot(sceneTable, aes(y = Response, x = sceneType, fill = sceneValence)) + 
geom_bar(stat = "identity", position = "dodge", width = 0.6, alpha = 0.6) 
+ geom_errorbar(aes(ymin = Response-se, ymax = Response+se), position = position_dodge(width=0.6), 
          width = .1, size = 0.5) + labs(title = "Pleasantness Rating by Scene Type",
 x = "Scene Type", y = "Pleasantness Rating", fill = "Scene Valence")

However, what I would really love to do is to reorder the bars in the graph so that all of the "negative" sceneTypes are on one side of the x-axis (in one colour), and all of the "positive" sceneTypes are on the other side of the x-axis (in a different colour).

At the moment, all of my sceneTypes are completely intermixed along the x-axis (in alphabetical order), irrespective of their valence, and can only be distinguished based upon their colour. I would also like to separate sceneType according to sceneValence, which allows for easier within-valence comparison. Is this possible?

Graph of Output

Data Table

camille
  • 16,432
  • 18
  • 38
  • 60
Samedi
  • 23
  • 4
  • I'm sure a question like this has been posted before, but without being able to run your code (no data) or see your output, it's hard to know how to help exactly – camille Feb 11 '22 at 16:43
  • Hello Camille, thank you for your response! I would love to post my output, but I don't yet have the privileges to post photos because I have only just joined Stack Overflow – Samedi Feb 11 '22 at 16:46
  • You can upload an image and someone else can put it inline for you, and you can add data based on the [guidance](https://stackoverflow.com/q/5963269/5325862) on reproducible examples also linked to at the top of the [tag:r] tag – camille Feb 11 '22 at 16:49
  • Thank you for the advice! I apologise about my naivety, but how would I upload my image without putting it inline? – Samedi Feb 11 '22 at 16:53
  • Look back at the [help center](https://stackoverflow.com/help/formatting), [edit] the question, and click the image button – camille Feb 11 '22 at 16:55
  • You essentially need set the order of sceneType as a factor. See the R Graph Cookbook (https://r-graphics.org/recipe-dataprep-factor-reorder-value) – Marcus Feb 11 '22 at 17:02
  • Thank you so much camille! I've just added images of my output graph and source data table. Unfortunately, my graph isn't expanding to fill the full window when I open it, so the x-axis labels aren't readable. I thought that was a separate problem which I didn't want to conflate with this. Nevertheless, any advice about that issue would also be appreciated! – Samedi Feb 11 '22 at 17:04
  • And thank you Marcus! I thought I might simply have to reorder my sceneType variable as a factor, but I hoped there might be a way to order the bars according to a higher variable like sceneValence. I guess there isn't, so I will use your solution! – Samedi Feb 11 '22 at 17:07
  • see e.g. `forcats::fct_reorder`. – Axeman Feb 11 '22 at 17:10
  • Hi Axeman, I've spent the last 20 minutes reading the fct_reorder documentation, but it seems quite unclear, and I'm not sure how I'd apply it to this situation. Any extra help would be hugely appreciated? – Samedi Feb 11 '22 at 17:36
  • The guidance I linked you to has several examples of how to include data that we can work with, which we can't do with images. Your screenshot also doesn't have some of the variables you're asking about (e.g. sceneType and sceneValence). Maybe you just want to facet the plot – camille Feb 11 '22 at 18:11

0 Answers0