R语言中绘制散点图,同时设定填充色和外环的颜色
001、
library(ggplot2) # 构建示例数据 df <- data.frame( lon = c(-3, 2, 5, 7, -8, -9, 1, 0), # 经度 lat = c(40, 42, 48, 50, 36, 35, 43, 45), # 纬度 group = rep(c("A", "B"), each = 4) # 两种分组变量 ) # 指定颜色映射:填充色和边框色 fill_colors <- c("A" = "#9ECAE1", "B" = "#BC80BD") # 填充色 border_colors <- c("A" = "#3182BD", "B" = "#7A0177") # 外圈边框色 # 绘图 ggplot(df, aes(x = lon, y = lat)) + geom_point(aes(fill = group, color = group), shape = 21, # 关键!21 是支持填充色 + 边框的点形状 size = 6, # 点大小 stroke = 1.2) + # 控制外圈粗细 scale_fill_manual(values = fill_colors) + scale_color_manual(values = border_colors) + theme_minimal() + labs(title = "双色圈散点图", fill = "组", color = "组")
。