我在ggplot2中创建了一个堆积条。每个条形图显示不同商品代码的销售额。对于某些商品代码,销售率很低,我无法正确看到书面销售百分比因为oberlapping.How我可以解决此?
这是我基于销售的商品代码情节:
ggplot(data = sale1, aes(x = ItemCode,y = SaleRate , group = FromFranchise)) +
geom_col(aes(fill = FromFranchise))+
geom_text(aes(label = paste0(round(SaleRate*100,1),"%")), position =
position_stack(vjust = 0.5),
color="white")+
xlab("Item Code")+
ylab("Sale Rate")+
ggtitle("Sale Rate Of Based On Item Code")+
theme(plot.title = element_text(color = "dodgerblue4",size = 30,hjust =
0.5),
axis.title.x = element_text(color = "dodgerblue4",size = 20),
axis.title.y = element_text(color = "dodgerblue4",size = 20),
axis.text.x = element_text(size = 15),
axis.text.y = element_text(size = 15),
legend.title = element_text(color = "dodgerblue4",size = 20),
legend.text = element_text(size = 15),
legend.position = "none",
legend.direction = "horizontal" ,
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
答案 0 :(得分:0)
我认为您最好的选择是围绕FromFranchise变量创建一个分面图表。请参阅以下示例:
library(ggplot2)
ItemCode <- c(rep(c("2", "3", "4", "5"), each = 4))
FromFranchise <- c(rep(c("A", "B", "C", "D"), times = 4))
SaleRate <- c(0.1, 0.05, 0.15, 0.18, 0.01, 0.02, 0.01, 0.03, 0.09, 0.12, 0.2, 0.15, 0.14, 0.23, 0.1, 0.05)
sale1 <- data.frame(ItemCode, FromFranchise, SaleRate)
# original chart
ggplot(sale1, aes(x = ItemCode, y = SaleRate, fill = FromFranchise, label = SaleRate)) +
geom_col() +
geom_text(aes(label = paste0(round(SaleRate * 100, 1), "%")),
position = position_stack(vjust = 0.5), color = "white") +
guides(fill = FALSE) +
labs(x = "Item Code", y = "Sales Rate", title = "Sale Rate Of Based On Item Code")
# faceted chart
ggplot(sale1, aes(x = ItemCode, y = SaleRate, fill = FromFranchise)) +
geom_col() +
geom_text(aes(label = paste0(round(SaleRate * 100, 1), "%")), vjust = -0.3) +
guides(fill = FALSE) +
facet_wrap(~ FromFranchise) +
ylim(0, 0.3) +
labs(x = "Item Code", y = "Sales Rate", title = "Sale Rate Of Based On Item Code")