1

Here is my data:

Data

How do I make it so that the column names appear on the x axis? I will probably use the facet function so that the number values aren't next to the duration values, so one graph will have these on the x axis: "Number Looks", "Number Gesture", "Number Reach", "Number Other" for group A, and another graph will have these on the x axis: "Duration Looks", "Duration Gesture", "Duration Reach", "Duration Other" for group A, with the data below the column titles as the y-axis values. I will also have to generate the data for group B in the same way

Phil
  • 7,287
  • 3
  • 36
  • 66
Ryan Bruno
  • 321
  • 1
  • 8
  • Please have a look here and provide a reproducible example, which makes it much easier to help you! https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – coffeinjunky Aug 05 '21 at 20:34
  • Do you want to have unique graphs for each participant? Try to give your data in your Q, so we can use it to make an answer. – Bloxx Aug 05 '21 at 20:49

1 Answers1

0

Here is how we could achieve your task:

  1. Bring your data in the correct format with pivot_longer
  2. Use filter for each number and Duration
  3. Now you have to separate dataframes
  4. plot them individually with ggplot2 using facet_wrap for group A and B
  5. The output arranged with plot_grid from cowplot package!
library(cowplot)
library(tidyverse)

df_number <- df %>% 
    pivot_longer(
        cols = 3:12,
        names_to = "names",
        values_to = "values"
    ) %>% 
    filter(grepl('Number', names))

df_Duration <- df %>% 
    pivot_longer(
        cols = 3:12,
        names_to = "names",
        values_to = "values"
    ) %>% 
    filter(grepl('Duration', names))

plot_number <- ggplot(df_number, aes(x=factor(names), y=values)) +
    geom_bar(stat = "identity") + 
    xlab("Number") + 
    ylab("Value") +
    facet_wrap(~Group) +
    theme_bw()

plot_Duration <- ggplot(df_Duration, aes(x=factor(names), y=values)) +
    geom_bar(stat = "identity") + 
    xlab("Duration") + 
    ylab("Value") +
    facet_wrap(~Group) +
    theme_bw()


plot_grid(plot_number, plot_Duration, labels = "AUTO")

data:

df <- structure(list(Participant = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17), Group = c("A", "A", "A", "A", "A", 
"A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B"), 
Number_Looks = c(47, 94, 23, 64, 99, 38, 85, 38, 20, 10, 
34, 54, 87, 78, 45, 63, 32), Duration_Look = c(247, 294, 
223, 264, 299, 238, 285, 238, 220, 210, 234, 254, 287, 278, 
245, 263, 232), Number_Gesture = c(39, 86, 15, 56, 91, 30, 
77, 30, 12, 20, 26, 46, 79, 70, 37, 55, 24), Duration_Gesture = c(29, 
76, 5, 46, 81, 20, 67, 20, 20, 10, 16, 36, 69, 60, 27, 45, 
14), Number_Reach = c(40, 87, 16, 57, 92, 31, 78, 31, 13, 
21, 27, 47, 80, 71, 38, 56, 25), Duration_Reach = c(89, 136, 
65, 106, 141, 80, 127, 80, 80, 70, 76, 96, 129, 120, 87, 
105, 74), Number_Other = c(52, 99, 28, 69, 104, 43, 90, 43, 
25, 33, 39, 59, 92, 83, 50, 68, 37), Duration_Other = c(339, 
386, 315, 356, 391, 330, 377, 330, 330, 320, 326, 346, 379, 
370, 337, 355, 324), Number_Sound = c(152, 199, 128, 169, 
204, 143, 190, 143, 125, 133, 139, 159, 192, 183, 150, 168, 
137), Duration_Sound = c(319, 366, 295, 336, 371, 310, 357, 
310, 310, 300, 306, 326, 359, 350, 317, 335, 304)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -17L)) 

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66