You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于国家均值绘制多元散点图(禁用tidyverse与dplyr)

不用tidyverse/dplyr的解决方案

第一步:保存并整理均值计算结果

先把你用aggregate算出的均值保存为单独对象,并重命名列名(避免默认的Group.1和x,方便后续合并):

# 计算并保存LGBT支持度的国家均值
lgbt_mean <- aggregate(ess$lgbtsupport, list(ess$cntry), FUN=mean, na.rm=T)
colnames(lgbt_mean) <- c("cntry", "lgbtsupport_mean")

# 计算并保存宗教信仰度的国家均值
relig_mean <- aggregate(ess$religiosity, list(ess$cntry), FUN=mean, na.rm=T)
colnames(relig_mean) <- c("cntry", "religiosity_mean")

第二步:合并两个均值数据集

用基础R的merge函数,按国家代码cntry将两个数据集合并成一个包含所有国家均值的数据集:

country_means <- merge(lgbt_mean, relig_mean, by = "cntry")

第三步:绘制散点图

用基础R的plot函数直接绘制,还可以添加回归线展示变量间的关联趋势:

# 绘制基础散点图
plot(x = country_means$religiosity_mean, 
     y = country_means$lgbtsupport_mean,
     main = "国家宗教信仰度均值 vs LGBT支持度均值",
     xlab = "宗教信仰度均值",
     ylab = "LGBT支持度均值",
     pch = 16, # 实心圆点样式
     col = "darkblue", # 点的颜色
     cex = 1.2) # 点的大小

# 可选:添加线性回归线,红色加粗
abline(lm(lgbtsupport_mean ~ religiosity_mean, data = country_means), 
       col = "red", lwd = 2)

这样得到的country_means就是你需要的合并后数据集,散点图完全用基础R实现,符合不使用tidyverse/dplyr的要求。

内容的提问来源于stack exchange,提问作者Jacob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 05:02:50