A bar plot depicting the differences of answers between 2 surveys from 1-5
I'm new to programming in R studio and am struggling with making a graph for a project. I've attached my current code and a photo of the graph I am trying to produce.
The data in my excel table gives me one count of option "1", in the Q1 initial survey, and 0 counts of option "1" for the follow up survey but for some reason, the graph is grey for Q1 for both sections. Not sure how I can fix this to get rid of the second grey bar and leave that area empty to indicate the lack of "1" votes for the follow up survey. How do i fix this?
library(readxl)
library(tidyverse)
DATA <- read_excel("C:/Users/dsm/OneDrive/Desktop/Poole Research/DATA.xlsx", sheet = "Sheet2")
Newpractice <- dplyr::filter(DATA, Type=="Q1 Follow Up" | Type=="Q1 Initial")
Newpractice %>%
ggplot(aes(x = Score, y = after_stat(count/sum(count)), fill = Type))
geom_bar(position = "dodge")
scale_fill_grey()
ylab("Frequency")
Newpractice <-
structure(list(
Type = c("Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial", "Q1 Initial",
"Q1 Initial", "Q1 Initial", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up",
"Q1 Follow Up", "Q1 Follow Up", "Q1 Follow Up", "Q1 Follow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, 5, 3, 5, 3, 3, 3, 4, 3, 3, 5, 4, 3, 3, 4, 3, 5, 4, 3,
5, 5, 3, 3, 4, 3, 3, 5, 3, 5, 3, 3, 5, 5, 3, 3, 3, 3, 3, 3, 5, 3,
3, 3, 4, 3, 4, 5, 3, 5, 3, 3, 3, 5, 3, 3, 5, 3, 3, 3, 5, 3, 5, 5,
4, 5, 3, 5, 5, 3, 2, 3, 3, 3, 3, 3, 4, 3, 3, 5, 4, 3, 3, 5, 3, 5,
3, 5, 3, 5, 3, 3, 3, 3, 5, 5, 4, 5, 4, 5, 3, 5, 5, 3, 5, 3, 3, 3,
5, 3, 3, 5, 3, 4, 5, 5, 2, 4, 4, 3)),
row.names = c(NA, -382L ), class = c("tbl_df", "tbl", "data.frame"))
CodePudding user response:
Here is a way.
The frequencies computation is done in the pipe so that the Type
is completed with the missing levels, filling them with zeros (only one but it would work for any number of missing levels).
I also use geom_col
, not geom_bar
, since the values to be plotted are already known.
suppressPackageStartupMessages({
library(ggplot2)
library(dplyr)
library(tidyr) # complete() and nesting()
})
Newpractice %>%
mutate(Type = as.factor(Type),
Score = as.factor(Score)) %>%
count(Score, Type, name = "Frequency") %>%
mutate(Frequency = Frequency/sum(Frequency)) %>%
complete(Type, nesting(Score), fill = list(Frequency = 0)) %>%
ggplot(aes(x = Score, y = Frequency, fill = Type))
geom_col(position = position_dodge())
scale_fill_grey(drop = FALSE)
scale_x_discrete(drop = FALSE)
Created on 2022-09-23 with reprex v2.0.2