r - 整个组的趋势线 - R
问题描述
我正在尝试为所有组绘制一条趋势线,但它为每个组绘制了一条趋势线。我使用的相同代码放在这里(使用 iris 数据集):
iris %>%
mutate(id = as.numeric(rownames(iris))) %>%
select(id, Sepal.Length, Sepal.Width, Petal.Length) %>%
reshape(., direction = "long", varying = names(.)[2:4], v.names = "valor", idvar = c("id"), timevar = "tipo", times= colnames(.[2:4])) %>%
ggplot(aes(x=id, y=valor, fill=tipo)) +
geom_area() +
geom_smooth(method = "lm")
我的数据框输出的图像(无线geom_smooth()
)
我尝试添加一条趋势线:
geom_smooth(method = "lm")
但是它为每个组添加了一条趋势线,而我只需要一个作为总数。
解决方案
aes
为每个单独的几何设置映射:
iris %>%
mutate(id = rownames(iris)) %>%
select(id, Sepal.Length, Sepal.Width, Petal.Length) %>%
reshape(., direction = "long", varying = names(.)[2:4], v.names = "valor", idvar = c("id"), timevar = "tipo", times= colnames(.[2:4])) %>%
mutate(id = as.numeric(id)) %>%
ggplot() +
geom_area(aes(x=id, y=valor, fill=tipo)) +
geom_smooth(aes(x=id, y=valor), method = "lm")
(我需要添加一个额外的 mutate 以更改id
为 numeric 以使您的代码正常工作)
推荐阅读
- makefile - android 使用 arm64-v8a ABI 编译错误“未定义的引用”
- soap - EWS 托管 api 检查用户帐户中是否存在事件
- scala - Guava Cache Generic LoadingCache
- python - 为什么 Tensorflow 网站花卉图像分类需要大约 6 秒才能进行分类?
- php - 我想将时间以 hrs:min:sec 格式存储在数据库中,并以 hrs:min:sec 格式显示时间
- elasticsearch - 大批量索引与涓流请求
- javascript - 查找具有频率值的二维关键字数组出现的更快方法
- excel - Excel,自动填充日期范围,每个日期范围一个单元格
- azure - Azure VM:创建 VHD URI、公共 IP 地址、虚拟网络/子网和 DNS 名称后被删除
- python - 对输入中的数字进行平均的功能,没有限制