2017-08-19 34 views
-1

我在ggplot2中創建了一個堆疊條。每個條顯示不同條目碼的銷售量。對於某些條目碼,銷售率很低,我無法看到書面銷售百分比正確由於oberlapping.How我可以解決這個問題?繪圖在編織後在R markdown中不可見

這是我基於銷售itemcode的情節: This is my plot of itemcode based on sale

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

這是什麼都與Rmarkdown辦? –

+0

我不能自己嘗試這個,因爲你的問題是不可重複的,但嘗試添加+ coord_flip()到你的ggplot。這可能釋放一些空間並使標籤可讀。 –

+0

我想把這個陰謀添加到我的r降價文件中。降價文件可以正常工作,但是當我將這個降價文件推到我的github上時,所有的陰謀都消失了。或許我忘了在我的問題中提到這一點。 –

回答

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") 

相關問題