[英]How do you order the fill-colours within ggplot2 geom_bar
我正在調用 ggplot 函數
ggplot(data,aes(x,y,fill=category)+geom_bar(stat="identity")
結果是條形圖,條形圖由與類別對應的各種顏色填充。 但是,條與條之間的顏色順序不一致。 假設有粉紅色、綠色和藍色。 有些條從下到上變成粉紅色、綠色、藍色,有些則變成綠色、粉紅色、藍色。 我沒有看到任何明顯的模式。
這些訂單是如何選擇的? 我怎樣才能改變它? 至少,我怎樣才能讓 ggplot 選擇一致的順序?
(x,y 和 category) 的類分別是 (integer,numeric 和 factor)。 如果我將類別作為有序因素,它不會改變這種行為。
有人知道怎么修這個東西嗎?
可重現的例子:
dput(data)
structure(list(mon = c(9L, 10L, 11L, 10L, 8L, 7L, 7L, 11L, 9L,
10L, 12L, 11L, 7L, 12L, 8L, 12L, 9L, 7L, 9L, 10L, 10L, 8L, 12L,
7L, 11L, 10L, 8L, 7L, 11L, 12L, 12L, 9L, 9L, 7L, 7L, 12L, 12L,
9L, 9L, 8L), gclass = structure(c(9L, 1L, 8L, 6L, 4L, 4L, 3L,
6L, 2L, 4L, 1L, 1L, 5L, 7L, 1L, 6L, 8L, 6L, 4L, 7L, 8L, 7L, 9L,
8L, 3L, 5L, 9L, 2L, 7L, 3L, 5L, 5L, 7L, 7L, 9L, 2L, 4L, 1L, 3L,
8L), .Label = c("Down-Down", "Down-Stable", "Down-Up", "Stable-Down",
"Stable-Stable", "Stable-Up", "Up-Down", "Up-Stable", "Up-Up"
), class = c("ordered", "factor")), NG = c(222614.67, 9998.17,
351162.2, 37357.95, 4140.48, 1878.57, 553.86, 40012.25, 766.52,
15733.36, 90676.2, 45000.29, 0, 375699.84, 2424.21, 93094.21,
120547.69, 291.33, 1536.38, 167352.21, 160347.01, 26851.47, 725689.06,
4500.55, 10644.54, 75132.98, 42676.41, 267.65, 392277.64, 33854.26,
384754.67, 7195.93, 88974.2, 20665.79, 7185.69, 45059.64, 60576.96,
3564.53, 1262.39, 9394.15)), .Names = c("mon", "gclass", "NG"
), row.names = c(NA, -40L), class = "data.frame")
ggplot(data,aes(mon,NG,fill=gclass))+geom_bar(stat="identity")
從 ggplot2_2.0.0 開始, order
美學不再可用。 要獲得按填充顏色排序的堆棧圖,您只需按要排序的分組變量對數據集進行排序。
為此,我經常使用dplyr 的arrange
。 在這里,我通過ggplot
調用中的fill
因子對數據集進行排序,而不是創建一個有序的數據集,但兩者都可以正常工作。
library(dplyr)
ggplot(arrange(data, gclass), aes(mon, NG, fill = gclass)) +
geom_bar(stat = "identity")
這在基礎 R 中很容易完成,當然,使用帶有提取括號的經典order
:
ggplot(data[order(data$gclass), ], aes(mon, NG, fill = gclass)) +
geom_bar(stat = "identity")
ggplot2_2.2.0 更新
在 ggplot_2.2.0 中,填充順序基於因子水平的順序。 默認順序將在堆棧頂部而不是底部繪制第一級。
如果您想要堆棧底部的第一級,您可以在position_stack
使用reverse = TRUE
。 請注意,您還可以使用geom_col
作為geom_bar(stat = "identity")
快捷方式。
ggplot(data, aes(mon, NG, fill = gclass)) +
geom_col(position = position_stack(reverse = TRUE))
您還需要指定order
美學。
ggplot(data,aes(mon,NG,fill=gclass,order=gclass))+
geom_bar(stat="identity")
這可能是也可能不是錯誤。
要訂購,您必須使用levels
參數並通知訂單。 像這樣:
data$gclass
(data$gclass2 <- factor(data$gclass,levels=sample(levels(data$gclass)))) # Look the difference in the factors order
ggplot(data,aes(mon,NG,fill=gclass2))+geom_bar(stat="identity")
您可以使用scale_fill_
函數更改顏色。 例如:
ggplot(dd,aes(mon,NG,fill=gclass)) +
geom_bar(stat="identity") +
scale_fill_brewer(palette="blues")
要在bars
獲得一致的排序,則需要對數據框進行排序:
dd = dd[with(dd, order(gclass, -NG)), ]
為了更改圖例的順序,請更改gclass
因子。 所以像:
dd$gclass= factor(dd$gclass,levels=sort(levels(dd$gclass), TRUE))
以@aosmith 的回答為基礎,另一種排序條形的方法,我發現稍微更直觀的是:
ggplot(data, aes(x=mon, y=reorder(NG,gclass), fill = gclass)) +
geom_bar(stat = "identity")
base stats 包中重新排序函數的美妙之處在於,您可以將它應用到reorder(based_on_dimension, y, function)
其中 y 使用 sum、mean 等函數對 based_on_dimension 進行排序。
由於此交換首先出現在“因子填充訂單”中,因此我將添加另一個解決方案,我認為它更直接一些,並且不需要更改您的基礎數據。
ggplot(data,aes(x,y,fill=factor(category, levels = c("Down-Down", "Down-Stable", "Down-Up", "Stable-Down", "Stable-Stable", "Stable-Down", "Up-Down", "Up-Stable", "Up-Up"))) +
geom_col(position = position_stack(reverse = FALSE))
或者按照我的喜好,我首先創建一個變量向量以簡化以后的編碼並使其更易於編輯:
v_factor_levels <- c("Down-Down", "Down-Stable", "Down-Up", "Stable-Down", "Stable-Stable", "Stable-Down", "Up-Down", "Up-Stable", "Up-Up")
ggplot(data,aes(x,y,fill=factor(category, levels = v_factor_levels)) +
geom_col(position = position_stack(reverse = FALSE))
你不需要 geom_col() 中的反向位置元素,我保留這些作為提醒,以防我想反向,但你可以通過消除它來進一步簡化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.