ggplot中无边框的圆形
Circle shape without border in ggplot
作为我的 的直接跟进(@PoGibas 友善地回答),我正在调整情节的外观。我想使用无边界的圆形,但在将其集成到我现有的代码中时遇到了问题。这是到目前为止的代码:
set.seed(42)
lat1 <- rnorm(10, 54, 12)
long1 <- rnorm(10, 44, 12)
val1 <- rnorm(10, 10, 3)
lat2 <- rnorm(10, 54, 12)
long2 <- rnorm(10, 44, 12)
val2 <- rnorm(10, 10, 3)
df1 <- as.data.frame(cbind(long1, lat1, val1))
df2 <- as.data.frame(cbind(long2, lat2, val2))
ggplot() +
geom_point(data = df1,
aes(lat1, long1, size = val1, fill = val1),
shape = 21, alpha = 0.6) +
geom_point(data = df2,
aes(lat2, long2, color = "val2"),
shape = 17, size = 3) +
geom_point(data = df2,
aes(lat2, long2, color = "val2"),
shape = 17, size = 11.5, alpha = 0) +
scale_size_continuous(range = c(2, 12), breaks = pretty_breaks(4)) +
scale_fill_distiller(direction = -1, palette = "RdYlBu", breaks = pretty_breaks(4)) +
scale_color_manual(values = "black", name = "Value\n") +
labs(fill = NULL,
size = NULL) +
guides(fill = guide_legend(),
size = guide_legend(),
color = guide_legend(order = 1)) +
theme_minimal() +
theme(legend.spacing.y = unit(-0.4, "cm"))
我试过设置 stroke=FALSE
并使用 shape=20
替换 fill=val1
为 color=val1
以启用不同的颜色。前者的变化没有效果,后者给了我Error: Discrete value supplied to continuous scale
。我做错了什么?
您可以添加colour = "transparent"
:
ggplot() +
geom_point(data = df1,
aes(lat1, long1, size = val1, fill = val1),
shape = 21, alpha = 0.6, colour = "transparent") +
geom_point(data = df2,
aes(lat2, long2, color = "val2"),
shape = 17, size = 3) +
geom_point(data = df2,
aes(lat2, long2, color = "val2"),
shape = 17, size = 11.5, alpha = 0) +
scale_size_continuous(range = c(2, 12), breaks = pretty_breaks(4)) +
scale_fill_distiller(direction = -1, palette = "RdYlBu", breaks = pretty_breaks(4)) +
scale_color_manual(values = "black", name = "Value\n") +
labs(fill = NULL,
size = NULL) +
guides(fill = guide_legend(),
size = guide_legend(),
color = guide_legend(order = 1)) +
theme_minimal() +
theme(legend.spacing.y = unit(-0.4, "cm"))
或者您在 aes 之外分配三角形的颜色并使用 scale_colour_distiller
和 shape = 19
(尽管这摆脱了图例中的 val2 键,但不确定如何解决) :
ggplot() +
geom_point(data = df1,
aes(lat1, long1, size = val1, colour = val1),
shape = 19, alpha = 0.6) +
geom_point(data = df2,
aes(lat2, long2),
shape = 17, size = 3, colour = "black") +
geom_point(data = df2,
aes(lat2, long2),
shape = 17, size = 11.5, alpha = 0, colour = "black") +
scale_size_continuous(range = c(2, 12), breaks = pretty_breaks(4)) +
scale_colour_distiller(direction = -1, palette = "RdYlBu", breaks = pretty_breaks(4)) +
labs(colour = NULL,
size = NULL) +
guides(colour = guide_legend(),
size = guide_legend(),
color = guide_legend(order = 1)) +
theme_minimal() +
theme(legend.spacing.y = unit(-0.4, "cm"))
作为我的
set.seed(42)
lat1 <- rnorm(10, 54, 12)
long1 <- rnorm(10, 44, 12)
val1 <- rnorm(10, 10, 3)
lat2 <- rnorm(10, 54, 12)
long2 <- rnorm(10, 44, 12)
val2 <- rnorm(10, 10, 3)
df1 <- as.data.frame(cbind(long1, lat1, val1))
df2 <- as.data.frame(cbind(long2, lat2, val2))
ggplot() +
geom_point(data = df1,
aes(lat1, long1, size = val1, fill = val1),
shape = 21, alpha = 0.6) +
geom_point(data = df2,
aes(lat2, long2, color = "val2"),
shape = 17, size = 3) +
geom_point(data = df2,
aes(lat2, long2, color = "val2"),
shape = 17, size = 11.5, alpha = 0) +
scale_size_continuous(range = c(2, 12), breaks = pretty_breaks(4)) +
scale_fill_distiller(direction = -1, palette = "RdYlBu", breaks = pretty_breaks(4)) +
scale_color_manual(values = "black", name = "Value\n") +
labs(fill = NULL,
size = NULL) +
guides(fill = guide_legend(),
size = guide_legend(),
color = guide_legend(order = 1)) +
theme_minimal() +
theme(legend.spacing.y = unit(-0.4, "cm"))
我试过设置 stroke=FALSE
并使用 shape=20
替换 fill=val1
为 color=val1
以启用不同的颜色。前者的变化没有效果,后者给了我Error: Discrete value supplied to continuous scale
。我做错了什么?
您可以添加colour = "transparent"
:
ggplot() +
geom_point(data = df1,
aes(lat1, long1, size = val1, fill = val1),
shape = 21, alpha = 0.6, colour = "transparent") +
geom_point(data = df2,
aes(lat2, long2, color = "val2"),
shape = 17, size = 3) +
geom_point(data = df2,
aes(lat2, long2, color = "val2"),
shape = 17, size = 11.5, alpha = 0) +
scale_size_continuous(range = c(2, 12), breaks = pretty_breaks(4)) +
scale_fill_distiller(direction = -1, palette = "RdYlBu", breaks = pretty_breaks(4)) +
scale_color_manual(values = "black", name = "Value\n") +
labs(fill = NULL,
size = NULL) +
guides(fill = guide_legend(),
size = guide_legend(),
color = guide_legend(order = 1)) +
theme_minimal() +
theme(legend.spacing.y = unit(-0.4, "cm"))
或者您在 aes 之外分配三角形的颜色并使用 scale_colour_distiller
和 shape = 19
(尽管这摆脱了图例中的 val2 键,但不确定如何解决) :
ggplot() +
geom_point(data = df1,
aes(lat1, long1, size = val1, colour = val1),
shape = 19, alpha = 0.6) +
geom_point(data = df2,
aes(lat2, long2),
shape = 17, size = 3, colour = "black") +
geom_point(data = df2,
aes(lat2, long2),
shape = 17, size = 11.5, alpha = 0, colour = "black") +
scale_size_continuous(range = c(2, 12), breaks = pretty_breaks(4)) +
scale_colour_distiller(direction = -1, palette = "RdYlBu", breaks = pretty_breaks(4)) +
labs(colour = NULL,
size = NULL) +
guides(colour = guide_legend(),
size = guide_legend(),
color = guide_legend(order = 1)) +
theme_minimal() +
theme(legend.spacing.y = unit(-0.4, "cm"))