亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

控制ggplot2圖例顯示順序

控制ggplot2圖例顯示順序

阿晨1998 2019-12-25 14:39:58
有誰知道如何在ggplot2中控制圖例的順序?從我可以看到的順序來看,出現的順序與實際的比例標簽有關,而不是與比例尺聲明順序有關。更改比例尺標題會更改順序。我用菱形數據集做了一個小例子來強調這一點。我正在嘗試將ggplot2用于一系列繪圖,我想使一個變量出現在所有繪圖的右側。目前,雖然這種情況僅發生在其中的一些情況下,但我在如何執行所需訂購的同時保留適當的比例尺標簽上一無所知。library(ggplot2)diamond.data <- diamonds[sample(nrow(diamonds), 1000), ]plot <- ggplot(diamond.data, aes(carat, price, colour = clarity, shape = cut)) +  geom_point() + opts(legend.position = "top", legend.box = "horizontal")plot # the legend will appear shape then colour plot + labs(colour = "A", shape = "B") # legend will be colour then shapeplot + labs(colour = "Clarity", shape = "Cut") # legend will be shape then colour
查看完整描述

2 回答

?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

在0.9.1中,確定圖例順序的規則是秘密且不可預測的。現在,在github中的dev版本0.9.2中,您可以使用參數設置圖例的順序。


這是示例:


plot <- ggplot(diamond.data, aes(carat, price, colour = clarity, shape = cut)) +

  geom_point() + opts(legend.position = "top")


plot + guides(colour = guide_legend(order = 1), 

              shape = guide_legend(order = 2))


plot + guides(colour = guide_legend(order = 2), 

              shape = guide_legend(order = 1))


查看完整回答
反對 回復 2019-12-25
  • 2 回答
  • 0 關注
  • 725 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號