gpt4 book ai didi

r - ggplot中没有边框的圆形

转载 作者:行者123 更新时间:2023-12-04 02:57:32 30 4
gpt4 key购买 nike

作为我 previous question 的直接跟进(@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=val1color=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"))

enter image description here

或者您指定 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"))

enter image description here

关于r - ggplot中没有边框的圆形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52272448/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com