Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
级别分解无法转换为ggplot BARCPLOT变量顺序_R_Ggplot2_Graph_Scale_Geom Bar - Fatal编程技术网

级别分解无法转换为ggplot BARCPLOT变量顺序

级别分解无法转换为ggplot BARCPLOT变量顺序,r,ggplot2,graph,scale,geom-bar,R,Ggplot2,Graph,Scale,Geom Bar,我试图在ggplot堆叠条形图中对变量进行排序。 以下是我目前的代码: levels(rs$Site) <- c("Mature","Little East","Upper Fill","Lower Fill") # I have rearranged the levels to the desired order, but the output looks like # c("Little East","Lower Fill","Upper Fill","Mature") libra

我试图在ggplot堆叠条形图中对变量进行排序。 以下是我目前的代码:

levels(rs$Site) <- c("Mature","Little East","Upper Fill","Lower Fill")
# I have rearranged the levels to the desired order, but the output looks like 
# c("Little East","Lower Fill","Upper Fill","Mature")

library(ggplot2)
library(scales)
ggplot(rs, aes(x = Site)) + geom_bar(aes(fill = At.Mature), position = 'fill') +
    scale_x_discrete(limits=unique(rs$Site)) +
    coord_flip()
我曾尝试使用factor()对级别进行重新排序,但结果保持不变


为什么“小东方”会移到末尾(图的底部)?如何解决此问题?

我们可以使用顺序中指定的
级别再次调用
因子

rs$Site <- factor(rs$Site, levels = c("Mature", "Little East", 
          "Upper Fill", "Lower Fill"))
数据
这解决了问题。我不知道指定级别会改变值。谢谢你。@BryantLuna很高兴它对你有用。你也可以查一下
rs$Site <- factor(rs$Site, levels = c("Mature", "Little East", 
          "Upper Fill", "Lower Fill"))
ggplot(rs, aes(x = Site)) +  
      geom_bar(aes(fill = At.Mature), position = 'fill') + 
      scale_x_discrete(limits = levels(rs$Site)) + 
      coord_flip()
set.seed(24)
rs <- data.frame(Site = sample(c("Mature","Little East",
"Upper Fill","Lower Fill"), 30, replace = TRUE), 
   At.Mature = sample(c("Yes", "No"), 30, replace = TRUE))
set.seed(24)
v1 <- factor(sample(LETTERS[1:5], 20, replace = TRUE))
v1
#[1] B B D C D E B D E B D B D D B E A A C A
#Levels: A B C D E
levels(v1) <- c('C', 'D', 'E', 'A', 'B')
v1
#[1] D D A E A B D A B D A D A A D B C C E C  ### values got replaced
#Levels: C D E A B