dave
dave

Reputation: 157

R structuring data to allow side-by-side comparisons

In R, how do you compare, in a single graph, Scheduled Use and Actual Use?

I can display a bar graph of Scheduled Use, or a bar graph of Actual Use, but I can't figure out how to show a bar graph that shows Scheduled next to Actual

I have the following table of data.

Room ToD Scheduled Use Actual Use
A Morning 37.5 22.3
A Afternoon 27 15.3
A Evening 6.3 2
B Morning 18 24
B Afternoon 27 27
B Evening 6.5 12.3
C Morning 15.8 7.5
C Afternoon 20 10.3
C Evening 12.8 18
D Morning 10 7.5
D Afternoon 10 7.5
D Evening 18 12.3

This is how I plot the Scheduled Use, with an example of the plot.

# Read the csv data
x <- read.csv("C:/Users/David/Desktop/R-ggplot.csv")
View(x)

# Re-level to show on facet as: Morning, Afternoon, Evening
# insteat of: Afternoon, Evening, Morning
x$ToD <- factor(x$ToD, levels = c("Morning",
                                  "Afternoon",
                                  "Evening"))

# Plot the graph
y <- ggplot(data = x, aes(x = `Room`,
                          y = `Scheduled.Use`,
                          fill = `ToD`)) +
    geom_bar(position='dodge', stat='identity') +
    facet_wrap(~`ToD`, nrow = 3) +
    ggtitle("Room Scheduled Use v. Actual Use") +
    ylab("Scheduled v. Acutual")
plot(y)

enter image description here

Upvotes: 0

Views: 122

Answers (1)

IceCreamToucan
IceCreamToucan

Reputation: 28705

ggplot expects it to be in "long" format, so you need to pivot_longer first. When you pivot to longer, the name of the columns being pivoted becomes a new column called "name", and the value of the column becomes a column called "value" (look at the output for just the first part of the pipe chain right after pivot_longer to see what I mean). If you want the columns in a different order, you need to convert "name" to factor and specify the levels in the order you want.

x <- read.delim(text = '
Room    ToD Scheduled.Use   Actual.Use
A   Morning 37.5    22.3
A   Afternoon   27  15.3
A   Evening 6.3 2
B   Morning 18  24
B   Afternoon   27  27
B   Evening 6.5 12.3
C   Morning 15.8    7.5
C   Afternoon   20  10.3
C   Evening 12.8    18
D   Morning 10  7.5
D   Afternoon   10  7.5
D   Evening 18  12.3
')

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)

sched_act_plot <- 
  x %>% 
    mutate(across(ToD, factor, levels = c("Morning",
                                          "Afternoon",
                                          "Evening"))) %>% 
    pivot_longer(c(Scheduled.Use,   Actual.Use)) %>% 
    mutate(across(name, factor, levels = c('Scheduled.Use', 
                                            'Actual.Use'))) %>% 
    ggplot(aes(x = Room, y = value, fill = name)) +
      geom_bar(position = 'dodge', stat = 'identity') +
      facet_wrap(~ToD, nrow = 3) +
      ggtitle("Room Scheduled Use v. Actual Use") +
      ylab("Scheduled v. Acutual")

plot(sched_act_plot)

Created on 2021-05-16 by the reprex package (v2.0.0)

Upvotes: 1

Related Questions