Update:(见 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")
Update澄清后(删除第一个答案):只删除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")