Home > Software design >  Bar Plot with 3 independent variables in one plot
Bar Plot with 3 independent variables in one plot

Time:01-25

My df looks like this:

Training_Task <- c("Imag", "Imag", "Imag", "Imag", "Rephrasing", "Rephrasing", "Rephrasing", "Rephrasing")
emotionality <- c("emo", "neu", "emo", "neu", "emo", "neu", "emo", "neu")
target_type <- c("novel", "novel", "pseudo", "pseudo", "novel", "novel", "pseudo", "pseudo")
acc <- c(93, 95, 97, 98, 96, 94, 92, 90)
df <- data.frame(Training_Task, emotionality, target_type, acc)

Now I would like to create a bar plot that looks somehow like this:

plot = ggplot(df, aes(fill =Training_Task, y=acc, x=target_type))

But I would like the IV emotionality to be added by emotional bars (emotionality = emo) being saturated and neutral bars (emotionality = neu) being pale.

Is there a way to do that?

CodePudding user response:

You can use alpha in your aesthetic and set position to dodge:

ggplot(df)  
  geom_bar(aes(fill = Training_Task, y=acc, x=target_type, alpha = emotionality), stat = 'identity', position = "dodge")  
  theme_minimal()  
  scale_alpha_discrete(range = c(1,0.4))

enter image description here

  •  Tags:  
  • Related