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")
CodePudding user response:
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")
CodePudding user response:
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))