书接adjustText实践——调整matplotlib散点图标签,避免重复
上文中,matplotlib+adjustText对于我的实例来说并没有起到很好的效果。所以,博主决定在R中利用gglot2+ggrepel绘制,期待效果。
博主不常使用R,在此过程中详细记录每一步骤,以作备忘。
library(openxlsx)
fossils = read.xlsx("D://Work_Space/mahonia/1-化石记录/2023-3-17-Mahonia_fossils_leaf.xlsx")
library(ggplot2)
plot(fossils$Lon, fossils$Lat)
library(gcookbook)
library(dplyr)
ggplot(data = fossils %>% select(Lon, Lat), aes(x=Lon, y=Lat)) + geom_point()
library(openxlsx)
fossils <- read.xlsx("D://Work_Space/mahonia/1-化石记录/2023-3-17-Mahonia_fossils_leaf.xlsx")
simple_fossils <- fossils %>% select(Genus, Name, Epoch, Lon, Lat, Group)
library(ggplot2)
# plot(fossils$Lon, fossils$Lat)
library(gcookbook)
library(dplyr)
ggplot(data = simple_fossils %>% select(Lon, Lat, Group, Epoch), aes(x=Lon, y=Lat, shape=Group, color=Epoch)) + geom_point()
ggplot(data = simple_fossils %>% select(Lon, Lat, Group, Epoch, Name), aes(x=Lon, y=Lat, shape=Group, color=Epoch)) + geom_point() + geom_text(aes(label = Name), size = 2)
library(ggrepel)
ggplot(data = simple_fossils %>% select(Lon, Lat, Group, Epoch, Name), aes(x=Lon, y=Lat, shape=Group, color=Epoch)) + geom_point() + geom_text_repel(aes(label = Name), size = 3)
尝试减小字体
ggplot(data = simple_fossils %>% select(Lon, Lat, Group, Epoch, Name), aes(x=Lon, y=Lat, shape=Group, color=Epoch)) + geom_point() + geom_label_repel(aes(label = Name), size = 1)
ggplot(data = simple_fossils %>% select(Lon, Lat, Group, Epoch, Name), aes(x=Lon, y=Lat, shape=Group, color=Epoch)) + geom_point() + geom_label_repel(aes(label = Name), size = 2, max.overlaps = 30)
效果也不行,还是挤在一起。
world <- map_data("world")
world_map <- ggplot() + geom_map(data = world, map=world, aes(x=long, y=lat, map_id=region),color = "black", fill = "lightgray", size = 0.1)+scale_x_continuous(breaks = seq(-180, 210, 45), labels = function(x){paste0(x, "°")}) +
scale_y_continuous(breaks = seq(-60, 100, 30), labels = function(x){paste0(x, "°")})
world_map
world_map + geom_point(data = simple_fossils %>% select(Lon, Lat, Group, Epoch, Name), aes(x=Lon, y=Lat, shape=Group, color=Epoch))
world_map + geom_point(data = simple_fossils %>% select(Lon, Lat, Group, Epoch, Name), aes(x=Lon, y=Lat, shape=Group, color=Epoch)) + geom_label_repel(data=simple_fossils %>% select(Lon, Lat, Group, Epoch, Name), aes(x=Lon, y=Lat, label=Name, color=Epoch,fontface="italic"), size=2, max.overlaps = 100, box.padding = 0.4, label.padding = 0.1)
经过一番努力,达到的效果比python绘图要好一些。但是在保存图片时却不如python。后面再努努力吧。