【发布时间】:2021-02-11 01:26:21
【问题描述】:
我已经成功地在下面绘制了我的dat。但是,我想做以下数据转换:dat %>% group_by(groups) %>% mutate(x.cm = mean(x), x.cwc = x-x.cm) 然后在我当前的绘图中添加 2 行:
-
geom_smooth()使用x.cm作为x -
geom_smooth()使用x.cwc作为x
有没有办法做到这一点?
p.s: 是否也可以在绘图上将 3 个 unique(x.cm) 值显示为 3 颗星? (见下图)
library(tidyverse)
dat <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/cw.csv')
dat %>% group_by(groups) %>% ggplot() +
aes(x, y, color = groups, shape = groups)+
geom_point(size = 2) + theme_classic()+
stat_ellipse()
# Now do the transformation:
dat %>% group_by(groups) %>% mutate(x.cm = mean(x), x.cwc = x-x.cm)
【问题讨论】:
-
试试这个,考虑到你有不同的措施:
dat %>% group_by(groups) %>% mutate(x.cm = mean(x), x.cwc = x-x.cm) %>% pivot_longer(-c(y,groups)) %>% ggplot(aes(value, y, color = factor(groups), shape = factor(groups))) + geom_point(size = 2) + theme_classic()+ geom_smooth(formula = y~x,se=F)+ stat_ellipse()+facet_wrap(.~name)
标签: r dataframe ggplot2 plot tidyverse