简体   繁体   中英

Horizontal ggplot - how to position data label inside bar

I am trying to position my data label inside the ggplot horizontal bar as the label gets cut off when placed at end of the bar. I have tried various different methods such as adjusting hjust position but the label disappears under the bar, I have even tried to increase x axis using xlim / scale_x_continuous to allow the label to be fully viewed but so far keep failing.

library(tidyverse)
library(ggplot2)

mydf <- data.frame( Category = c("Approved transactions"),
                    Nov_21=c(11028),
                    Dec_21=c(31455),
                    Jan_22=c(44480))

mydf %>%
  gather(Month, Total, -Category) %>%
  mutate(Month = reorder(Month, row_number())) %>%
  mutate(Category = reorder(Category, row_number())) %>%
  ggplot(aes(Month, Total, fill = Month, group = Category)) +
  geom_text(aes(label=Total), position=position_dodge(width=0.9), hjust= 0.25) +
  geom_bar(stat = "identity", position = "dodge", width=0.5, fill="light blue") +
  coord_flip() +
  labs(x = "", y = "", title = "Approved Counter Transactions", subtitle = "3 Month View", legend=FALSE) +
  theme_bw() +
  theme(panel.grid.major.x = element_blank(),
        panel.border = element_blank(),
        plot.title = element_text(hjust = 0),
        axis.text.x = element_text(size = 10, face = "bold"),
        axis.text.y = element_text(size = 10, face = "bold"),
        legend.position = "none")

在此处输入图像描述

Or you can enter the counts inside the bar plots

Sample code:

mydf %>%
  gather(Month, Total, -Category) %>%
  mutate(Month = reorder(Month, row_number())) %>%
  mutate(Category = reorder(Category, row_number())) %>%
  ggplot(aes(Month, Total, fill = Month, group = Category)) +
  geom_bar(stat = "identity", position = "dodge", width=0.5, fill="light blue") +
  geom_text(aes(label=Total),position = position_dodge(width = .4),hjust=1.3, size = 10)+
  labs(x = "", y = "", title = "Approved Counter Transactions", subtitle = "3 Month View", legend=FALSE) +
  theme_bw() +
  theme(panel.grid.major.x = element_blank(),
        panel.border = element_blank(),
        plot.title = element_text(hjust = 0),
        axis.text.x = element_text(size = 10, face = "bold"),
        axis.text.y = element_text(size = 10, face = "bold"),
        legend.position = "none")

Plot: 在此处输入图像描述

Just pass hjust= -0.25 and add + scale_y_continuous(labels = abs, limits = c(0, 50000)) This is the code

mydf %>%
gather(Month, Total, -Category) %>%
mutate(Month = reorder(Month, row_number())) %>%
mutate(Category = reorder(Category, row_number())) %>%
ggplot(aes(Month, Total, fill = Month, group = Category)) +
geom_text(aes(label=Total), position=position_dodge(width=0.9), hjust= -0.25) +
geom_bar(stat = "identity", position = "dodge", width=0.5, fill="light blue") +
coord_flip() +
labs(x = "", y = "", title = "Approved Counter Transactions", subtitle = "3 Month View", legend=FALSE) +
theme_bw() +
theme(panel.grid.major.x = element_blank(),
      panel.border = element_blank(),
      plot.title = element_text(hjust = 0),
      axis.text.x = element_text(size = 10, face = "bold"),
      axis.text.y = element_text(size = 10, face = "bold"),
      legend.position = "none") +
scale_y_continuous(labels = abs, limits = c(0, 50000))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM