gpt4 book ai didi

r - 使用 fill 参数保持 geom_bar 中变量的相同顺序

转载 作者:行者123 更新时间:2023-12-04 07:36:04 24 4
gpt4 key购买 nike

我有以下数据框:

data <- structure(list(Date = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 
4L), .Label = c("Mar-2021", "Jun-2021", "Sep-2021", "Dec-2021"
), class = "factor"), Key = c("Previous Forecast", "Previous Forecast",
"Previous Forecast", "Previous Forecast", "Current Forecast",
"Current Forecast", "Current Forecast", "Current Forecast"),
Value = c("3.1", "3.4", "3.3", "3.0", "2.8", "4.1", "3.8",
"3.7")), row.names = c(NA, -8L), class = c("tbl_df", "tbl",
"data.frame"))

当我创建一个简单的条形图时,出于某种原因, fill参数从第二个月开始更改条形图的顺序。有没有办法让每个月保持相同的顺序(红条在蓝条之前)?
library(ggplot2)

ggplot(data, aes(fill=Key, y=Value, x=Date)) +
geom_bar(position=position_dodge(), stat="identity")

enter image description here

最佳答案

将数字保留为数字类:

data$Value <- as.numeric(data$Value)

ggplot(data, aes(fill = Key, y = Value, x = Date)) +
geom_bar(position = position_dodge(), stat = "identity")
enter image description here

关于r - 使用 fill 参数保持 geom_bar 中变量的相同顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67735115/

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