r - Geom_density_2d_filled() ggplot:(1)如何围绕所有点绘制轮廓,(2)如何改变渐变颜色?
问题描述
我有两个问题,我无法弄清楚我们的谷歌:
数据和ggplot:
x = rnorm(n = 500, mean = 0.5, sd = 0.3)
y = rnorm(n = 500, mean = 6, sd = 1)
data = merge(x, y, by = "row.names", all = TRUE)
data %>% ggplot(aes(x, y))+
geom_density_2d_filled(contour_var = "ndensity", bins = 5)+
theme_classic()
(1)如何围绕所有点绘制轮廓?目前,20% 的值与背景颜色相同。应该添加新层?
(2) 如何将背景颜色更改为白色,并将渐变颜色更改为 brewer "Blues" 调色板(所有高于 0 的值都应具有与白色背景不同的蓝色)?
解决方案
You'd probably want to decouple the stat from the geom, as the geom_density_2d_filled()
draws polygons but also includes the outer layer as a rectangle.
Here is my suggestion:
x = rnorm(n = 500, mean = 0.5, sd = 0.3)
y = rnorm(n = 500, mean = 6, sd = 1)
data = merge(x, y, by = "row.names", all = TRUE)
data %>% ggplot(aes(x, y))+
stat_density_2d(geom = "polygon", contour = TRUE,
aes(fill = after_stat(level)), colour = "black",
bins = 5) +
scale_fill_distiller(palette = "Blues", direction = 1) +
theme_classic()
推荐阅读
- neo4j - Neo4j / Cypher - 查找路径,但该路径的中间节点不能是 X 类型
- lua - Lua 键盘/鼠标宏 - 释放键时停止
- python - 如何在 cur.execute 中使用 python 返回有效的时间戳值
- c++ - 当我们将指向结构类型参数的指针传递给函数时,C++ 编译错误
- html - 如何修复 SVG 投影剪切问题
- python - 读取 html 格式的 csv 文件“ParserError:错误标记数据。C 错误:第 103 行中应有 1 个字段,看到 2”
- r - 在 R 中绘制直方图
- go - 如果无法访问 gradle Go,则跳过 Bitbucket API
- php - 为什么 PHP intl v.1.1.0 为“-SV”返回“萨尔瓦多”?
- c# - 从“控制台应用程序”更改为“控制台应用程序(.Net Framework)”后找不到程序集引用,在当前上下文中不存在?