Paillou
Paillou

Reputation: 839

Generate histogram from boxplots in R

I have this dataframe :

> my_data
       Dimensions  Mean    SE Lower Upper
1       Cognitive 2.617 0.148 2.318 2.916
2       Affective 2.417 0.128 2.157 2.676
3 Comportementale 2.017 0.139 1.736 2.297

I would like to generate an histogram plot with (x=Dimensions, y=Mean and with upper and lower limits the "Lower" value and the "Upper" value). For the moment, I only have a "point graph" , but I would like to set up 3 bars with the peak of the bar as the point value.

I used ggplot2,

ggplot(my_data,aes(x=Dimensions, y=Mean)) + geom_point() + ylim(0,4) + geom_errorbar(aes(ymin=Lower-SE, ymax=Upper+SE),width=.2)

Upvotes: 0

Views: 61

Answers (1)

DaveArmstrong
DaveArmstrong

Reputation: 22044

Is this what you're looking for?

library(ggplot2)
my_data <- tibble::tribble(
  ~Dimensions,  ~Mean,    ~SE, ~Lower, ~Upper,
"Cognitive", 2.617, 0.148, 2.318, 2.916,
"Affective", 2.417, 0.128, 2.157, 2.676,
"Comportementale", 2.017, 0.139, 1.736, 2.297)

my_data$Dimensions <- factor(my_data$Dimensions, 
                             levels=c("Cognitive", "Affective", "Comportementale"))

ggplot(my_data, aes(x=Dimensions, y=Mean, ymin=Lower, ymax=Upper)) + 
  geom_bar(stat="identity", fill="gray75") + 
  geom_errorbar(width=.15) + 
  theme_classic()

Created on 2022-07-07 by the reprex package (v2.0.1)

Upvotes: 1

Related Questions