r - geom_density_2d 中的轮廓太紧
问题描述
我希望轮廓一直围绕数据,但我的密度区域很小。有谁知道如何解决这个问题?我将附上我的代码和数据。我尝试了不同的参数,但我似乎无法改变它。我想我想要的一种思考方式是 100% 的轮廓,而不是 90% 的轮廓,然后是 80% 等等。
ggplot(df3, aes(x, y)) +
stat_density_2d(size = 1, aes(color = "Contours")) +
geom_path(data = df3, aes(color = "Parameter Path"), lwd = 1) +
geom_point(data = df3, aes(color = "Accepted Values"), size = 2) +
labs(x = expression(mu), y = expression(phi)) +
theme(axis.text.x = element_text(size = 18),
axis.title.x = element_text(size = 20),
axis.text.y = element_text(size = 18),
axis.title.y = element_text(size = 20, angle = 0, vjust = 0.5),
plot.title = element_text(size = 20, face = "bold", hjust = 0.5),
legend.position = c(0.14, 0.84),
legend.background = element_rect(fill = "grey96", size = 1, linetype =
"solid"),
legend.text = element_text(size = 16),
legend.title = element_text(size = 18, face = "bold")) +
guides(fill = guide_legend(reverse = TRUE),
color=guide_legend(override.aes=list(fill=NA))) +
scale_x_continuous(breaks = seq(0.065, 0.135, 0.01),
limits = c(0.065, 0.135)) +
scale_y_continuous(breaks = seq(0.03, 0.055, 0.005),
limits = c(0.03, 0.055)) +
ggtitle("Confidence Ellipse") +
scale_color_manual("Results", values = c("red1", "deepskyblue2", "black"),
breaks = c("Accepted Values", "Parameter Path", "Contours"))
数据:
> x0
[1] 0.08358176 0.06553959 0.08701262 0.13261197 0.11827522 0.12895425
0.07641320 0.09780957 0.12004418 0.10330265 0.13115285 0.11239738
[13] 0.09539895 0.09978812 0.08133239 0.08256920 0.06962943 0.11592930
0.06910299 0.08968366 0.09268526 0.09354692 0.09407394 0.10241154
[25] 0.11241420 0.10679109 0.10047328 0.08391576 0.09663064 0.09601004
0.08067915 0.09624377 0.10755340 0.09217993 0.10787710 0.08061391
[37] 0.08068861 0.08953676 0.09189470 0.12449563 0.09883104 0.12972641
0.11940337 0.11907046 0.10960429 0.09448710
> y0
[1] 0.04219829 0.03586581 0.03353818 0.05380570 0.05151715 0.05587795
0.03240938 0.03869719 0.04355228 0.03748867 0.05029841 0.04462947
[13] 0.04057039 0.03691092 0.03801298 0.03997664 0.03429455 0.04798272
0.03641225 0.04115275 0.04029572 0.04043962 0.03793742 0.03940454
[25] 0.04468192 0.04189995 0.04296391 0.03632054 0.04192571 0.03698961
0.03703587 0.04008437 0.04824601 0.03933985 0.04131714 0.03589466
[37] 0.03665176 0.03872114 0.04056889 0.04843315 0.04547021 0.04853519
0.04710753 0.04888831 0.04833231 0.03744968
df3 = data.frame(x = x0, y = y0)
解决方案
如果我在没有任何更改的情况下运行您的代码,我会得到下面的图,我认为这就是您所追求的。
我按以下方式导入上面的数据:
library(dplyr)
library(ggplot2)
y0 <- t(read.table(text = "0.04219829 0.03586581 0.03353818 0.05380570 0.05151715 0.05587795 0.03240938 0.03869719 0.04355228 0.03748867 0.05029841 0.04462947 0.04057039 0.03691092 0.03801298 0.03997664 0.03429455 0.04798272 0.03641225 0.04115275 0.04029572 0.04043962 0.03793742 0.03940454 0.04468192 0.04189995 0.04296391 0.03632054 0.04192571 0.03698961 0.03703587 0.04008437 0.04824601 0.03933985 0.04131714 0.03589466 0.03665176 0.03872114 0.04056889 0.04843315 0.04547021 0.04853519 0.04710753 0.04888831 0.04833231 0.03744968",
sep=" ",header=FALSE,dec="."))
x0 <- t(read.table(text = "0.08358176 0.06553959 0.08701262 0.13261197 0.11827522 0.12895425 0.07641320 0.09780957 0.12004418 0.10330265 0.13115285 0.11239738 0.09539895 0.09978812 0.08133239 0.08256920 0.06962943 0.11592930 0.06910299 0.08968366 0.09268526 0.09354692 0.09407394 0.10241154 0.11241420 0.10679109 0.10047328 0.08391576 0.09663064 0.09601004 0.08067915 0.09624377 0.10755340 0.09217993 0.10787710 0.08061391 0.08068861 0.08953676 0.09189470 0.12449563 0.09883104 0.12972641 0.11940337 0.11907046 0.10960429 0.09448710",
sep=" ",header=FALSE,dec="."))
df3 = data.frame(x = x0, y = y0)
我运行您的代码(没有任何更改)
我得到以下warning messages
Warning messages:
1: Removed 1 rows containing non-finite values (stat_density2d).
2: Removed 1 rows containing missing values (geom_point).
可能是版本问题:
version.string R version 3.5.3 (2019-03-11)
> packageVersion("ggplot2")
[1] ‘3.1.1’
如果我使用你的版本运行ggplot2
> packageVersion("ggplot2")
[1] ‘3.0.0’
我仍然使用相同的代码得到与上面相同的情节。
此外,您可以geom_density_2d
不加修饰地尝试,看看是否至少可以按预期生成轮廓:
ggplot(df3, aes(x, y)) +
geom_density_2d(size=1,aes(color = "Contours"))+
geom_point(data = df3, aes(color = "Accepted Values"), size = 2)
推荐阅读
- html - 如何用 div 替换表格按钮
- c++ - 是否有 Win32 API 在字符串中添加双引号?
- java - TestNG:并行执行:只有活动窗口才能完成测试
- python - 仅使用参数的子集作为标识符来缓存 Python 函数结果
- c# - 多对多 Entity Framework Core 如何插入
- vector - 如何加载 Vec 的所有条目
任意长度到堆栈上? - r - 如何在 ggplot 2 中对单个条形进行分组 - 按颜色绘制条形图?
- ios - 在应用程序初始启动后,如何快速检索 Firebase Cloud Messaging 的消息传递令牌?
- python - Pygame屏幕抖动减慢游戏如何修复?
- node.js - 更新firestore文档映射字段onWrite触发器的firebase函数仅适用于最后一个索引