如何摆脱 R 中 geom_point 中填充较深颜色的内点?
Posted
技术标签:
【中文标题】如何摆脱 R 中 geom_point 中填充较深颜色的内点?【英文标题】:How to get rid of inner dot filled with deeper colour in geom_point in R? 【发布时间】:2021-09-14 20:38:53 【问题描述】:我不希望图形中的几何点中出现更小、更内部、更深的彩色点。我该如何摆脱它?我试着玩弄形状,但什么也没发生。
library(stats)
library(ggfortify)
sp1 <- rnorm(72, mean = 4, 1)
sp2 <- rnorm(72, mean = 2, 1)
sp3 <- rnorm(72, mean = 3, 1)
sp4 <- rnorm(72, mean = 9, 1)
temp <- rnorm(72, mean = 20, 5)
season <- rep(c("Summer", "Autumn","Winter", "Spring"), each = 18)
sp.abd <- data.frame(sp1, sp2, sp3, sp4, season, temp)
pca_res <- prcomp(sp.abd[,-c(5,6)], scale. = TRUE)
autoplot(pca_res,
loadings = TRUE, loadings.colour = 'blue',
loadings.label = TRUE, loadings.label.size = 3) +
theme_classic() + geom_point(aes(color = sp.abd$season), size = 6, alpha = 0.5) +
scale_color_manual(values = rainbow(12,s = 0.6, start = 0, end = 0.7, rev = TRUE))
按照下面兰迪的回答,我尝试复制它以按温度着色(此处为渐变)
autoplot(pca_res, sp.abd, color = temp,
loadings = TRUE, loadings.colour = 'blue',
loadings.label = TRUE, loadings.label.size = 3) +
scale_colour_gradient2(low = "white", mid = "cyan", high = "dodgerblue4", na.value = NA)
它只生成一个灰色图。只有当我在 autoplot 命令之外调用颜色时,它才会使用 scale_colour_gradient2 选项。但这仍然是点
autoplot(pca_res, size = 6, alpha. = 0.5,
loadings = TRUE, loadings.colour = 'blue',
loadings.label = TRUE, loadings.label.size = 3) + geom_point(aes(colour = sp.abd$temp)) +
scale_colour_gradient2(low = "white", mid = "cyan", high = "dodgerblue4", na.value = NA)
【问题讨论】:
您的season
未定义
请说明您正在为这些功能使用哪些包 - 例如,prcomp()
来自 stats
和 autoplot()
来自 ggplot2
?问是因为当我尝试运行您的代码时,我收到错误 Objects of type prcomp not supported by autoplot
尝试从autoplot
传递size = 0
→ autoplot.prcomp
(in ggfortify
) → gg.biplot
我认为这个问题完全不依赖于vegan
包,所以最好从问题中删除library(vegan)
。代码中还有其他内容可以删除以使问题更简单(theme_classic()
、scale_color_manual()
等)。
奇怪的是,我仍然使用size=0
获得积分,但size=-1
似乎可以解决问题
【参考方案1】:
看起来autoplot.prcomp
(ggfortify
指定的prcomp
对象的autoplot
方法)已经在没有geom_point()
调用的情况下处理了点,因此您可以放置颜色映射、点大小、并在autoplot
内点不透明度:
library(stats)
library(ggfortify)
sp1 <- rnorm(72, mean = 4, 1)
sp2 <- rnorm(72, mean = 2, 1)
sp3 <- rnorm(72, mean = 3, 1)
sp4 <- rnorm(72, mean = 9, 1)
season <- rep(c("Summer", "Autumn","Winter", "Spring"), each = 18)
sp.abd <- data.frame(sp1, sp2, sp3, sp4, season)
pca_res <- prcomp(sp.abd[,-5], scale. = TRUE)
autoplot(pca_res,
################
# Add this stuff
data = sp.abd,
color = 'season',
size=6, alpha=.5,
################
loadings = TRUE, loadings.colour = 'blue',
loadings.label = TRUE, loadings.label.size = 3) +
theme_classic() +
scale_color_manual(values = rainbow(12,s = 0.6, start = 0, end = 0.7, rev = TRUE))
【讨论】:
谢谢!但如果我尝试通过连续变量+ scale_colour_gradient2(low = "white", mid = "cyan", high = "dodgerblue4", na.value = NA)
着色,它就不起作用了,我会更新问题以上是关于如何摆脱 R 中 geom_point 中填充较深颜色的内点?的主要内容,如果未能解决你的问题,请参考以下文章