我正在尝试在ggplot2中叠加两个散点图。目标是根据一个变量(6个类别,因子)使点的外部部分着色,并在内部填充另一个连续变量(数字)的渐变颜色。
我写了两段代码,每段都是独立的(请看下面的截图)。
ggplot(PCA_isotopes_2, aes(x=PC1, y=PC2)) +
theme_classic() +
geom_point(aes(color = factor(subspecies)), shape = 1, size = 2.95, stroke=1, alpha=5/6) +
scale_color_manual(breaks = c("gutturalis", "rg.hybrids", "rt", "rustica", "tg", "tytleri"), values=c("#0066CC", "#9933CC", "#FFCC99", "#CC0000", "#33CC99", "#FFFF00"))
ggplot(PCA_isotopes_2, aes(x=PC1, y=PC2)) +
theme_classic() +
geom_point(aes(color = carbon.ratio), size = 2.88, alpha=5/6) +
scale_colour_gradient(low = "blue", high = "yellow")
当我尝试以这种方式覆盖它们时:
p <- ggplot(PCA_isotopes_2, aes(x=PC1, y=PC2)) +
theme_classic() +
geom_point(aes(color = carbon.ratio), size = 2.88, alpha=5/6) +
scale_colour_gradient(low = "blue", high = "yellow")
p + geom_point(aes(color = factor(subspecies)), shape = 1, size = 2.95, stroke=1, alpha=5/6) +
scale_color_manual(breaks = c("gutturalis", "rg.hybrids", "rt", "rustica", "tg", "tytleri"), values=c("#0066CC", "#9933CC", "#FFCC99", "#CC0000", "#33CC99", "#FFFF00"))
我收到错误消息:
“'颜色'的比例已经存在。为'颜色'添加另一个比例,其中 将取代现有的规模。 错误:连续值提供给离散比例“。
我花了几个小时试图弄清楚它为什么不起作用。我将非常感谢您的帮助!
谢谢, 格奥尔基
答案 0 :(得分:3)
一般来说,你只能映射一次美学。这是一种解决方法,使用fill
美学对连续变量作为替代,使用shape = 21
。但是,我希望完全映射到不同的美学,例如shape
,就像在第二个版本中一样。
library(tidyverse)
ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
theme_classic() +
geom_point(
mapping = aes(colour = Species),
shape = 1,
size = 3,
stroke = 2,
alpha = 5 / 6
) +
geom_point(
mapping = aes(fill = Sepal.Length, colour = NA),
size = 2.88,
alpha = 5 /6,
shape = 21
) +
scale_fill_gradient(low = "blue", high = "yellow")
library(viridis)
#> Loading required package: viridisLite
ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
theme_classic() +
geom_point(
mapping = aes(colour = Sepal.Length, shape = Species),
size = 3,
alpha = 5 / 6
) +
scale_colour_viridis()
由reprex package(v0.2.0)创建于2018-04-19。