ggplot通过循环与ggtitle混合样式

时间:2019-03-15 09:32:36

标签: r loops ggplot2 italics ggtitle

我创建了一个简单的循环,使用形状文件制作了不同植物物种的分布图。数据集包含以下变量: 物种 (带有作者的全名),纬度,经度(收集位置)以及属别,物种1,author1。

当我保存地图时,会根据 种类 列将正确的标题添加到每个地图中

ggtitle(paste(strwrap(i, width = 60), collapse = "\n" ))

但是应该使用特定的样式:斜体的属和种,使用正常字体的作者。

如何在循环中格式化图标题?

for (i in unique(data$species)) { 
data_map <- ggplot() + 
geom_polygon(data = shp, aes(x = long, y = lat, group = group), 
colour = "black", fill = NA) +
coord_map(xlim = c(56.57, 56.72),ylim = c(-10.32, -10.5))+
geom_point(data = data[which(data$species==i),],  
          aes(x = long,  
          y = lat), 
          alpha = 0.6,  
          size = 4, colour="Red") +  
theme_classic() +  
ylab(expression("Latitude ("*degree*")" )) +  
xlab(expression("Longitude ("*degree*")" )) + 
ggtitle(paste(strwrap(i, width = 60), collapse = "\n" ))

ggsave(data_map, file=paste0("data_", i,".png"), width = 14, height = 10,      
units = "cm")
}

0 个答案:

没有答案
相关问题