如何用geom_point为点添加光影效果以匹配目标图像?
如何用ggplot2的geom_point实现带光影效果的点图
可以实现带光影效果的点图,以下提供两种可行方案,其中借助ggfx包的模糊功能是更简洁高效的方式:
1. 传统重复绘点方案(参考你提到的多色点思路)
这种方法通过重复绘制不同样式的点层来模拟光影,需要手动控制每层的大小、颜色,适合简单场景但批量处理较繁琐:
library(ggplot2) dfDot <- data.frame(value=1, group="A") # 复制数据生成三层(阴影、过渡、主体) df_rep <- dfDot[rep(1:nrow(dfDot), 3), ] df_rep$layer <- factor(1:3, levels = c(1,2,3)) ggplot(df_rep, aes(x=group, y=value, size=layer, color=layer)) + geom_point() + # 从外到内依次减小尺寸、调整颜色 scale_size_manual(values = c(85, 80, 75)) + scale_color_manual(values = c("#333333", "#555555", "#7be5d7")) + theme_dark() + theme( axis.title=element_blank(), axis.text=element_blank(), axis.ticks=element_blank(), legend.position = "none" )
2. 借助ggfx包的模糊效果实现光影(高效方案)
ggfx的with_blur函数可以快速生成模糊的光影背景层,再叠加主体点层,无需修改数据结构,效果更自然:
library(ggplot2) library(ggfx) # 构造分组数据 dfDot <- data.frame( value = sort(rnorm(120)), group = rep(c("A", "B", "C"), each=40) ) # 定义分组配色 myColors <- c("#7be5d7", "#e57b7b", "#e5d57b") ggplot(data=dfDot, aes(x=group, y=value, color=group)) + # 绘制模糊光影层,sigma参数控制模糊程度 with_blur( geom_point(size=8), sigma=20 ) + # 叠加低透明度小点增强层次感 geom_point(size=3, alpha=0.05) + scale_colour_manual(values=myColors, guide="none") + theme_dark() + theme( axis.title=element_blank(), axis.text=element_blank(), axis.ticks=element_blank() )
内容的提问来源于stack exchange,提问作者Yacine Hajji
相关产品推荐
相关产品推荐

