alex_bn_lee

导航

【861】Thematic mapping based on R programming

Ref: ggplot2 title : main, axis and legend titles

Ref: ggplot2 标题居中

Ref: ggplot的3种去掉图例的方法 (theme(legend.position="none"))

Ref: R绘图:一文了解ggplot2颜色的设置

Ref: R语言绘图|分级色彩地图

Ref: R语言数据地图——全球填色地图

Ref: R 语言画中国地图


Example: theme(plot.title = element_text(color="red", size=14, face="bold.italic"))

# Default plot
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot() +
  ggtitle("Plot of length \n by dose") +
  xlab("Dose (mg)") + ylab("Teeth length")
p
# Change the color, the size and the face of
# the main title, x and y axis labels
p + theme(
plot.title = element_text(color="red", size=14, face="bold.italic"),
axis.title.x = element_text(color="blue", size=14, face="bold"),
axis.title.y = element_text(color="#993333", size=14, face="bold")
)

Example: theme(plot.title = element_text(hjust = 0.5))

library(ggplot2)
ggplot(data=mtcars, aes(x=wt, y=mpg)) +
  geom_point() +
  labs(title="Automobile Data", x="Weight", y="Miles Per Gallon")+
  theme(plot.title = element_text(hjust = 0.5))  #也就加上这一行

Example: 使用ggplot2包中的scale_fill_gradient() 函数 填充颜色

 geom_sf(data = countries,color = "black", aes(fill = gdp_cap_est)) + 
  scale_fill_gradient(low = "white", high = "red") + 
  theme_minimal() 

Example:

library(tidyverse)
library(sf)
library(openxlsx)
library(ggplot2)
library(tmap)
tmap_mode("view")
library(sfhotspot)
library(sfdep)
library(dplyr)

setwd("/Users/libingnan/Documents/09-Samsung/25-New paper-Hotspot-Monkeypox/00_codes/ehsa_data/multi_months_2022_Europe")

geo_1 <- sf::read_sf("../gdf_europe.gpkg")

fn = "europe_2021-07-10_2022-06-10"

# read in data
file_name = substring(fn, 1, 28)
df_1 <- readr::read_csv(paste0(file_name,".csv"), col_types = "cDd")



# Create spacetime object called `bos`
bos <- spacetime(.data = df_1, 
                 .geometry = geo_1,
                 .loc_col = ".region_id",
                 .time_col = "time_period")

# conduct EHSA
ehsa <- emerging_hotspot_analysis(
  x = bos,
  .var = "value",
  k = 1,
  nsim = 199
)

# should put geo in the first place, otherwise it will triger the projection error
geo_ehsa <- merge(geo_1, ehsa, by.x=".region_id", by.y="location")

# tm_shape: Specify the shape object
# tm_polygons: Draw polygons
# "clssification" is a column of hotspot_results

ggplot() + 
  geom_sf(data = geo_ehsa,color = "black", aes(fill = classification) ) + 
  scale_fill_manual(values=c("new hotspot" = "red",
                             "new coldspot" = "#F0F0F0",
                             "consecutive hotspot" = "orange",
                             "consecutive coldspot" = "#F0F0F0",
                             "intensifying coldspot" = "#F0F0F0",
                             "intensifying hotspot" = "purple",
                             "oscilating hotspot" = "pink",
                             "oscilating coldspot" = "#F0F0F0",
                             "persistent coldspot" = "#F0F0F0",
                             "persistent hotspot" = "pink",
                             "sporadic coldspot" = "#F0F0F0",
                             "sporadic hotspot" = "yellow",
                             "no pattern detected" = "#F0F0F0")) +
  theme_minimal() + 
  labs(title = file_name) + 
  theme(plot.title = element_text(hjust = 0.5))

#ggsave(filename=paste0("./images/Rplot_", file_name, ".png"))

 

posted on 2023-07-18 15:17  McDelfino  阅读(14)  评论(0)    收藏  举报