How do I create a bar graph in R that builds the bars around 0.5 instead of 0 on the y-axis?

77 Views Asked by At

Let's say I have the following mean values: In my research I assigned 0 and 1 values to no and yes responses so below are the means given a 0 to 1 scale.

| Item | Mean |
| a    | 0.66 |
| b    | 0.37 |
| c    | 0.36 |
| d    | 0.12 |
| e    | 0.42 |
| f    | 0.68 |
| g    | 0.19 |
| h    | 0.27 |
| i    | 0.11 |
| j    | 0.37 |

How do I create a bar graph in R where you will see the bars centered around 0.5? For means > 0.5 (e.g., item a and f), the bar is above 0.5, and, for means < 0.5, the bar is below 0.5.

I tried googling code for R but I was unsuccessful at finding what I wanted. It is possible I was using the wrong key words. I am not an expert in R so I find everything R related challenging. I don't even know how to achieve what I want given my current R experience.

3

There are 3 best solutions below

4
TarJae On BEST ANSWER

Update: (see comments):

library(ggplot)

df$Deviation = df$Mean - 0.5

ggplot(df, aes(x = Item, y = Deviation)) +
  geom_col(aes(fill = Deviation > 0), position = position_dodge()) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
  scale_fill_manual(values = c("TRUE" = "cadetblue3", "FALSE" = "coral3")) +
  theme_minimal() +
  scale_y_continuous(limits = c(-0.5, 0.5),  
                     breaks = seq(-0.5, 0.5, by = 0.1),
                     labels = function(x) sprintf("%.1f", x + 0.5)) +
  theme(legend.position = "bottom")

enter image description here

Update after clarification (removed first answer): Just remove coor_flip():

library(ggplot2) 

df$Deviation = df$Mean - 0.5

ggplot(df, aes(x = Item, y = Deviation)) +
  geom_col(aes(fill = Deviation > 0), position = position_dodge()) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "black") +
  scale_fill_manual(values = c("TRUE" = "cadetblue3", "FALSE" = "coral3")) +
  theme_minimal()+
  scale_y_continuous(labels = function(x) x + 0.5, 
                     breaks = function(x) seq(floor(min(df$Mean)),  
                                              ceiling(max(df$Mean)), by = 0.1)) +
  theme(legend.position = "bottom")

enter image description here

0
jay.sf On

You could subtract .5. Not sure how you want the y-axis labels look like.

> barplot(dat$Mean - .5, names.arg=dat$Item)

enter image description here


Data:

> dput(dat)
structure(list(Item = c("a", "b", "c", "d", "e", "f", "g", "h", 
"i", "j"), Mean = c(0.66, 0.37, 0.36, 0.12, 0.42, 0.68, 0.19, 
0.27, 0.11, 0.37)), class = "data.frame", row.names = c(NA, -10L
))
0
MetehanGungor On

I know this isn't the answer you're looking for. But, I feel that you are working on something related to the item difficulty index (p). So, a plot like this might be useful for you.

# your data
Data <- structure(list(Item = c("a", "b", "c", "d", "e", "f", "g", "h", 
                        "i", "j"), Mean = c(0.66, 0.37, 0.36, 0.12, 0.42, 0.68, 0.19, 
                                            0.27, 0.11, 0.37)), class = "data.frame", row.names = c(NA, -10))

# Adding a new column
Data <- Data %>% 
  mutate(myGroup = ifelse(Mean > 0.5, "Above", "Below")) 

# plot
ggplot(Data, aes(x = Item, y = Mean)) +
  geom_segment(aes(x = Item, xend = Item, y = 0, yend = Mean, color = myGroup), size = 10, alpha = 0.9) +
  geom_text(aes(label = Mean), vjust = 3, colour = "black", fontface = "bold") +
  # plot theme
  theme_classic() +
  # no legend
  theme(legend.position = "none", panel.border = element_blank()) +
  # x and y labs
  xlab("Items") +
  ylab("Difficulties") +
  # setting y limits to 0:1
  ylim(0, 1) +
  # 'Easy' and 'Hard' texts
  annotate(geom = "text", x = .75, y = .85, label = "Easy", color = "red", size = 3) +
  annotate(geom = "text", x = .75, y = .15, label = "Hard", color = "red", size = 3) +
  # horizontal lines (0.20, 0.50 and 0.80)
  geom_hline(yintercept = .5, linetype = "dashed", color = "gray", size = .6) +
  geom_hline(yintercept = .2, linetype = "dashed", color = "red", size = .6) +
  geom_hline(yintercept = .8, linetype = "dashed", color = "red", size = .6)

BarPlot