[英]Add legend for alpha when using identity scale
我正在構建一個稍微復雜的圖,並希望為每個點手動指定Alpha。 scale_alpha_identity
,我可以使用scale_alpha_identity
做到這一點。 但是現在我想為我的Alpha刻度添加一個圖例。 向scale_alpha_identity
添加指南似乎不起作用-即使我嘗試按照文檔中的建議添加中斷和標簽,也只會收到一個錯誤: http : //ggplot2.tidyverse.org/reference/scale_identity.html 。
最小的例子:這產生了我想要的情節,但沒有圖例。
ggplot(data = iris) +
geom_point(aes(x = Sepal.Length, y = Sepal.Width,
alpha = Petal.Length / max(Petal.Length))) +
scale_alpha_identity()
根據文檔,我認為這可以工作,但不能:
ggplot(data = iris) +
geom_point(aes(x = Sepal.Length, y = Sepal.Width,
alpha = Petal.Length / max(Petal.Length))) +
scale_alpha_identity(breaks = c(0, 1), labels = c(0, 1), guide = 'legend')
我還嘗試了許多其他變體:將'colorbar'
或'legend'
直接傳遞給帶有或不帶有換行符和標簽的guide
參數,傳遞guide_legend
或guide_colorbar
而不是字符串-不走運,只是不同的錯誤消息。 按照ffp中的建議添加假標尺是否可以為ggplot的alpha添加圖例? 對我不起作用,因為它會覆蓋我的身份等級。
如果您將limits
在scale_alpha_identity
您可以通過添加一個傳奇guides
。
ggplot(data = iris) +
geom_point(aes(x = Sepal.Length, y = Sepal.Width,
alpha = Petal.Length / max(Petal.Length))) +
scale_alpha_identity(limits = c(.2, 1)) +
guides(alpha = guide_legend() )
根據https://github.com/tidyverse/ggplot2/issues/2112的說法,可能的解決方法是使用size
標識功能...
ggplot(data = iris) +
geom_point(aes(x = Sepal.Length, y = Sepal.Width,
alpha = Petal.Length / max(Petal.Length))) +
scale_size_identity(guide = 'legend')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.