我有一個帶有 2 個數值的堆疊條形圖,條形圖堆疊在一起。我想顯示一個說明 2 種顏色描述的圖例。這是我的代碼,但沒有顯示。我會很感激任何人的任何幫助。
ggplot(data=my_data, aes(x = Xaxis))
ggtitle("My Title")
theme(plot.title = element_text(hjust = 0.5, face="bold"))
geom_col(aes(y = v1), fill ="red")
geom_col(aes(y = v2), fill="blue")
labs(y= "Amount", color ="Legend")
theme(legend.title = element_text(color='black',face='bold'),
legend.text = element_text(color='black',face='bold'))
scale_y_continuous(labels = comma)
從上面的代碼圖例不顯示
uj5u.com熱心網友回復:
更新: 正如我在評論中已經提到的,您可能會考慮另一種具有長格式的策略,但是您的格式與顯示填充引數的圖例和獲得正確的顏色相沖突。
如果您將
fill引數放在圖例之外,aes則不會顯示任何圖例,但您會得到正確的顏色。如果您將
fill引數放在其中,aes則將顯示圖例,但顏色將不理想 -> 這是因為您基本上是在生成一個新的因子變數和一個圖例。

First answer: try this:
ggplot(data=my_data, aes(x = Xaxis))
ggtitle("My Title")
theme(plot.title = element_text(hjust = 0.5, face="bold"))
geom_col(aes(y = v1, fill ="red"))
geom_col(aes(y = v2, fill="blue")))
labs(y= "Amount", color ="Legend")
theme(legend.title = element_text(color='black',face='bold'),
legend.text = element_text(color='black',face='bold'))
scale_y_continuous(labels = comma)
your code with the mtcars data set:
ggplot(data=mtcars, aes(x = cyl))
ggtitle("My Title")
theme(plot.title = element_text(hjust = 0.5, face="bold"))
geom_col(aes(y = disp, fill ="red"))
geom_col(aes(y = mpg, fill="blue"))
labs(y= "Amount", color ="Legend")
theme(legend.title = element_text(color='black',face='bold'),
legend.text = element_text(color='black',face='bold'))
gives:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427673.html
