gpt4 book ai didi

r - ggplot 中的次要点没有填充

转载 作者:行者123 更新时间:2023-12-04 08:40:42 25 4
gpt4 key购买 nike

下面的情节有两个问题。首先,如何让我的 3 个黑点没有填充?其次,如何为这 3 个黑点设置 geom_smooth()

我已经尝试过两者都没有成功。

dat <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/cw2.csv')
dat$groups <- factor(dat$groups)

dat2 <- dat %>% group_by(groups) %>% summarize(mean_x = mean(x),
mean_y = mean(y),
.groups = 'drop')

dat %>% group_by(groups) %>% ggplot() +
aes(x, y, color = groups, shape = groups)+
geom_point(size = 2) + theme_classic()+
stat_ellipse(level = .6) +
geom_point(data = dat2,
mapping = aes(x = mean_x, y = mean_y), fill = NA, ### No fill
size = 4, color = 1, show.legend = F) +
geom_smooth(data = dat2, mapping = aes(x = mean_x, y = mean_y),
method = "lm", se=F, color = 1, formula = 'y ~ x') ## geom_smooth doesn't show

enter image description here

最佳答案

对非填充点试试这个:

library(tidyverse)
#Code
dat %>% group_by(groups) %>% ggplot() +
aes(x, y, color = groups, shape = groups)+
geom_point(size = 2) + theme_classic()+
stat_ellipse(level = .6) +
geom_point(data = dat2,
mapping = aes(x = mean_x, y = mean_y,fill = factor(groups)),
size = 4, show.legend = F,shape=21) +
geom_smooth(data = dat2, mapping = aes(x = mean_x, y = mean_y),
method = "lm", se=F, color = 1, formula = 'y ~ x') ## geom_smooth doesn't show

输出:

enter image description here

为了顺利,试试这个:

#Code 2
dat %>% group_by(groups) %>% ggplot() +
aes(x, y, color = groups, shape = groups)+
geom_point(size = 2) + theme_classic()+
stat_ellipse(level = .6) +
geom_point(data = dat2,
mapping = aes(x = mean_x, y = mean_y,fill = factor(groups)),
size = 4, show.legend = F,shape=21) +
geom_smooth(data = dat2, mapping = aes(x = mean_x, y = mean_y,group=1),
method = "lm", se=F, color = 1, formula = 'y ~ x')+
scale_fill_manual(values=rep('black',3))

输出:

enter image description here

尝试这个来保持相同的形状:

#Code 3
dat %>% group_by(groups) %>% ggplot() +
aes(x, y, color = groups, shape = groups)+
geom_point(size = 2) + theme_classic()+
stat_ellipse(level = .6) +
geom_point(data = dat2,
mapping = aes(x = mean_x, y = mean_y,color = factor(groups)),
size = 4, show.legend = F,color='black') +
geom_smooth(data = dat2, mapping = aes(x = mean_x, y = mean_y,group=1),
method = "lm", se=F, color = 1, formula = 'y ~ x')+
scale_fill_manual(values=rep('black',3))

输出:

enter image description here

关于r - ggplot 中的次要点没有填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64579456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com