使用geom_area()
时是否可以强制执行堆叠顺序?我无法弄清楚为什么geom_area(position = "stack")
会在1605左右的堆栈顺序中产生这种奇怪的波动。
数据框中没有缺失值。
library(ggplot2)
counts <- read.csv("https://gist.githubusercontent.com/mdlincoln/d5e1bf64a897ecb84fd6/raw/34c6d484e699e0c4676bb7b765b1b5d4022054af/counts.csv")
ggplot(counts, aes(x = year, y = artists_strict, fill = factor(nationality))) + geom_area()
答案 0 :(得分:10)
您需要订购数据。在您的数据中,每年找到的第一个值是“佛兰芒”,直到1605年,从1606开始,第一个值是“荷兰语”。所以,如果我们这样做:
ggplot(counts[order(counts$nationality),],
aes(x = year, y = artists_strict, fill = factor(nationality))) + geom_area()
导致
如果我们使用随机排序,则进一步说明:
set.seed(123)
ggplot(counts[sample(nrow(counts)),],
aes(x = year, y = artists_strict, fill = factor(nationality))) + geom_area()
答案 1 :(得分:1)
正如randy所说,ggplot2 2.2.0会自动排序。如果要更改订单,只需重新排序用于填充的因子。如果要在图例中切换哪个组位于顶部而不是图表,则可以将scale_fill_manual()与limits选项一起使用。
(从John Colby生成ggplot颜色的代码)
gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}
cols <- gg_color_hue(2)
图例中的默认排序
ggplot(counts,
aes(x = year, y = artists_strict, fill = factor(nationality))) +
geom_area()+
scale_fill_manual(values=c("Dutch" = cols[1],"Flemish"=cols[2]),
limits=c("Dutch","Flemish"))
传奇中的反向排序
ggplot(counts,
aes(x = year, y = artists_strict, fill = factor(nationality))) +
geom_area()+
scale_fill_manual(values=c("Dutch" = cols[1],"Flemish"=cols[2]),
limits=c("Flemish","Dutch"))
剧情和传奇中的反向排序
counts$nationality <- factor(counts$nationality, rev(levels(counts$nationality)))
ggplot(counts,
aes(x = year, y = artists_strict, fill = factor(nationality))) +
geom_area()+
scale_fill_manual(values=c("Dutch" = cols[1],"Flemish"=cols[2]),
limits=c("Flemish","Dutch"))
答案 2 :(得分:0)
这应该为你做
ggplot(counts[order(counts$nationality),],
aes(x = year, y = artists_strict, fill = factor(nationality))) + geom_area()
希望这会有所帮助