gpt4 book ai didi

r - 如何在 ggplot geom_area 中强制执行堆栈排序

转载 作者:行者123 更新时间:2023-12-04 09:28:28 31 4
gpt4 key购买 nike

使用 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()

最佳答案

您需要订购您的数据。在您的数据中,在 1605 年之前找到的每年的第一个值是“佛兰芒语”,从 1606 年开始,第一个值是“荷兰语”。所以,如果我们这样做:

ggplot(counts[order(counts$nationality),], 
aes(x = year, y = artists_strict, fill = factor(nationality))) + geom_area()

它导致

enter image description here

如果我们使用随机排序,则进一步说明:
set.seed(123)
ggplot(counts[sample(nrow(counts)),],
aes(x = year, y = artists_strict, fill = factor(nationality))) + geom_area()

enter image description here

关于r - 如何在 ggplot geom_area 中强制执行堆栈排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32612511/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com