我想要生成泡泡图,其中气泡有一个黑色的轮廓。但是,由于某些原因,我很难让geom_point接受scale_fill。这给了我一个很好的图,其中气泡颜色的尺度上有一个连续的变量,颜色:
age <-c(16, 5, 6, 22, 11, 12, 11, 13, 4, 8)
y <- c(0.53, 0.50, 0.50, 0.46, 0.44, 0.44, 0.44, 0.43, 0.40, 0.40)
s <- c(11.5, 78.0, 753.5, 44.5, 372.0, 62.0, 163.0, 25.0, 56.0, 80.5)
color <- c(29, 15, 7, 30, 15, 26, 8, 14, 17, 12)
df <- data.frame(age, y, s, color)
p <- ggplot(df)+
geom_point(aes(x = age, y = y, size = s, color = color))+
labs(x = "age", y = "rate")+
scale_size(range = c(.1, 10), name="s")+
scale_color_viridis(limits = c(5, 20), oob = squish, option = "magma")+
scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), limits = c(0,55))+
scale_y_continuous(breaks=c(-0.4, -0.2, 0, 0.2, 0.4), limits=c(-0.5, 0.5))+
theme_minimal()但是如果我把颜色换成填充,我就会得到所有的黑泡泡:
p <- ggplot(df)+
geom_point(aes(x = age, y = y, size = s, fill = color))+
labs(x = "age", y = "rate")+
scale_size(range = c(.1, 10), name="s")+
scale_fill_viridis(limits = c(5, 20), oob = squish, option = "magma")+
scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), limits = c(0,55))+
scale_y_continuous(breaks=c(-0.4, -0.2, 0, 0.2, 0.4), limits=c(-0.5, 0.5))+
theme_minimal()如果我指定形状:
p <- ggplot(df)+
geom_point(aes(x = age, y = y, size = s, shape = 21, fill = color))+
labs(x = "age", y = "rate")+
scale_size(range = c(.1, 10), name="s")+
scale_fill_viridis(limits = c(5, 20), oob = squish, option = "magma")+
scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), limits = c(0,55))+
scale_y_continuous(breaks=c(-0.4, -0.2, 0, 0.2, 0.4), limits=c(-0.5, 0.5))+
theme_minimal()我得到了
scale_f()中的错误:!连续变量不能映射为形状。
如果我正确理解,填充应该指定气泡的颜色和颜色应该让我指定一个黑色的轮廓周围的气泡。我在这里错过了什么?谢谢。
发布于 2022-08-29 17:56:04
我不太确定这是否是您的预期输出,但请注意:
请注意,21-24形状既有笔画颜色,也有填充。填充件的尺寸由尺寸控制,冲程的大小由冲程控制。每一个都是以毫米为单位的,点的总大小是两者之和。注意,在下图中,沿着对角线大小是常量的。
来源:vignette("ggplot2-specs")
library(tidyverse)
ggplot(df, aes(x = age, y = y, size = s, color = color))+
geom_point(fill = color, shape=21)+
labs(x = "age", y = "rate")+
scale_size(range = c(.1, 10), name="s")+
scale_color_viridis(limits = c(5, 20), oob = squish, option = "magma")+
scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), limits = c(0,55))+
scale_y_continuous(breaks=c(-0.4, -0.2, 0, 0.2, 0.4), limits=c(-0.5, 0.5))+
theme_minimal()

https://stackoverflow.com/questions/73532533
复制相似问题