我試圖將我的資料標簽放置在 ggplot 水平條內,因為標簽在放置在條形末端時會被切斷。我嘗試了各種不同的方法,例如調整hjust位置,但標簽消失在欄下,我什至嘗試使用xlim/增加 x 軸scale_x_continuous以允許完全查看標簽,但到目前為止一直失敗。
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")

uj5u.com熱心網友回復:
或者您可以在條形圖中輸入計數
示例代碼:
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")
陰謀:
uj5u.com熱心網友回復:
只需通過hjust= -0.25并添加 scale_y_continuous(labels = abs, limits = c(0, 50000))
這是代碼
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))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/440257.html
上一篇:使用多種語言更改影像
