简体   繁体   中英

How to use columns as x-axis in RStudio

Here is my 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

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)) 

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM