R语言 按最高变量值重新排序多组棒棒糖图

gcxthw6b  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(105)

我试图重新排序我的图表,以显示最大值最高的变量到最低的变量。我最初试图创建一个新的变量,并使用forcats中的fct_inorder,但我无法使其工作。我想我有一个困难的时候,因为我有同一个茎上的设施,但对我来说最有吸引力的是,而不是通过面板将它们分开。
我的数据:

Kappa_viz <- structure(list(vname = c("Variable 1", "Variable 2", "Variable 3", 
                         "Variable 4", "Variable 5", "Variable 6", "Variable 7", "Variable 8", 
                         "Variable 9", "Variable 10", "Variable 11", "Variable 12", "Variable 13", 
                         "Variable 14", "Variable 15", "Variable 16", "Variable 1", "Variable 2", 
                         "Variable 3", "Variable 4", "Variable 5", "Variable 6", "Variable 7", 
                         "Variable 8", "Variable 9", "Variable 10", "Variable 11", "Variable 12", 
                         "Variable 13", "Variable 14", "Variable 15", "Variable 16", "Variable 1", 
                         "Variable 2", "Variable 3", "Variable 4", "Variable 5", "Variable 6", 
                         "Variable 7", "Variable 8", "Variable 9", "Variable 10", "Variable 11", 
                         "Variable 12", "Variable 13", "Variable 14", "Variable 15", "Variable 16"), 
               facname = c("Facility 1", "Facility 1", "Facility 1", "Facility 1", 
                           "Facility 1", "Facility 1", "Facility 1", "Facility 1", "Facility 1", 
                           "Facility 1", "Facility 1", "Facility 1", "Facility 1", "Facility 1", 
                           "Facility 1", "Facility 1", "Facility 2", "Facility 2", "Facility 2", 
                           "Facility 2", "Facility 2", "Facility 2", "Facility 2", "Facility 2", 
                           "Facility 2", "Facility 2", "Facility 2", "Facility 2", "Facility 2", 
                           "Facility 2", "Facility 2", "Facility 2", "Facility 3", "Facility 3", 
                           "Facility 3", "Facility 3", "Facility 3", "Facility 3", "Facility 3", 
                           "Facility 3", "Facility 3", "Facility 3", "Facility 3", "Facility 3", 
                           "Facility 3", "Facility 3", "Facility 3", "Facility 3"), 
               values = c(0.71, 
                          0.22, 0.13, 0.6, 0.22, 0.12, 0.16, 0.1, 0.97, 0.84, 0.53, 0.04, 
                          1, 0.92, 0.55, 0.1, 0.09, 0.84, 0.4, 0.25, 0.53, 0.49, 0.62, 
                          0.55, 0.52, 1, 0.96, 0.9, 0.55, 1, 0.21, 0.51, 0.81, 0.1, 0.63, 
                          0.92, 0.07, 0.55, 0.84, 0.81, 0.27, 0.93, 0.7, 0.05, 0.43, 0.78, 
                          0.47, 0.92)), class = "data.frame", row.names = c(NA, -48L))

下面是图表的代码。它生成了我想要的,但我被困在试图重新排列图表。

library(plyr)
library(dplyr)
library(tidyr)
library(ggplot2)

p <- ggplot(Kappa_viz, 
            aes(y = vname, x = values, fill = facname)) +
  theme(axis.text = element_text(size = 15)) +
  theme(legend.text = element_text(size = 15)) +
  theme(legend.title = element_text(size = 20))+
  ggtitle("Correlation by Facility")+
  theme(plot.title = element_text(size = 20,hjust=0.5))+
  geom_segment(aes(x = 0, y = vname, xend = values, yend = vname), 
               color = "grey50", size=1) +
  geom_point(aes(color=facname), size = 5, alpha=0.7) +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        legend.position = "bottom") +
  scale_color_discrete(name = "Kappa Interpretation")+
  scale_fill_discrete(guide = "none")

print(p)

生成的图表:

我试图创建一个新的变量,并使用fct_inorder从forcat,但我不成功.我失去了代码,不幸的是,所以我没有它.代码运行成功,但图表的顺序保持不变.我试图重新排序它通过使一个订单变量和排序的值由assigment 1 - 16表示最高,但由于我有它across三组,我相信我所做的变量是问题。

wrrgggsh

wrrgggsh1#

尝试从最高值到最低值对 Dataframe 进行排序,然后应用fct_inorder(),以便vname的水平跟随每个变量的第一次出现。
之后,如果我没阅读你的问题&你想要从上到下的最大值到最小值(而不是相反),使用fct_rev()反转水平,因为ggplot的y轴通常是最高值在顶部,最低值在底部。

Kappa_viz_new <- Kappa_viz %>%
  arrange(desc(values)) %>%
  mutate(vname = forcats::fct_inorder(vname) %>%
           forcats::fct_rev())

结果:

相关问题