R语言 按值对ggplot中的分组数据进行排序

fafcakar  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(246)

嗨,我正在尝试绘制一个图,其中我的数据被分组在一起,即一个过程的气泡在一起,这很有效。现在我想根据这些组的值对其进行排序(最大值到最小值)但保持分组。我做了一个可复制的例子。我希望这足以理解问题并找到解决方案。如果我需要添加任何信息,请让我知道。我还尝试将代码保持在最低限度。我尝试了fct_reorder,并且我已经在我的代码中使用了。
谢啦,谢啦

library(ggplot2)
library(viridis)
library(forcats)
library(tidyverse)
library("ggtext")



#ert$myPalette <- paste0("<span style=\"color: ", fct_reorder(rank_table$Biocolor,rank_table$Bioprocess_num), "\">", rank_table$name, "</span>")

ert <- structure(list(name = c("B", "C", "A", "E", "D", "F", "G", "H"
), diff.1 = c(0.054442494, -0.000557423, 0.052649513, 0.046030676, 0.00337372, 
              0.05238731, -0.009539911,  -0.008215683), myPalette = c("<span style=\"color: #0D0887FF\">B</span>", 
                                                                                   "<span style=\"color: #0D0887FF\">C</span>", "<span style=\"color: #0D0887FF\">A</span>", 
                                                                                   "<span style=\"color: #0D0887FF\">E</span>", "<span style=\"color: #0D0887FF\">D</span>", 
                                                                                   "<span style=\"color: #F0F921FF\">F</span>", "<span style=\"color: #F0F921FF\">G</span>", 
                                                                                   "<span style=\"color: #F0F921FF\">H/span>"), contribution.scaled = c(0.054442494, 
                                                                                                                                                        0.046030676, 0.052649513, 0.057581535, 0.05238731, 0.082962111,  
                                                                                                                                                            0.064853576, 0.101192828), Bioprocess = structure(c(2L, 2L, 2L, 
                                                                                                                                                                                                                2L, 2L, 1L, 1L, 1L), .Label = c("Angiogenesis", "Hypertrophy"
                                                                                                                                                                                                                ), class = "factor")), row.names = c(NA, -8L), class = "data.frame")
 


ggplot(data=ert, aes(x=diff.1, y=myPalette, size = contribution.scaled, text=name, color=Bioprocess)) +
  geom_point(alpha=0.5) +
  scale_size(range = c(1.4, 18), name="Activity") +
  scale_color_viridis(discrete=T,option = "C", direction = -1) + #, guide=FALSE) +
  theme(legend.position="right") +
  theme(axis.text.y=ggtext::element_markdown(size=10)) +
  #theme_ipsum() +
  guides(color = guide_legend(override.aes = list(size = 10))) +
  xlim(-0.1, 0.1) + # Sets limits of y-axis
  #xlim(0, 1) + # Sets limits of x-axis
  labs(title="Comparison of activated pathways",
       x ="<- activated in cc          activated in tt ->   ", y = "pathways")+ 
  theme(plot.title = element_text(hjust="0.5"))+
  #theme(legend.position = c(.9, .3)) + guides(fill=guide_colourbar(title="Combined Z-Score", direction ="horizontal", barwidth = 10, ticks = FALSE, title.position="top", title.hjust=0.5))  +
  theme(text=element_text(size=16.5))

The bubbles are sorted alphabetically i think.

wkyowqbh

wkyowqbh1#

一种选择是按照所需的顺序排列数据集,然后使用forcats::fct_inorder来固定类别的顺序:

library(tidyverse)
library(viridis)
library(ggtext)
library(dplyr)

ert <- ert |> 
  arrange(desc(Bioprocess), diff.1) |> 
  mutate(myPalette = fct_inorder(myPalette))

ggplot(data = ert, aes(x = diff.1, y = myPalette, size = contribution.scaled, text = name, color = Bioprocess)) +
  geom_point(alpha = 0.5) +
  scale_size(range = c(1.4, 18), name = "Activity") +
  scale_color_viridis(discrete = T, option = "C", direction = -1) +
  theme(legend.position = "right") +
  theme(axis.text.y = ggtext::element_markdown(size = 10)) +
  # theme_ipsum() +
  guides(color = guide_legend(override.aes = list(size = 10))) +
  xlim(-0.1, 0.1) + # Sets limits of y-axis
  # xlim(0, 1) + # Sets limits of x-axis
  labs(
    title = "Comparison of activated pathways",
    x = "<- activated in cc          activated in tt ->   ", y = "pathways"
  ) +
  theme(plot.title = element_text(hjust = "0.5")) +
  theme(text = element_text(size = 16.5))

相关问题