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

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

How to arrange multiple plots in using cowplot and ggplot

问题

以下是代码的翻译部分:

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

# 绘制1数据
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)

# 绘制1无年度分面
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")

# 绘制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

<details>
<summary>英文:</summary>

Edit: added image for reference \
https://prnt.sc/Lm5Kk9phys7i

I have two plots that I am trying to combine into one image and then add additional texts. 
I am trying to: 
1) Add a blue background to the title bar
2) Add a paragraph of text above each plot (two blocks of text - one above each individual plot title)

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)
)


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

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

确定