Let's say I have 2 subjects. Each did experimental trials and control trials where the outcome for both sets of trials is either 1 or 0.
I want a stacked bar plot that shows the proportion of 1 responses and 0 responses for both types of trials for each subject.
So each subject would have two bars that both reach up to a proportion of 1.00 on the y axis.
Mock data
df1 <- data.frame(Sbj_ID = c(1),
Trial_Type = c("Testing", "Testing", "Control", "Control"),
Outcome = c(1, 0, 1, 0),
Proportion = c(0.41, 0.59, 0.02, 0.98))
df2 <- data.frame(Sbj_ID = c(2),
Trial_Type = c("Testing", "Testing", "Control", "Control"),
Outcome = c(1, 0, 1, 0),
Proportion = c(0.30, 0.70, 0.10, 0.90))
df <- merge(df1, df2, all=TRUE)
I want it to look like this type of figure, but with stacked bars
data(iris)
library(ggplot2)
library(tidyr)
iris %>%
gather("Type", "Value",-Species) %>%
ggplot(aes(Species, Value, fill = Type)) +
geom_bar(position = "dodge", stat = "identity")
Here are my failed attempts...
library(ggplot2)
library(tidyr)
# fail 1
df $>$
gather("Outcome", "Proportion", -Sbj_ID) %>%
ggplot(aes(Sbj_ID, Proporiton, fill = Outcome)) +
geom_bar(stat = "identity", position = "dodge")
# fail 2
ggplot(df, aes( x = factor(Sbj_ID), y = Proportion, fill = Outcome)) +
geom_bar(stat = "identity", position = "stack")
Any ideas? Thank you in advance!