gpt4 book ai didi

r - ggplot barplot中y轴的顺序错误

转载 作者:行者123 更新时间:2023-12-01 00:59:10 25 4
gpt4 key购买 nike

在此条形图中,条形图的缩放方式不同。所以 y 轴没有正确排序(157 > 1342.6)。我该如何解决?

library(reshape)
library(ggplot2)

data <- matrix(1:9, 3, 3)
colnames(data) <- c("approach", "best", "worst")
data[1,] <- c("a", 1.8, 157.0)
data[2,] <- c("b", 592.3, 1342.6)
data[3,] <- c("c", 613.1, 3744.1)
data <- as.data.frame(data)
data <- melt(data, id="approach")

p <- ggplot(data, aes(x=approach, y=value, fill=variable)) +
geom_bar(position="dodge", stat="identity")
p

plot

提前致谢。

最佳答案

您的值列中需要有数值,目前您有以下因素:

library(reshape)
library(ggplot2)

data <- matrix(1:9, 3, 3)
colnames(data) <- c("approach", "best", "worst")
data[1,] <- c("a", 1.8, 157.0)
data[2,] <- c("b", 592.3, 1342.6)
data[3,] <- c("c", 613.1, 3744.1)
data <- as.data.frame(data)
data <- melt(data, id="approach")

data$value = as.double(levels(data$value))[data$value] # <-- converting

p <- ggplot(data, aes(x=approach, y=value, fill=variable)) +
geom_bar(position="dodge", stat="identity")
p

enter image description here

关于r - ggplot barplot中y轴的顺序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25081052/

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