简体   繁体   English

ggplot:使用RColorBrewer软件包填充Boxplots +使用R Cowxlot软件包中的plot_grid()绘制Boxplots

[英]ggplot: Fill Boxplots Using the Package RColorBrewer + Plot Boxplots Using plot_grid() in the Cowplot Package in R

Overview 总览

I have a two data frames called 'ANOVA.Dataframe.1' and 'ANOVA.Dataframe.2' (see below). 我有两个数据框,分别称为“ ANOVA.Dataframe.1”“ ANOVA.Dataframe.2” (如下所示)。

For this project, I have two aims: 对于这个项目,我有两个目标:

(1) Fill the boxplots using the package RColorBrewer ; (1)使用软件包RColorBrewer填充盒图;

(2) Plot the boxplots using the package Cowplot (2)使用Cowplot软件包绘制箱线图

Issues 问题

  • In the first instance, I generated two objects called New.filled.Boxplot.obs1.Canopy.Urban , and New.filled.Boxplot.obs2.Canopy.Urban , and I added the function (ie function 1 or function 2 - see R-code below) that generated the boxplots with the function scale_fill_brewer(palette="Dark2") found in the RColorBrewer package by following this example to produce the desired results. 在第一个实例中,我生成了两个对象,分别称为New.filled.Boxplot.obs1.Canopy.UrbanNew.filled.Boxplot.obs2.Canopy.Urban ,然后添加了函数(即函数1或函数2-参见R下面的代码) ,通过遵循此示例可以生成具有在RColorBrewer程序包中找到的函数scale_fill_brewer(palette =“ Dark2”)箱线图 ,以产生所需的结果。 However, my code did not work (see image below). 但是,我的代码不起作用(请参见下图)。

  • When I plotted the boxplots using plot_grid() in the Cowplot package , the positioning of the label headings (ie A: Observation Period 1 or B: Observation Period 2 - see image below) overlay both boxplots (see image below). 当我在Cowplot包中使用plot_grid()绘制箱线图时,标签标题的位置(即A:观察期1或B:观察期2-参见下图)覆盖了两个箱图(参见下图)。 Is there a method to manipulate the plotting space in the plot window so the boxplots are very slightly smaller and the label headings are positioned above each boxplot instead? 是否有一种方法可以操纵打印窗口中的打印空间,使箱形图略小,而标签标题却位于每个箱形图上方?

If anyone can be of assistance, I would be deeply appreciative. 如果有人可以提供帮助,我将非常感激。

R-Code R代码

library(tidyverse)
library(wrapr)
library(RColorBrewer)
library(dplyr)

# Open Colour Brewer Paletter Options
display.brewer.all()


## Function 1 to produce the boxplots for Dataframe 1

Boxplot.obs1.Canopy.Urban<-ANOVA.Dataframe.1 %.>%
                                   ggplot(data = ., aes(
                                   x = Urbanisation_index,
                                   y = Canopy_Index,
                                   group = Urbanisation_index,
                                   )) +
                                   stat_boxplot(
                                   geom = 'errorbar',
                                   width = .25
                                   ) +
                                   geom_boxplot(notch=T) +
                                   geom_line(
                                   data = group_by(., Urbanisation_index) %>%
                                   summarise(
                                   bot = min(Canopy_Index),
                                   top = max(Canopy_Index)
                                    ) %>%
                                   gather(pos, val, bot:top) %>% 
                                   select(
                                   x = Urbanisation_index,
                                   y = val
                                   ) %>%
                                   mutate(gr = row_number()) %>%
                                   bind_rows(
                                   tibble(
                                   x = 0,
                                   y = max(.$y) * 1.15,
                                   gr = 1:8
                                   )
                                   ),
                                  aes(
                                  x = x,
                                  y = y,
                                  group = gr
                                  )) +
                                  theme_light() +
                                  theme(panel.grid = element_blank()) +
                                  coord_cartesian(
                                  xlim = c(min(.$Urbanisation_index) - .5, max(.$Urbanisation_index) + .5),
                                  ylim = c(min(.$Canopy_Index) * .95, max(.$Canopy_Index) * 1.05)
                                   ) +
                                 ylab('Company Index (%)') +
                                 xlab('Urbanisation Index')

 ## Change the colours of the boxplot
New.filled.Boxplot.obs1.Canopy.Urban <- Boxplot.obs1.Canopy.Urban + scale_fill_brewer(palette="Dark2")

## Function 2 to produce the boxplots for Dataframe 2
Boxplot.obs2.Canopy.Urban<-ANOVA.Dataframe.2 %.>%
                                   ggplot(data = ., aes(
                                   x = Urbanisation_index,
                                   y = Canopy_Index,
                                   group = Urbanisation_index,
                                   )) +
                                   stat_boxplot(
                                   geom = 'errorbar',
                                   width = .25
                                   ) +
                                   geom_boxplot(notch=T) +
                                   geom_line(
                                   data = group_by(., Urbanisation_index) %>%
                                   summarise(
                                   bot = min(Canopy_Index),
                                   top = max(Canopy_Index)
                                    ) %>%
                                   gather(pos, val, bot:top) %>% 
                                   select(
                                   x = Urbanisation_index,
                                   y = val
                                   ) %>%
                                   mutate(gr = row_number()) %>%
                                   bind_rows(
                                   tibble(
                                   x = 0,
                                   y = max(.$y) * 1.15,
                                   gr = 1:8
                                   )
                                   ),
                                  aes(
                                  x = x,
                                  y = y,
                                  group = gr
                                  )) +
                                  theme_light() +
                                  theme(panel.grid = element_blank()) +
                                  coord_cartesian(
                                  xlim = c(min(.$Urbanisation_index) - .5, max(.$Urbanisation_index) + .5),
                                  ylim = c(min(.$Canopy_Index) * .95, max(.$Canopy_Index) * 1.05)
                                   ) +
                                 ylab('Company Index (%)') +
                                 xlab('Urbanisation Index')


## Change the colours of the boxplot

 New.filled.Boxplot.obs2.Canopy.Urban<- Boxplot.obs2.Canopy.Urban + scale_fill_brewer(palette="Dark2")

library(cowplot)

## Open New plot window
dev.new()

Combined_boxplot_Obs<-plot_grid(New.filled.Boxplot.obs1.Canopy.Urban, 
                                New.filled.Boxplot.obs2.Canopy.Urban, 
                                labels=c("A: Observation Period 1",
                                         "B: Observation Period 2"),
                                label_fontface="bold",
                                label_fontfamily="Times New Roman",
                                label_size=12,
                                align="v",
                                ncol=2, nrow=1)

Combined_boxplot_Obs

This R-code produces these plots: 此R代码产生以下图:

在此处输入图片说明

Data frame 1 数据框1

structure(list(Urbanisation_index = c(2, 2, 4, 4, 3, 3, 4, 4, 
4, 2, 4, 3, 4, 4, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 
2, 2, 2, 4, 4, 3, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 4, 4, 4, 
4, 4, 4, 4), Canopy_Index = c(65, 75, 55, 85, 85, 85, 95, 85, 
85, 45, 65, 75, 75, 65, 35, 75, 65, 85, 65, 95, 75, 75, 75, 65, 
75, 65, 75, 95, 95, 85, 85, 85, 75, 75, 65, 85, 75, 65, 55, 95, 
95, 95, 95, 45, 55, 35, 55, 65, 95, 95, 45, 65, 45, 55)), row.names = c(NA, 
-54L), class = "data.frame")

Dataframe 2 数据框2

structure(list(Urbanisation_index = c(2, 2, 4, 4, 3, 3, 4, 4, 
4, 3, 4, 4, 4, 4, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 
2, 2, 2, 4, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 4, 4, 4, 4, 4, 4, 4
), Canopy_Index = c(5, 45, 5, 5, 5, 5, 45, 45, 55, 15, 35, 45, 
5, 5, 5, 5, 5, 5, 35, 15, 15, 25, 25, 5, 5, 5, 5, 5, 5, 15, 25, 
15, 35, 25, 45, 5, 25, 5, 5, 5, 5, 55, 55, 15, 5, 25, 15, 15, 
15, 15)), row.names = c(NA, -50L), class = "data.frame")
  1. The scale_fill_brewer(palette = "Dark2") does not work in your example, because you don not provide a fill -aesthetics. 在您的示例中, scale_fill_brewer(palette = "Dark2")不起作用,因为您未提供fill美学。 You need to add that to your boxplot. 您需要将其添加到箱线图中。
  2. The labels in plot_grid are meant to be single letters (or at least short) for reference in a caption. plot_grid中的标签应为单个字母(或至少简短),以供字幕参考。 For your purpose I'd recommend to use titles in the original plots. 为了您的目的,建议在原始图中使用标题。
  3. Your code is quite hard to read and you can reduce the number of packages used. 您的代码很难阅读,可以减少使用的软件包数量。 I also shortend the name as they are not so important here and make everything more verbose. 我还缩短了名称,因为它们在这里并不那么重要,并使所有内容都变得更加冗长。
  4. I would calculate special statistics not inside the ggplot -call, but before that in a separate data.frame . 我将不在ggplot内而是在单独的data.frame计算特殊统计信息。

Packages 配套

library(tidyverse)
library(cowplot)

1st Boxplots 第一箱图

# Calculate special positions for lines first
mydf.1.lines <- mydf.1 %>% 
  group_by(Urbanisation) %>%
  summarise(bot = min(Canopy), top = max(Canopy)) %>%
  gather(pos, val, bot:top) %>% 
  select(x = Urbanisation, y = val) %>%
  mutate(gr = row_number()) %>%
  bind_rows(tibble(x = 0, y = max(.$y) * 1.15, gr = 1:8))

# Calculate plot limits 
xlimits.1 <- with(mydf.1, c(min(Urbanisation) - .5, max(Urbanisation) + .5))
ylimits.1 <- with(mydf.1, c(min(Canopy) * .95, max(Canopy) * 1.05))

Boxplot.1 <- 
  ggplot(mydf.1, aes(Urbanisation, Canopy, group = Urbanisation)) +
  stat_boxplot(geom = 'errorbar', width = .25) +
  # Add a fill aesthetics in the geom_boxplot - call:
  geom_boxplot(aes(fill = factor(Urbanisation)), notch = TRUE) +
  geom_line(data = mydf.1.lines, 
            aes(x, y, group = gr)) +
  theme_light() +
  theme(panel.grid = element_blank()) +
  coord_cartesian(xlim = xlimits.1, ylim = ylimits.1) +
  ylab('Company Index (%)') +
  xlab('Urbanisation Index')

New.filled.Boxplot.1 <- Boxplot.1 + scale_fill_brewer(palette = "Dark2")

2nd Boxplots 第二箱图
Analogous to the 1st: 类似于第一个:

mydf.2.lines <- mydf.2 %>% 
  group_by(Urbanisation) %>%
  summarise(bot = min(Canopy), top = max(Canopy)) %>%
  gather(pos, val, bot:top) %>% 
  select(x = Urbanisation, y = val) %>%
  mutate(gr = row_number()) %>%
  bind_rows(tibble(x = 0, y = max(.$y) * 1.15, gr = 1:8))

xlimits.2 <- with(mydf.2, c(min(Urbanisation) - .5, max(Urbanisation) + .5))
ylimits.2 <- with(mydf.2, c(min(Canopy) * .95, max(Canopy) * 1.05))

Boxplot.2 <- 
  ggplot(mydf.2, aes(Urbanisation, Canopy, group = Urbanisation)) +
  stat_boxplot(geom = 'errorbar', width = .25) +
  geom_boxplot(aes(fill = factor(Urbanisation)), notch = TRUE) +
  geom_line(data = mydf.2.lines, 
            aes(x, y, group = gr)) +
  theme_light() +
  theme(panel.grid = element_blank()) +
  coord_cartesian(xlim = xlimits.2, ylim = ylimits.2) +
  ylab('Company Index (%)') +
  xlab('Urbanisation Index')

New.filled.Boxplot.2 <- Boxplot.2 + scale_fill_brewer(palette = "Dark2")

Combine Plots 组合图

plot_grid(New.filled.Boxplot.1 + ggtitle("A: Observation Period 1"),
          New.filled.Boxplot.2 + ggtitle("B: Observation Period 2"), 
          align = "v",
          ncol = 2,
          nrow = 1)

Or with the correct specification of the title and hjust (Thanks to Claus Wilke): 或使用正确的标题和标题规范(感谢克劳斯·威尔克):

plot_grid(New.filled.Boxplot.1 + ggtitle(""),
          New.filled.Boxplot.2 + ggtitle(""), 
          align = "v",
          labels = c("A: Observation Period 1", "B: Observation Period 2"),
          hjust = 0, 
          label_x = 0.01,
          ncol = 2,
          nrow = 1)

在此处输入图片说明

Boxplot outside of plot 情节外的箱线图
The problem here is that the notches are outside the hinges. 这里的问题是,凹口在铰链之外。 If you set notch = FALSE for the second plot (or both) it is no problem. 如果您为第二个图(或两个图)都设置了notch = FALSE ,则没有问题。 Alternatively you could also manipulate the ylimits as you already suggested. 或者,您也可以按照建议操作ylimit。 The function with simply specifies the data.frame (mydf.2) in which the following columns can be found. 该功能with简单地指定data.frame (mydf.2),其中以下的列中可以找到。 Thus the call 因此,电话

ylimits.2 <- with(mydf.2, c(min(Canopy) * .95, max(Canopy) * 1.05))

is equivalent to 相当于

ylimits.2 <- c(min(mydf.2$Canopy) * .95, max(mydf.2$Canopy) * 1.05)

and you could for example specify 例如,您可以指定

ylimits.2 <- c(-20, max(mydf.2$Canopy) * 1.05)

This would set the lower limit to -20 and the upper limit to 1.05 times the maximum of the Canopy index in the second dataframe. 这会将下限设置为-20,将上限设置为第二个数据帧中Canopy索引最大值的1.05倍。

Data 数据

mydf.1 <- 
  structure(list(Urbanisation = c(2, 2, 4, 4, 3, 3, 4, 4, 4, 2, 4, 3, 4, 4, 1, 
                                  1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 
                                  2, 2, 4, 4, 3, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 
                                  2, 1, 4, 4, 4, 4, 4, 4, 4), 
                 Canopy = c(65, 75, 55, 85, 85, 85, 95, 85, 85, 45, 65, 75, 75, 
                            65, 35, 75, 65, 85, 65, 95, 75, 75, 75, 65, 75, 65, 
                            75, 95, 95, 85, 85, 85, 75, 75, 65, 85, 75, 65, 55, 
                            95, 95, 95, 95, 45, 55, 35, 55, 65, 95, 95, 45, 65, 
                            45, 55)), 
            row.names = c(NA, -54L), class = "data.frame")

mydf.2 <- 
  structure(list(Urbanisation = c(2, 2, 4, 4, 3, 3, 4, 4, 4, 3, 4, 4, 4, 4, 1, 
                                  1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 
                                  2, 2, 4, 4, 3, 2, 2, 2, 2, 2, 2, 1, 1, 4, 4, 
                                  4, 4, 4, 4, 4), 
                 Canopy = c(5, 45, 5, 5, 5, 5, 45, 45, 55, 15, 35, 45, 5, 5, 5, 
                            5, 5, 5, 35, 15, 15, 25, 25, 5, 5, 5, 5, 5, 5, 15, 
                            25, 15, 35, 25, 45, 5, 25, 5, 5, 5, 5, 55, 55, 15, 
                            5, 25, 15, 15, 15, 15)), 
            row.names = c(NA, -50L), class = "data.frame")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 hist() 和 boxplot() object 转换为 ggplot() object 以使用 R 中的 Cowplot Package 中的 plot_grid() 对齐我的绘图 - Converting a hist() and boxplot() object into a ggplot() object to align my plots using plot_grid() in the Cowplot Package in R Cowplot程序包:在R中使用plot_grid()将许多图安排成一个图后,如何垂直向下调整图例 - Cowplot Package: How to align legends vertically downwards after arranging many plots into one plot using plot_grid() in R 使用 cowplot 的 plot_grid 放置图例 - Placement of legend using plot_grid of cowplot 如何使用`cowplot` package中的`plot_grid`对齐包含在最后一列图中的图例? - How to align legends included in the plots of the last column of an arrange of plots using `plot_grid` from `cowplot` package? 如何使用ggplot2在R中绘制箱形图的比较 - How to plot a comparison of boxplots in R using ggplot2 如何使用 plot_grid() 将两个图组合在一起在“cowplot”中有一个单一的网格背景? 在 R 语言 - How to have a single grid background in "cowplot" using plot_grid() combining two plots together? in R language 如何使用R中的plot_grid()函数绘制几个seqplots(TraMineR包)? - How to plot several seqplots (TraMineR package) using plot_grid() function in R? 使用R中的基本图形绘制一系列箱形图 - Plot series of boxplots using base graphics in R Cowplot plot_grid 从 ggplot facet strips 中删除空间 - Cowplot plot_grid remove space from ggplot facet strips Cowplot plot_grid() :图中的标题 - Cowplot plot_grid() : Title in plot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM