r - ggplot中没有填充次要点
问题描述
下面的情节有两个问题。首先,我怎样才能让我的 3 个黑点没有填充?其次,我怎样才能拥有geom_smooth()
这 3 个黑点?
我为两者都尝试了一些东西,但都没有成功。
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
解决方案
试试这个非填充点:
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
输出:
为了顺利尝试这个:
#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))
输出:
试试这个保持相同的形状:
#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))
输出:
推荐阅读
- python - Tkinter 窗口和 pygame 窗口同时打开
- reactjs - 如何更改 Material-UI Button 的 tabIndex?
- vb.net - ContextMenuStrip 需要两次右键才能显示
- azure - 当我在 terraform 中运行 cidrsubnet 函数时,我得到“前缀扩展 3 不容纳编号为 8 的子网”。为什么是这样?
- reactjs - 将动态空对象添加到数组 React
- sql - 为什么 GROUP BY 在 SQL 中比 DISTINCT 快?
- cxf - apache cxf-codegen-plugin wsdl2java 忽略 WSDL 选项
- xml - 如何使用 XSL 按子元素名称对元素进行排序
- javascript - 使用 sinon 模拟函数中的一行
- lua - 尝试将 nil 与字符串 Roblox 连接