簡體   English   中英

ggplot2:geom_bar填充顏色; 如何更改為不同的數據分組

[英]ggplot2: geom_bar fill colour; how to change to different grouping of data

我有以下數據集,顯示了會計期間,產品和交易總額:

app$FISCAL_PERIOD <-c(201604,201604,201604,201605,201605,201605,201606,201606,201606,201607,201607,201607,201608,201608,201608,201609,201609,201610,201610,201611,201611)

app$Product <- c("Product 1","Product 3","Other","Product 1","Product 3","Other","Product 1","Product 3","Other","Product 1","Product 3","Other","Product 1","Product 3","Other","Product 2","Product 3","Product 2","Product 3","Product 2","Product 3")

app$sum_trans<-c(78,23410,1946,84,29532,417,16,30364,129,305,32386,584,424,20873,274,20,20929,470,19261,10,6131)

我在ggplot2中將其繪制為躲避的條形圖。 Ggplot會自動為每個填充分配顏色,以便每個會計期間使用不同的顏色。

ggplot(data = app, aes(x = Product, y = sum_trans, fill = as.factor(FISCAL_PERIOD))) + 
    geom_bar(stat="identity", position="dodge", colour="black")

上面的ggplot代碼繪制的圖

我需要顯示每種具有相同顏色和不影響圖表的填充變量的產品。 即我希望所有產品1會計期間數據為一種顏色,而所有產品3會計期間數據為另一種顏色。

您可以將fill變量設置為product ,以定義所需的顏色,並指定其他組變量FISCAL_PERIOD以便每個條形仍按年份進行細分:

ggplot(data = app, aes(x = Product, y = sum_trans, fill = Product, group = FISCAL_PERIOD)) + 
   geom_bar(stat="identity", position="dodge", colour="black")

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM