在ggplot()中处理视觉图形:geom_bar()+使用R

时间:2019-02-06 03:50:23

标签: r ggplot2 cowplot

概述

我有两个称为'mydf1'和'mydf2'的数据框(请参见下文),我想使用geom_bar()包中的ggplot产生两个条形图。理想情况下,我想用不同的颜色填充每个小节以匹配图例。

在绘制两个条形图之后,我想使用plot_grid()软件包使用cowplot在相同的绘制窗口中对其进行绘制。

我对如何在其他命令中正确放置geom_bar()中的美观感到困惑。首先,我生成了所需结果1 ,它可以很好地可视化数据。

但是,我操纵了这段代码以生成带有图例的各个彩色条。如所需结果2(如下所示)所示运行新代码后,在'所需绘图1'中看到的条形图的某些可视组件丢失了,例如:

  1. 带标签的单个条形图显示了Mean_Canopy_Index的值
  2. “ New Times Roman”字体中每个条形图的标题
  3. “新时代罗马”字体中x(城市索引)和y(中冠指数)标签的正确格式。
  4. 每个小节的标题都用斜体字表示栎属栎(Quercus petraea)

如果有人对如何将所需的图1和所需的图2结合在一起有任何建议,我将非常感激。

所需结果1

理想情况下,我想生成与'所需结果1'中所见的相同的条形图,但是要包含在'所需结果2'中所见的不同的色条和图例。 strong>

我制作了两个条形图,如'期望的结果1'(如下所示)所示,其中显示了 R-code,如下所示的期望的结果1 所需结果1 所需结果2 的R代码之间唯一的区别是geom_bar函数中的命令。

R代码用于生成“所需结果1

Canopy_Urban_2 <- ggplot(mydf2, 
                         aes(x = Urbanisation_index, y = Canopy_Index_Mean)) +
  geom_bar(stat="identity", color="steelblue", fill="steelblue", width=0.5) +
  labs(title=plottitle2,
       x="Categories of Urbansiation Index",
       y="Mean Canopy Index (%)",
       size=13)+
  geom_text(aes(label = sprintf("%.1f", Canopy_Index_Mean)),
            vjust=1.6,
            color="white",
            size=3.5)+
  theme(axis.text.x = element_text(angle = 1, hjust = 1), 
        text = element_text(size=12, family = "Times New Roman")) + 
  theme(panel.background = element_blank(), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.border = element_blank()) + 
  theme(axis.line.x = element_line(color="black", size = 0.8),
        axis.line.y = element_line(color="black", size = 0.8))

所需结果1的图 enter image description here

所需结果2的条形图

我使用下面显示的R代码​​制作了“需求结果2”

enter image description here

所需结果2的R代码:

包装

library(ggplot2)
library(cowplot)
library(RColorBrewer)

将两个条形图的标题变为斜体

##Produce an object called Quercus petraea
word='Quercus petraea'

##Produce an object for the main title with the italicised species name
plottitle2 <- substitute('Barplot of Mean Canopy Index (%) for Categories of the Urbanisation Index for ' ~ italic(x), 
                         list(x=word))

##Importing fonts onto the hard drive
library(extrafont)
font_import()

数据框1的Barplot 1

barplot1 <- ggplot(mydf.1, 
                   aes(x = Urbanisation_index, y = Canopy_Index_Mean)) +
  geom_bar(stat="identity", aes(fill = factor(Urbanisation_index)), width=0.5,
  labs(title=plottitle2,
       x="Categories of Urbansiation Index",
       y="Mean Canopy Index (%)",
       size=13)+
  geom_text(aes(label = sprintf("%.1f", Canopy_Index_Mean)),
            vjust=1.6,
            color="white",
            size=3.5)+
  theme(axis.text.x = element_text(angle = 1, hjust = 1), 
        text = element_text(size=12, family = "Times New Roman")) + 
  theme(panel.background = element_blank(), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.border = element_blank()) + 
  theme(axis.line.x = element_line(color="black", size = 0.8),
        axis.line.y = element_line(color="black", size = 0.8)))

填充栏并为Boxplot 1添加图例

Barplot1.1 <- barplot1 +
  scale_fill_brewer(palette = "Dark2", 
                    name="Urbanisation Index", 
                    breaks=c("1", "2", "3", "4"),
                    labels=c("Urban", "Suburban", "Village/Small Town", "Rural"))

数据名人2的原型2

barplot2 <- ggplot(mydf.2, 
                   aes(x = Urbanisation_index, y = Canopy_Index_Mean)) +
  geom_bar(stat="identity", aes(fill = factor(Urbanisation_index)), width=0.5,
  labs(title=plottitle2,
       x="Categories of Urbansiation Index",
       y="Mean Canopy Index (%)",
       size=13)+
  geom_text(aes(label = sprintf("%.1f", Canopy_Index_Mean)),
            vjust=1.6,
            color="white",
            size=3.5)+
  theme(axis.text.x = element_text(angle = 1, hjust = 1), 
        text = element_text(size=12, family = "Times New Roman")) + 
  theme(panel.background = element_blank(), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.border = element_blank()) + 
  theme(axis.line.x = element_line(color="black", size = 0.8),
        axis.line.y = element_line(color="black", size = 0.8)))

填充栏并为Boxplot 2添加图例

 Barplot2.2 <- barplot2 +
   scale_fill_brewer(palette = "Dark2", 
                     name="Urbanisation Index", 
                     breaks=c("1", "2", "3", "4"),
                     labels=c("Urban", "Suburban", "Village/Small Town", "Rural"))

使用plot_grid()组合Barplot 1和Barplot 2

plot_grid(Barplot1.1 + ggtitle("A: Observation Period 1"),
          Barplot2.2 + ggtitle("B: Observation Period 2"), 
          align = "v",
          label_fontface="bold",
          label_fontfamily="Times New Roman",
          ncol = 1,
          nrow = 2)

数据

mydf.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_Mean = 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_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_Mean = 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")

0 个答案:

没有答案