概述
我有两个称为'mydf1'和'mydf2'的数据框(请参见下文),我想使用geom_bar()
包中的ggplot
产生两个条形图。理想情况下,我想用不同的颜色填充每个小节以匹配图例。
在绘制两个条形图之后,我想使用plot_grid()
软件包使用cowplot
在相同的绘制窗口中对其进行绘制。
我对如何在其他命令中正确放置geom_bar()
中的美观感到困惑。首先,我生成了所需结果1 ,它可以很好地可视化数据。
但是,我操纵了这段代码以生成带有图例的各个彩色条。如所需结果2(如下所示)所示运行新代码后,在'所需绘图1'中看到的条形图的某些可视组件丢失了,例如:>
如果有人对如何将所需的图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))
所需结果2的条形图
我使用下面显示的R代码制作了“需求结果2” 。
所需结果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)))
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)))
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")