How to adjust label position in ggplot bar graph in R with positive and negative values?

I have a bar graph with positive and negative values, and want to have the value labels just outside each bar (i.e., above the bars with positive values and below the bars with negative values). However, I can only either get them to be right at the edge of the bar (where it is unreadable), or all value labels adjusted upward (which looks strange for the negative values).[Figure 1]([[Figure 2] https://i.stack.imgur.com/zc7zf.jpg)].

The code I’ve tried:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>ggplot(data = data, mapping = aes(x = IV, y = DV, fill = Mod)) +
stat_summary(fun.data = mean_sdl, geom = "bar", position = position_dodge(), color = "black") +
theme_classic() +
theme(text = element_text(family = "Arial")) +
labs(y = "Dependent Variable") +
theme(plot.title = element_text(hjust = 0.5, vjust = -3, size = 25, face = 'bold'), axis.title = element_text(size = 15, face = "bold"),
axis.text.x = element_text(size = 15), axis.text.y = element_text(size = 15), legend.position = "right") +
scale_x_discrete("Factor 1", labels = c("Level 1","Level 2", "Level 3")) + # Needs this variable to be a FACTOR
scale_fill_manual(values = c("#9a0000", "#02029883"), name = "Factor 1", breaks = c("0","1"), labels = c("Level 1","Level 3")) +
coord_cartesian(ylim = c(-1, 1)) +
scale_y_continuous(breaks = c(-1, 0, 1)) +
stat_summary(fun.data = mean_se, geom = "errorbar", width=0.3, position = position_dodge(.9)) +
stat_summary(aes(label = round(..y.., 2)), fun.y = mean, geom = "text", family = "Arial", size = 3, vjust = -4, position = position_dodge(0.9)) +
geom_segment(aes(x = 0, xend = 3, y = 0, yend = 0))
</code>
<code>ggplot(data = data, mapping = aes(x = IV, y = DV, fill = Mod)) + stat_summary(fun.data = mean_sdl, geom = "bar", position = position_dodge(), color = "black") + theme_classic() + theme(text = element_text(family = "Arial")) + labs(y = "Dependent Variable") + theme(plot.title = element_text(hjust = 0.5, vjust = -3, size = 25, face = 'bold'), axis.title = element_text(size = 15, face = "bold"), axis.text.x = element_text(size = 15), axis.text.y = element_text(size = 15), legend.position = "right") + scale_x_discrete("Factor 1", labels = c("Level 1","Level 2", "Level 3")) + # Needs this variable to be a FACTOR scale_fill_manual(values = c("#9a0000", "#02029883"), name = "Factor 1", breaks = c("0","1"), labels = c("Level 1","Level 3")) + coord_cartesian(ylim = c(-1, 1)) + scale_y_continuous(breaks = c(-1, 0, 1)) + stat_summary(fun.data = mean_se, geom = "errorbar", width=0.3, position = position_dodge(.9)) + stat_summary(aes(label = round(..y.., 2)), fun.y = mean, geom = "text", family = "Arial", size = 3, vjust = -4, position = position_dodge(0.9)) + geom_segment(aes(x = 0, xend = 3, y = 0, yend = 0)) </code>
ggplot(data = data, mapping = aes(x = IV, y = DV, fill = Mod)) +
  stat_summary(fun.data = mean_sdl, geom = "bar", position = position_dodge(), color = "black") + 
  theme_classic() + 
  theme(text = element_text(family = "Arial")) + 
  labs(y = "Dependent Variable") +
  theme(plot.title = element_text(hjust = 0.5, vjust = -3, size = 25, face = 'bold'), axis.title = element_text(size = 15, face = "bold"), 
    axis.text.x = element_text(size = 15), axis.text.y = element_text(size = 15), legend.position = "right") +
  scale_x_discrete("Factor 1", labels = c("Level 1","Level 2", "Level 3")) + # Needs this variable to be a FACTOR
  scale_fill_manual(values = c("#9a0000", "#02029883"), name = "Factor 1", breaks = c("0","1"), labels = c("Level 1","Level 3")) + 
  coord_cartesian(ylim = c(-1, 1)) +
  scale_y_continuous(breaks = c(-1, 0, 1)) + 
  stat_summary(fun.data = mean_se, geom = "errorbar", width=0.3, position = position_dodge(.9)) + 
stat_summary(aes(label = round(..y.., 2)), fun.y = mean, geom = "text", family = "Arial", size = 3, vjust = -4, position = position_dodge(0.9)) +
geom_segment(aes(x = 0, xend = 3, y = 0, yend = 0))

I’ve tried several iterations with geom_text() but since I want to display a label of the mean, it shows all observations instead of one.

Based on other similar questions on SO, I’ve tried several alternatives. E.g., instead of:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>stat_summary(aes(label = round(..y.., 2)), fun.y = mean, geom = "text", family = "Arial", size = 3, position = position_dodge(0.9)) +
</code>
<code>stat_summary(aes(label = round(..y.., 2)), fun.y = mean, geom = "text", family = "Arial", size = 3, position = position_dodge(0.9)) + </code>
stat_summary(aes(label = round(..y.., 2)), fun.y = mean, geom = "text", family = "Arial", size = 3, position = position_dodge(0.9)) +

I’ve also tried:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> geom_label(aes(label = round(mean(DV), digits = 0), segment.colour = NA)) +
</code>
<code> geom_label(aes(label = round(mean(DV), digits = 0), segment.colour = NA)) + </code>
 geom_label(aes(label = round(mean(DV), digits = 0), segment.colour = NA)) +

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>geom_text(aes(mean_IV = mean_IV + sign(mean_IV),label = mean_IV)) +
</code>
<code>geom_text(aes(mean_IV = mean_IV + sign(mean_IV),label = mean_IV)) + </code>
geom_text(aes(mean_IV = mean_IV + sign(mean_IV),label = mean_IV)) +
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> geom_text(aes(label = round(..y.., 2), fun.y = mean, vjust = place), size = 4, fontface = "bold", family = "Fira Sans") +
</code>
<code> geom_text(aes(label = round(..y.., 2), fun.y = mean, vjust = place), size = 4, fontface = "bold", family = "Fira Sans") + </code>
 geom_text(aes(label = round(..y.., 2), fun.y = mean, vjust = place), size = 4, fontface = "bold", family = "Fira Sans") +

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>stat_summary(data = . %>% filter(mean(zEng) >= 0), aes(label = round(..y.., 2)), fun.y = mean, geom = "text", family = "Arial", size = 3, vjust = -5, position = position_dodge(0.9)) +
stat_summary(data = . %>% filter(mean(zEng) < 0), aes(label = round(..y.., 2)), fun.y = mean, geom = "text", family = "Arial", size = 3, vjust = 5, position = position_dodge(0.9)) +
</code>
<code>stat_summary(data = . %>% filter(mean(zEng) >= 0), aes(label = round(..y.., 2)), fun.y = mean, geom = "text", family = "Arial", size = 3, vjust = -5, position = position_dodge(0.9)) + stat_summary(data = . %>% filter(mean(zEng) < 0), aes(label = round(..y.., 2)), fun.y = mean, geom = "text", family = "Arial", size = 3, vjust = 5, position = position_dodge(0.9)) + </code>
stat_summary(data = . %>% filter(mean(zEng) >= 0), aes(label = round(..y.., 2)), fun.y = mean, geom = "text", family = "Arial", size = 3, vjust = -5, position = position_dodge(0.9)) +
stat_summary(data = . %>% filter(mean(zEng) < 0), aes(label = round(..y.., 2)), fun.y = mean, geom = "text", family = "Arial", size = 3, vjust = 5, position = position_dodge(0.9)) +

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># geom_text(aes(label = round(..y.., 2), fun.y = mean, vjust = ifelse(fun.y >= 0, 0, 1))) +
</code>
<code># geom_text(aes(label = round(..y.., 2), fun.y = mean, vjust = ifelse(fun.y >= 0, 0, 1))) + </code>
# geom_text(aes(label = round(..y.., 2), fun.y = mean, vjust = ifelse(fun.y >= 0, 0, 1))) +

and

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>vjust = ifelse(..y.. >= 0, 1, -1)
</code>
<code>vjust = ifelse(..y.. >= 0, 1, -1) </code>
vjust = ifelse(..y.. >= 0, 1, -1)

New contributor

dka238 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật