I have a dataframe df
with three columns: TASK
, CONDITION
, and SCORE
.
I want to represent the data:
- as barplots (I'm using
geom_col
) - with a separate plot for each
TASK
(I'm usingfacet_wrap(~TASK)
) - with a separate bar for each
CONDITION
(I'm usingggplot(df, aes(x=CONDITION))
)
Additionally, the expected behavior is that, if the data of a given bar sum up to a given percentage, then that bar should be the same color as other bars that reach the same percentage. Unfortunately, I can't get that to work.
In the minimal example below, 3 bars are reaching 100%, therefore I expect them to all be blue as per the instruction high="blue"
but this is not what is happening.
Input =("
TASK CONDITION SCORE
GAU 0 0.25
GAU 0 0.25
GAU 0 0.25
GAU 0 0.25
GAU 1 0.2
GAU 1 0.2
GAU 1 0.2
GAU 1 0.2
GAU 1 0.2
PLN 0 0.3333
PLN 0 0.3333
PLN 0 0
PLN 1 0.5
PLN 1 0.5
")
df <- read.table(textConnection(Input),
header=TRUE)
df$CONDITION <- factor(df$CONDITION)
library(ggplot2)
ggplot(df, aes(x=CONDITION, y=SCORE, fill=SCORE)) +
geom_col() +
ggtitle("Performance") +
ylab("Total") +
scale_y_continuous(labels = scales::percent) +
facet_wrap(~TASK) +
scale_fill_gradient(low="red", high="blue")