如何使用cowplot和ggplot排列多个图。

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

How to arrange multiple plots in using cowplot and ggplot

问题

以下是代码的翻译部分:

  1. library(tidyverse)
  2. library(ggrepel)
  3. library(ggtext)
  4. library(lubridate)
  5. library(scales)
  6. library(cowplot)
  7. # 绘制1数据
  8. date <- as.Date(c("2019-09-01", "2019-10-01", "2019-11-01", "2019-12-01",
  9. "2020-01-01", "2020-02-01", "2020-03-01", "2020-04-01"))
  10. year <- c(2019, 2019, 2019, 2019, 2020, 2020, 2020, 2020)
  11. month <- c("Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr")
  12. sales <- c(100, 200, 600, 200, 100, 100, 800, 100)
  13. df <- data.frame(date, year, month, sales)
  14. df <- add_row(df, date = as.Date("2019-08-15"), year = 2019, month = "Aug", sales = NA)
  15. df <- add_row(df, date = as.Date("2020-04-15"), year = 2020, month = "Apr", sales = NA)
  16. df <- df %>% arrange(date)
  17. # 绘制1无年度分面
  18. data_start <- df %>% filter(row_number() == 2)
  19. data_mid <- df %>% filter(row_number() == 5)
  20. data_end <- df %>% filter(row_number() == 9)
  21. p1 <- ggplot(df, aes(x = date, y = sales)) +
  22. geom_line() +
  23. scale_x_date(
  24. date_labels = "%b",
  25. date_breaks = "month",
  26. expand = c(0, 0)
  27. ) +
  28. theme_bw() +
  29. theme(
  30. strip.placement = "outside",
  31. strip.background = element_rect(fill = NA, colour = NA),
  32. panel.spacing = unit(0, "lines"),
  33. axis.line = element_line(colour = "gray"),
  34. panel.grid.major = element_blank(),
  35. panel.grid.minor = element_blank(),
  36. panel.border = element_blank(),
  37. panel.background = element_blank(),
  38. axis.title.x = element_text(colour = "gray"),
  39. axis.title.y = element_text(hjust = 1, colour = "gray"),
  40. axis.text.x = element_text(colour = "gray"),
  41. axis.text.y = element_text(colour = "gray"),
  42. strip.text.x = element_text(colour = "gray", size = 9),
  43. plot.title = element_markdown(size = 14),
  44. plot.subtitle = element_text(size = 10, colour = "#818589"),
  45. plot.margin = margin(0, 0, 0, 0, "cm")
  46. ) + #margins: (top,right,bottom,left)
  47. geom_point(data_start, mapping = aes(x = date, y = sales), colour = "blue", size = 2) +
  48. geom_point(data_mid, mapping = aes(x = date, y = sales), colour = "blue", size = 2) +
  49. geom_point(data_end, mapping = aes(x = date, y = sales), colour = "blue", size = 2) +
  50. geom_text(data = subset(df, year == 2019 & month == "Sep"),
  51. aes(date, sales, label = sales),
  52. position = position_dodge(width = 1),
  53. vjust = -1, hjust = 1, size = 5) +
  54. geom_text(data = subset(df, year == 2019 & month == "Dec"),
  55. aes(date, sales, label = sales),
  56. position = position_dodge(width = 1),
  57. vjust = 2, size = 5) +
  58. geom_text(data = subset(df, year == 2020 & month == "Apr"),
  59. aes(date, sales, label = sales),
  60. position = position_dodge(width = 1),
  61. vjust = 0, hjust = 0, size = 5) +
  62. geom_text_repel(data = df[5, ], label = "Total number of dollars circulation", nudge_y = 500, nudge_x = 10, color = "blue") +
  63. labs(title = "Chocolate sales",
  64. y = "Number of Units",
  65. x = "2019 2020")
  66. # 绘制2
  67. df2 <- df %>% filter(date == "2020-03-01" | date == "2020-04-04")
  68. choco_date <- as.Date(c("2021-01-01", "2021-01-01", "2021-01-01",
  69. "2022-01-01", "2022-01-01", "2022-01-01"))
  70. choco_type <- c("Dark", "Milk", "White", "Dark", "Milk", "White")
  71. choco_sales <- c(1000, 600, 100, 800, 400, 200)
  72. df2 <- data.frame(choco_date, choco_type, choco_sales)
  73. dark_start <- df2 %>% filter(choco_type == "Dark" & choco_date == "2021-01-01")
  74. milk_start <- df2 %>% filter(choco_type == "Milk" & choco_date == "2021-01-01")
  75. white_start <- df2 %>% filter(choco_type == "White" & choco_date == "2021-01-01")
  76. dark_end <- df2 %>% filter(choco_type == "Dark" & choco_date == "2022-01-01")
  77. milk_end <- df2 %>% filter(choco_type == "Milk" & choco_date == "2022-01-01")
  78. white_end <- df2 %>% filter(choco_type == "White" & choco_date == "2022-01-01")
  79. p2 <- df2 %>% ggplot(aes(x = choco_date, y = choco_sales, color = choco_type)) +
  80. geom_line() +
  81. geom_point(dark_start, mapping = aes(x = choco_date, y = choco_sales), colour = "red", size = 2) +
  82. geom_point(milk_start, mapping = aes(x = choco_date, y = choco_sales), colour = "green", size = 2) +
  83. geom_point(white_start, mapping = aes(x = choco_date, y = choco_sales), colour = "blue", size = 2) +
  84. geom_point(dark_end, mapping = aes(x = choco_date, y = choco_sales), colour = "red", size = 2) +
  85. geom_point(milk_end, mapping = aes(x = choco_date, y = choco_sales), colour = "green", size = 2) +
  86. geom_point(white_end, mapping = aes(x = choco_date, y = choco_sales), colour = "blue", size = 2) +
  87. geom_text(data = df2[1, ], label = "1000", nudge_x = -30, size
  88. <details>
  89. <summary>英文:</summary>
  90. Edit: added image for reference \
  91. https://prnt.sc/Lm5Kk9phys7i
  92. I have two plots that I am trying to combine into one image and then add additional texts.
  93. I am trying to:
  94. 1) Add a blue background to the title bar
  95. 2) Add a paragraph of text above each plot (two blocks of text - one above each individual plot title)
  96. I would prefer the solution to look nice in RMarkdown html output, but a .png would be fine as well.

