有誰知道如何在ggplot2中控制圖例的順序?從我可以看到的順序來看,出現(xiàn)的順序與實際的比例標簽有關(guān),而不是與比例尺聲明順序有關(guān)。更改比例標題會更改順序。我用菱形數(shù)據(jù)集做了一個小例子來強調(diào)這一點。我正在嘗試將ggplot2用于一系列繪圖,我想使一個變量出現(xiàn)在所有繪圖的右側(cè)。目前,雖然這種情況僅發(fā)生在其中的一些情況下,但我在如何執(zhí)行所需訂購的同時保留適當?shù)谋壤邩撕炆蠀s一無所知。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貢獻1777條經(jīng)驗 獲得超10個贊
在0.9.1中,確定圖例順序的規(guī)則是秘密且不可預(yù)測的?,F(xiàn)在,在github中的dev版本0.9.2中,您可以使用參數(shù)設(shè)置圖例的順序。
這是示例:
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))
- 2 回答
- 0 關(guān)注
- 2675 瀏覽
添加回答
舉報
0/150
提交
取消