R语言绘制地图背景颜色设置 - 如何将地图背景设置为白色或透明
R语言绘制地图背景颜色设置 - 如何将地图背景设置为白色或透明
本文介绍了如何使用 R 语言中的 gplot 函数绘制地图,并提供详细步骤来设置地图背景颜色为白色或透明。此外,文章还提供了示例代码和解释,方便读者理解和应用。
代码示例:
library(raster)
library(ggplot2)
library(rasterVis)
library(ggplot2)
library(RImagePalette)
myfigure <- jpeg::readJPEG('testtif.jpeg') #选择你想上传的图片,注意是jpeg格式
display_image(myfigure)
mycolors <- image_palette(myfigure, n=30)
scales::show_col(mycolors)
map <- raster('eo_base_2020_clean_geo.tif')
gplot(map, maxpixels = 5e5) +
geom_tile(aes(fill = value)) +
facet_wrap(~ variable) +
# scale_fill_gradient(low = 'white', high = 'black') +
coord_equal()
polygon_layer <- shapefile('World_Continents.shp')
clipped_raster <- crop(map, extent(polygon_layer))
clipped_raster <- mask(clipped_raster, polygon_layer)
gplot(clipped_raster, maxpixels = 3e5) +
geom_tile(aes(fill = value), na.rm = T) +
scale_fill_manual(values=mycolors)+
# scale_fill_image(image = myfigure, discrete = F)+
# scale_fill_viridis_b()+
coord_equal()
library(terra)
clipped_raster1 <- rast(clipped_raster)
library(viridis)
gplot(clipped_raster1, maxpixels = 4e5) +
geom_tile(aes(fill = value), na.rm = T) +
scale_fill_distiller(name = NULL,palette = 'RdYlGn', direction = 1,
breaks= seq(0, 300, 50),
limits = c(50, 260))+
# scale_fill_manual(values=mycolors)+
# scale_fill_image(image = myfigure, discrete = F)+
# scale_fill_viridis_c()+
# scale_fill_viridis(discrete=F,
# option = 'G')+
coord_equal()
# 设置地图背景为白色
```r
gplot(map, maxpixels = 5e5) +
geom_tile(aes(fill = value)) +
facet_wrap(~ variable) +
coord_equal() +
theme(panel.background = element_blank(),
plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
# 设置地图背景为透明
gplot(map, maxpixels = 5e5) +
geom_tile(aes(fill = value)) +
facet_wrap(~ variable) +
coord_equal() +
theme(panel.background = element_rect(fill = 'transparent'),
plot.background = element_rect(fill = 'transparent'),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
解释:
theme()函数用于修改 ggplot2 图的主题,可以控制图形的各个元素,包括背景颜色、网格线、边框等。panel.background和plot.background参数用于设置面板和绘图的背景颜色。element_blank()用于设置背景为空白,element_rect(fill = 'transparent')用于设置背景为透明。
总结:
本文介绍了如何使用 theme() 函数修改 ggplot2 图的主题,并提供详细步骤来设置地图背景颜色为白色或透明。通过修改 theme() 函数的其他参数,你还可以调整图形的边界、标题、标签等元素,进一步自定义图形的外观。
原文地址: https://www.cveoy.top/t/topic/b7AW 著作权归作者所有。请勿转载和采集!