library(tidyverse)
library(ggrepel)
library(ggtext)
library(lubridate)
library(scales)
library(cowplot)

#plot 1 data
date <- as.Date(c("2019-09-01","2019-10-01","2019-11-01","2019-12-01",
"2020-01-01","2020-02-01","2020-03-01","2020-04-01"))
year <- c(2019,2019,2019,2019,2020,2020,2020,2020)
month <- c("Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar", "Apr")
sales <- c(100,200,600,200,100,100,800,100)
df <- data.frame(date,year,month,sales)
df <- add_row(df, date = as.Date("2019-08-15"), year = 2019, month = "Aug", sales = NA)
df <- add_row(df, date = as.Date("2020-04-15"), year = 2020, month = "Apr", sales = NA)
df <- df %>% arrange(date)

#plot 1 without facet years
data_start <- df %>% filter(row_number()==2)
data_mid <- df %>% filter(row_number()==5)
data_end <- df %>% filter(row_number()==9)

p1 <- ggplot(df, aes(x = date, y = sales)) +
geom_line() +
scale_x_date(
date_labels = "%b",
date_breaks = "month",
expand = c(0, 0)) +
theme_bw() +
theme(
strip.placement = "outside",
strip.background = element_rect(fill = NA, colour = NA),
panel.spacing = unit(0, "lines"),
axis.line = element_line(colour = "gray"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.title.x = element_text(colour = "gray"),
axis.title.y = element_text(hjust=1, colour = "gray"),
axis.text.x = element_text(colour = "gray"),
axis.text.y = element_text(colour = "gray"),
strip.text.x = element_text(colour = "gray", size = 9),
plot.title = element_markdown(size = 14),
plot.subtitle = element_text(size=10, colour = "#818589"),
plot.margin = margin(0,0,0,0, "cm")) + #margins: (top,right,bottom,left)
geom_point(data_start, mapping=aes(x=date,y=sales), colour="blue", size=2) +
geom_point(data_mid, mapping=aes(x=date,y=sales), colour="blue", size=2) +
geom_point(data_end, mapping=aes(x=date,y=sales), colour="blue", size=2) +
geom_text(data=subset(df, year == 2019 & month == "Sep"),
aes(date,sales,label=sales),
position = position_dodge(width = 1),
vjust = -1, hjust=1,size = 5) +
geom_text(data=subset(df, year == 2019 & month == "Dec"),
aes(date,sales,label=sales),
position = position_dodge(width = 1),
vjust = 2, size = 5) +
geom_text(data=subset(df, year == 2020 & month == "Apr"),
aes(date,sales,label=sales),
position = position_dodge(width = 1),
vjust = 0, hjust=0,size = 5) +
geom_text_repel(data=df[5, ], label="Total number of dollars circulation", nudge_y=500, nudge_x=10, color = "blue") +
labs(title = "Chocolate sales",
y = "Number of Units",
x = "2019 2020")

#plot 2
df2 <- df %>% filter(date == "2020-03-01" | date == "2020-04-04")

choco_date <- as.Date(c("2021-01-01","2021-01-01","2021-01-01",
"2022-01-01","2022-01-01","2022-01-01"))
choco_type <- c("Dark", "Milk", "White","Dark", "Milk", "White")
choco_sales <- c(1000,600,100,800,400,200)
df2 <- data.frame(choco_date,choco_type,choco_sales)

dark_start <- df2 %>% filter(choco_type == "Dark" & choco_date == "2021-01-01")
milk_start <- df2 %>% filter(choco_type == "Milk" & choco_date == "2021-01-01")
white_start <- df2 %>% filter(choco_type == "White" & choco_date == "2021-01-01")
dark_end <- df2 %>% filter(choco_type == "Dark" & choco_date == "2022-01-01")
milk_end <- df2 %>% filter(choco_type == "Milk" & choco_date == "2022-01-01")
white_end <- df2 %>% filter(choco_type == "White" & choco_date == "2022-01-01")

p2 <- df2 %>% ggplot(aes(x=choco_date, y=choco_sales, color = choco_type)) +
geom_line() +
geom_point(dark_start, mapping=aes(x=choco_date,y=choco_sales), colour="red", size=2) +
geom_point(milk_start, mapping=aes(x=choco_date,y=choco_sales), colour="green", size=2) +
geom_point(white_start, mapping=aes(x=choco_date,y=choco_sales), colour="blue", size=2) +
geom_point(dark_end, mapping=aes(x=choco_date,y=choco_sales), colour="red", size=2) +
geom_point(milk_end, mapping=aes(x=choco_date,y=choco_sales), colour="green", size=2) +
geom_point(white_end, mapping=aes(x=choco_date,y=choco_sales), colour="blue", size=2) +
geom_text(data=df2[1, ], label = "1000", nudge_x = -30, size = 3) +
geom_text(data=df2[2, ], label = "600", nudge_x = -25, size = 3) +
geom_text(data=df2[3, ], label = "100", nudge_x = -25, size = 3)+
geom_text(data=df2[4, ], label = "800 Dark", nudge_x = 105, size = 3) +
geom_text(data=df2[5, ], label = "400 Milk", nudge_x = 100, size = 3) +
geom_text(data=df2[6, ], label = "200 White", nudge_x = 110, size = 3) +
geom_text(data=df2[1, ], label = "- 20%", nudge_x = 200, nudge_y = -40, size = 3) +
geom_text(data=df2[2, ], label = "- 33%", nudge_x = 200, nudge_y = -40, size = 3) +
geom_text(data=df2[3, ], label = "100%", nudge_x = 200, nudge_y = 100, size = 3)+
scale_x_date(breaks = as.Date(c("2021-01-01", "2022-01-01")),
labels = c("Last Month \n Mar-20", "This Month \n Apr-20")) +
labs(title = "Number of chocolate bars sold",
subtitle = "# OF UNITS % CHANGE") +
theme_bw() +
theme(strip.placement = "outside",
strip.background = element_rect(fill=NA,colour="grey50"),
panel.spacing=unit(0,"cm"),
axis.line.y = element_blank(),
axis.line.x = element_line(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
aspect.ratio = 1,
plot.title = element_text(color = "black", size = 14),
plot.subtitle = element_text(color = "dark gray", size = 8),
legend.position = "none",
plot.margin = unit(c(0,0,0,0), "lines")) + #margins: (top,right,bottom,left)
coord_fixed(ratio = 1, clip = 'off') +
labs(x="",y="")

#cowplot
img_final <- plot_grid(p1, p2, rel_widths = c(2,1))
img_final

#title
title <- ggdraw() +
draw_label(
"Miles per gallon decline with displacement and horsepower",
fontface = 'bold',
x = 0,
hjust = 0
) +
theme(
# add margin on the left of the drawing canvas,
# so title is aligned with left edge of first plot
plot.margin = margin(0, 0, 0, 7)
)
plot_grid(
title, img_final,
ncol = 1,

rel_heights values control vertical title margins

rel_heights = c(0.1, 1)
)

  1. </details>
  2. # 答案1
  3. **得分**: 3
  4. 你可以在一个单独的图中生成安排的每个面板,并使用 `patchwork` 安排它们:
  5. ```R
  6. st1 <- paste(rep(paste(paste(rep("green", 8), collapse = " "), "\n"), 5), collapse = "")
  7. st2 <- paste(rep(paste(paste(rep("soccer ball", 5), collapse = " "), "\n"), 5), collapse = "")
  8. # 标题面板
  9. title_panel <- ggplot() + labs(title = "我的标题") +
  10. theme(plot.background = element_rect(fill = "#00A2E8"),
  11. panel.background = element_blank(),
  12. plot.title = element_text(size = 20, vjust = -1.5))
  13. # 段落面板
  14. paragraph_panel_1 <- ggplot() + theme_void() +
  15. labs(title = '人们喜欢<span style = color:"red">红色</span>',
  16. subtitle = st1) +
  17. theme(plot.title = element_markdown(size = 20))
  18. paragraph_panel_2 <- ggplot() + theme_void() +
  19. labs(title = '在<span style = color:"purple">比萨饼</span>上放奶酪',
  20. subtitle = st2) +
  21. theme(plot.title = element_markdown(size = 20))
  22. # 使用 patchwork 安排
  23. library(patchwork)
  24. title_panel + paragraph_panel_1 + paragraph_panel_2 + p1 + p2 +
  25. plot_layout(design = c(area(t = 1, l = 1, b = 1, r = 4),
  26. area(t = 2, l = 1, b = 2, r = 2),
  27. area(t = 2, l = 3, b = 2, r = 4),
  28. area(t = 3, l = 1, b = 12, r = 2),
  29. area(t = 3, l = 3, b = 12, r = 3))

如何使用cowplot和ggplot排列多个图。

英文:

You could generate each panel of your arrangement in a separate plot and arrange them with patchwork:

  1. st1 &lt;- paste(rep(paste(paste(rep(&quot;green&quot;, 8), collapse = &quot; &quot;), &quot;\n&quot;), 5), collapse = &quot;&quot;)
  2. st2 &lt;- paste(rep(paste(paste(rep(&quot;soccer ball&quot;, 5), collapse = &quot; &quot;), &quot;\n&quot;), 5), collapse = &quot;&quot;)
  3. # Title panel
  4. title_panel &lt;- ggplot() + labs(title = &quot;My title&quot;) +
  5. theme(plot.background = element_rect(fill = &quot;#00A2E8&quot;),
  6. panel.background = element_blank(),
  7. plot.title = element_text(size = 20, vjust = -1.5))
  8. # Paragraph panels
  9. paragraph_panel_1 &lt;- ggplot() + theme_void() +
  10. labs(title = &#39;People like **the color** &lt;span style = color:&quot;red&quot;&gt;red&lt;/span&gt;&#39;,
  11. subtitle = st1) +
  12. theme(plot.title = element_markdown(size = 20))
  13. paragraph_panel_2 &lt;- ggplot() + theme_void() +
  14. labs(title = &#39;Put **cheese** on &lt;span style = color:&quot;purple&quot;&gt;pizza&lt;/span&gt;&#39;,
  15. subtitle = st2) +
  16. theme(plot.title = element_markdown(size = 20))
  17. # Arrange with patchwork
  18. library(patchwork)
  19. title_panel + paragraph_panel_1 + paragraph_panel_2 + p1 + p2 +
  20. plot_layout(design = c(area(t = 1, l = 1, b = 1, r = 4),
  21. area(t = 2, l = 1, b = 2, r = 2),
  22. area(t = 2, l = 3, b = 2, r = 4),
  23. area(t = 3, l = 1, b = 12, r = 2),
  24. area(t = 3, l = 3, b = 12, r = 3)))

如何使用cowplot和ggplot排列多个图。

huangapple
  • 本文由 发表于 2023年2月24日 06:43:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551081.html
匿名

发表评论

匿名网友

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

确定