如何在ggplot中制作基本的R双条形图

huangapple go评论107阅读模式
英文:

How to make base R double barplot in ggplot

问题

I've made the following double marplot in base R, but would like to make the same graph in gig-lot if possible. How would I go about doing this?

我已经在基本的R中制作了以下的双Y轴图,但如果可能的话,我想在gig-lot中制作相同的图表。我该如何操作?

英文:

I've made the following double marplot in base R, but would like to make the same graph in gig-lot if possible. How would I go about doing this?

  1. data <- structure(list(
  2. A= c(53, 39),
  3. B = c(30, 36),
  4. C = c(12, 19),
  5. D = c(5, 6)),
  6. .Names = c("Poor", "Fair", "Good", "Excellent"),
  7. class = "data.frame",
  8. row.names = c(NA, -2L))
  9. attach(data)
  10. print(data)
  11. colors <- c("dodgerblue3","red3")
  12. barplot(as.matrix(data),
  13. main="",
  14. beside = T, ylab = "", ylim = range(0,65), col=colors, border=F)
  15. legend(5,60, legend=c("Fox News", "CNN"),
  16. fill=colors, horiz = T, bty='n')
  17. text(1.5, 55, "53%")
  18. text(2.5, 41, "39%")
  19. text(4.5, 32, "30%")
  20. text(5.5, 38, "36%")
  21. text(7.5, 14, "12%")
  22. text(8.5, 21, "19%")
  23. text(10.5, 7, "5%")
  24. text(11.5, 8, "6%")

如何在ggplot中制作基本的R双条形图

答案1

得分: -3

这是我们可以这样做的方式:

  1. library(ggplot2)
  2. library(dplyr)
  3. library(tidyr)
  4. library(forcats)
  5. data %>%
  6. mutate(Group = row_number()) %>%
  7. pivot_longer(cols = -Group) %>%
  8. ggplot(aes(x = fct_inorder(name), y = value, fill = factor(Group))) +
  9. geom_col(position = position_dodge()) +
  10. labs(x = NULL, y = NULL, fill = NULL) +
  11. scale_fill_manual(values = c("dodgerblue3", "red3"), labels = c("Fox New", "CNN")) +
  12. theme_minimal() +
  13. theme(
  14. axis.text.y = element_blank(),
  15. axis.ticks.y = element_blank(),
  16. panel.grid.major = element_blank(),
  17. panel.grid.minor = element_blank(),
  18. panel.background = element_blank(),
  19. legend.position = c(.95, .95),
  20. legend.justification = c("right", "top"),
  21. legend.box.just = "right",
  22. legend.margin = margin(6, 6, 6, 6),
  23. legend.direction = "horizontal",
  24. axis.text.x = element_text(hjust = 1)
  25. ) +
  26. geom_text(
  27. aes(label = paste0(value, "%")),
  28. position = position_dodge(width = 1),
  29. vjust = -0.5
  30. )

如何在ggplot中制作基本的R双条形图

英文:

Here is how we could do it:

  1. library(ggplot2)
  2. library(dplyr)
  3. library(tidyr)
  4. library(forcats)
  5. data %>%
  6. mutate(Group = row_number()) %>%
  7. pivot_longer(cols = -Group) %>%
  8. ggplot(aes(x = fct_inorder(name), y = value, fill = factor(Group))) +
  9. geom_col(position = position_dodge()) +
  10. labs(x = NULL, y = NULL, fill = NULL) +
  11. scale_fill_manual(values = c("dodgerblue3","red3"), labels = c("Fox New", "CNN"))+
  12. theme_minimal() +
  13. theme(
  14. axis.text.y=element_blank(),
  15. axis.ticks.y=element_blank(),
  16. panel.grid.major = element_blank(),
  17. panel.grid.minor = element_blank(),
  18. panel.background = element_blank(),
  19. legend.position = c(.95, .95),
  20. legend.justification = c("right", "top"),
  21. legend.box.just = "right",
  22. legend.margin = margin(6, 6, 6, 6),
  23. legend.direction="horizontal",
  24. axis.text.x = element_text(hjust = 1)
  25. ) +
  26. geom_text(
  27. aes(label = paste0(value, "%")),
  28. position = position_dodge(width = 1),
  29. vjust = -0.5
  30. )

如何在ggplot中制作基本的R双条形图

huangapple
  • 本文由 发表于 2023年5月18日 01:42:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定