y轴含负值时stat_cor无法定位到底部的解决方法咨询
解决stat_cor标签位置异常的问题
你的问题出在ggplot全局映射中设置了color=nlk,导致stat_cor默认按nlk分组计算相关系数并生成标签,多个标签叠加后看起来停留在绘图中间。要将全局相关系数标签放到右下角,可通过以下两种方式解决:
方法一:忽略全局美学映射
在stat_cor中设置inherit.aes = FALSE,单独指定全局的x、y映射,再调整位置参数:
ggplot(df, aes(x= nlk_percent ,y= c_percent, color=nlk)) + geom_point(position = position_jitter(width = 0.05, height = 0), size=7, alpha=.3) + geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+ theme_classic() + stat_cor( method = "pearson", inherit.aes = FALSE, aes(x = nlk_percent, y = c_percent), label.x.npc = "right", label.y.npc = "bottom" )
方法二:强制全局分组计算
在stat_cor的美学映射中设置group=1,强制按整体数据集计算相关系数:
ggplot(df, aes(x= nlk_percent ,y= c_percent, color=nlk)) + geom_point(position = position_jitter(width = 0.05, height = 0), size=7, alpha=.3) + geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+ theme_classic() + stat_cor( method = "pearson", aes(group=1), label.x.npc = "right", label.y.npc = "bottom" )
两种方法都能生成全局的Pearson相关系数标签,并将其定位到绘图右下角。
内容的提问来源于stack exchange,提问作者Ahdee
相关产品推荐
相关产品推荐

