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

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

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

  1. library(ggplot2)
  2. library(viridis)
  3. library(forcats)
  4. library(tidyverse)
  5. library("ggtext")
  6. #ert$myPalette <- paste0("<span style=\"color: ", fct_reorder(rank_table$Biocolor,rank_table$Bioprocess_num), "\">", rank_table$name, "</span>")
  7. ert <- structure(list(name = c("B", "C", "A", "E", "D", "F", "G", "H"
  8. ), diff.1 = c(0.054442494, -0.000557423, 0.052649513, 0.046030676, 0.00337372,
  9. 0.05238731, -0.009539911, -0.008215683), myPalette = c("<span style=\"color: #0D0887FF\">B</span>",
  10. "<span style=\"color: #0D0887FF\">C</span>", "<span style=\"color: #0D0887FF\">A</span>",
  11. "<span style=\"color: #0D0887FF\">E</span>", "<span style=\"color: #0D0887FF\">D</span>",
  12. "<span style=\"color: #F0F921FF\">F</span>", "<span style=\"color: #F0F921FF\">G</span>",
  13. "<span style=\"color: #F0F921FF\">H/span>"), contribution.scaled = c(0.054442494,
  14. 0.046030676, 0.052649513, 0.057581535, 0.05238731, 0.082962111,
  15. 0.064853576, 0.101192828), Bioprocess = structure(c(2L, 2L, 2L,
  16. 2L, 2L, 1L, 1L, 1L), .Label = c("Angiogenesis", "Hypertrophy"
  17. ), class = "factor")), row.names = c(NA, -8L), class = "data.frame")
  18. ggplot(data=ert, aes(x=diff.1, y=myPalette, size = contribution.scaled, text=name, color=Bioprocess)) +
  19. geom_point(alpha=0.5) +
  20. scale_size(range = c(1.4, 18), name="Activity") +
  21. scale_color_viridis(discrete=T,option = "C", direction = -1) + #, guide=FALSE) +
  22. theme(legend.position="right") +
  23. theme(axis.text.y=ggtext::element_markdown(size=10)) +
  24. #theme_ipsum() +
  25. guides(color = guide_legend(override.aes = list(size = 10))) +
  26. xlim(-0.1, 0.1) + # Sets limits of y-axis
  27. #xlim(0, 1) + # Sets limits of x-axis
  28. labs(title="Comparison of activated pathways",
  29. x ="<- activated in cc activated in tt -> ", y = "pathways")+
  30. theme(plot.title = element_text(hjust="0.5"))+
  31. #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)) +
  32. theme(text=element_text(size=16.5))

The bubbles are sorted alphabetically i think.

wkyowqbh

wkyowqbh1#

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

  1. library(tidyverse)
  2. library(viridis)
  3. library(ggtext)
  4. library(dplyr)
  5. ert <- ert |>
  6. arrange(desc(Bioprocess), diff.1) |>
  7. mutate(myPalette = fct_inorder(myPalette))
  8. ggplot(data = ert, aes(x = diff.1, y = myPalette, size = contribution.scaled, text = name, color = Bioprocess)) +
  9. geom_point(alpha = 0.5) +
  10. scale_size(range = c(1.4, 18), name = "Activity") +
  11. scale_color_viridis(discrete = T, option = "C", direction = -1) +
  12. theme(legend.position = "right") +
  13. theme(axis.text.y = ggtext::element_markdown(size = 10)) +
  14. # theme_ipsum() +
  15. guides(color = guide_legend(override.aes = list(size = 10))) +
  16. xlim(-0.1, 0.1) + # Sets limits of y-axis
  17. # xlim(0, 1) + # Sets limits of x-axis
  18. labs(
  19. title = "Comparison of activated pathways",
  20. x = "<- activated in cc activated in tt -> ", y = "pathways"
  21. ) +
  22. theme(plot.title = element_text(hjust = "0.5")) +
  23. theme(text = element_text(size = 16.5))

展开查看全部

相关问题