楼主: Laura1020
12519 5

[问答] ggplot中stat_ellipse添加置信区间,怎么使椭圆内有填充色 [推广有奖]

  • 0关注
  • 0粉丝

等待验证会员

高中生

70%

还不是VIP/贵宾

-

威望
0
论坛币
20 个
通用积分
6.1181
学术水平
3 点
热心指数
3 点
信用等级
3 点
经验
125 点
帖子
11
精华
0
在线时间
44 小时
注册时间
2019-12-8
最后登录
2022-2-27

楼主
Laura1020 发表于 2021-5-24 14:16:38 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币



如题,想让绿环变半透明的绿椭圆


p3 = ggplot(sample.score, aes(p1, o1, color = age)) +
#添加线条
  geom_hline(yintercept = 0, linetype = 'dashed', size = 0.5) +
  geom_vline(xintercept = 0, linetype = 'dashed', size = 0.5) +
  geom_point() +
#ggrepel包添加散点标签
  geom_text_repel(aes(p1, o1,label = rownames(sample.score))) +
  labs(x = 'Tp',y = 'To') +
#置信椭圆
  stat_ellipse(level = 0.95, linetype = 'solid',
               size = 1, show.legend = FALSE) +
  scale_color_manual(values = c('#008000','#FFA74F')) +
  theme_bw() +
  theme(legend.position = c(0.9,0.85),
        legend.title = element_blank(),
        legend.text = element_text(color = 'black',size = 12,face = 'plain'),
        panel.background = element_blank(),
        panel.grid = element_blank(),
        axis.text = element_text(color = 'black',size = 15,  face = 'plain'),
        axis.title = element_text(color = 'black',size = 15,  face = 'plain'),
        axis.ticks = element_line(color = 'black'))

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:ellipse gplot 置信区间 plot lips

微信图片_20210524141612.png (18.63 KB)

微信图片_20210524141612.png

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
Sunknownay + 3 + 3 + 3 鼓励积极发帖讨论

总评分: 学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

沙发
owenqi 在职认证  学生认证  发表于 2021-5-24 22:43:31
主要是修改stat_ellipse那里,给你个例子
  1. ggplot(faithful, aes(waiting, eruptions, color = eruptions > 3, fill = eruptions > 3)) +
  2.   geom_point() +
  3.   stat_ellipse(type = "norm", geom = 'polygon', alpha = .3, linetype = 'dashed')
复制代码

已有 2 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
Sunknownay + 3 + 3 + 3 热心帮助其他会员
cheetahfly + 10 精彩帖子

总评分: 论坛币 + 10  学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

藤椅
Laura1020 发表于 2021-5-26 22:50:49
owenqi 发表于 2021-5-24 22:43
主要是修改stat_ellipse那里,给你个例子
可能我没有举一反三
我理想的结果是绿环变半透明绿圈,黄环变半透明黄圈
看了stat_ellipse {ggplot2}的帮助文档也没有get

板凳
owenqi 在职认证  学生认证  发表于 2021-5-27 10:05:21
Laura1020 发表于 2021-5-26 22:50
可能我没有举一反三
我理想的结果是绿环变半透明绿圈,黄环变半透明黄圈
看了stat_ellipse {ggplot2}的 ...
我给你的例子不就是你所谓的半透明椭圆吗?

报纸
jijitoutou 发表于 2021-8-9 12:41:21
owenqi 发表于 2021-5-27 10:05
我给你的例子不就是你所谓的半透明椭圆吗?
老师请问 我输入了你提供的代码,为什么椭圆置信区间的色彩部分正常,但看不到边框呢?修改很多次也没有解决问题麻烦了
autoplot(pca1, data = iris, col= "Species", size=2, shape = "Species",
loadings = TRUE, loadings.colour ='blue',
frame = FALSE,
loadings.label = TRUE, loadings.label.size =3,
label = FALSE, label.size = 3, axes = F) +stat_ellipse(aes(fill=iris$Species), type = "norm", geom ="polygon", alpha=0.2, level=0.95, show.legend = FALSE, linetype = 'dashed', size=3)+geom_hline(yintercept = 0 , linetype = 'dashed', size = 0.5) +
geom_vline(xintercept = 0 , linetype = 'dashed', size = 0.5) +
scale_x_continuous(limits = c(-0.2, 0.2), breaks=seq(-0.2, 0.2, 0.1))+
scale_y_continuous(limits = c(-0.3, 0.3), breaks=seq(-0.3, 0.3, 0.15)) + theme(panel.border = element_blank(), axis.ticks.length=unit(-0.18, "cm")) PCA.png

PCA.png (16.57 KB)

PCA.png

地板
jijitoutou 发表于 2021-8-9 12:53:18
owenqi 发表于 2021-5-27 10:05
我给你的例子不就是你所谓的半透明椭圆吗?
在老师的基础上,添加了一些细节,就可以达到线条+置信椭圆填充的效果了
附代码(大家可以粘贴使用,记得把数据改成自己的名字),注意在置信区间中,要写fill=以及color=,保证线条和填充都会显示正常:
theme_set(theme_bw())
autoplot(pca1, data = iris, col= "Species", size=2, shape = "Species") +stat_ellipse(aes(fill=iris$Species, color=iris$Species), geom ="polygon", type = "norm", alpha=0.2, level=0.95, show.legend = FALSE, linetype = 'dashed', size=1)+geom_hline(yintercept = 0 , linetype = 'dashed', size = 0.5)+
geom_vline(xintercept = 0 , linetype = 'dashed', size = 0.5) +
scale_x_continuous(limits = c(-0.2, 0.2), breaks=seq(-0.2, 0.2, 0.1))+
scale_y_continuous(limits = c(-0.3, 0.3), breaks=seq(-0.3, 0.3, 0.15)) + theme(panel.border = element_blank(), axis.ticks.length=unit(-0.18, "cm"))
我的代码tick是向内的(负数)
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
Sunknownay + 3 + 3 + 3 热心帮助其他会员

总评分: 学术水平 + 3  热心指数 + 3  信用等级 + 3   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-2-8 05:11