gpt4 book ai didi

r - 更改 y 轴值的顺序

转载 作者:行者123 更新时间:2023-12-04 19:50:39 25 4
gpt4 key购买 nike

我正在使用 ggplot R 包,我用下面的代码做了这个图:

p3 = data.frame(cbind(c(1:70),
c("G1" , "G2" , "G1" , "G1" , "G1" , "G2" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1",
"G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G2" , "G1" , "G2" , "G1" , "G1" ,
"G1" , "G1" , "G1" , "G2" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" ,
"G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" ,
"G1" , "G1" , "G1" , "G1" , "G2" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" , "G1" ,
"G1" , "G1" , "G1" , "G1" , "G2"),
c(rep("B",35),rep("C",15),rep("A",20))
))

colnames(p3)=c("Num","L" , "G")

library(ggthemes)
library(ggplot2)

pp <- ggplot(p3, aes(Num, G)) +
geom_tile(aes(fill = L), colour = "white")
pp + theme(legend.position="bottom", axis.text.x = element_text(angle=90, size=7,vjust=0.5)) + # scale_fill_grey() + theme_classic()
scale_fill_manual(values=c("#990000","#E69F00","#999999"))

enter image description here

我想根据柱数(= 变量 G)更改 y 轴值的顺序

预期剧情:

enter image description here

非常感谢您的帮助!

最佳答案

根据 p3$G 变量的排序表更改 p3$G 的级别:

p3$G <- factor(p3$G, names(sort(table(p3$G))))

sort(table(p3$G))
# C A B
# 15 20 35

关于r - 更改 y 轴值的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49797369/

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