ggplot中没有边框的圆形

M.T*_*ich 3 r legend shapes ggplot2

作为我上一个问题(@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"))
Run Code Online (Sandbox Code Playgroud)

我试图设置stroke=FALSE和可替换地使用shape=20,而更换fill=val1使用color=val1,以启用不同的着色。前者的变化没有效果,后者给了我Error: Discrete value supplied to continuous scale。我究竟做错了什么?

bee*_*oot 5

您可以添加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"))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

或者您在 aes 之外分配三角形的颜色并使用scale_colour_distillerwith 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"))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明