ggplot中无边框的圆形

时间:2018-09-11 09:07:48

标签: r ggplot2 legend shapes

作为我的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=val1替换color=val1以启用不同的颜色。前一个更改无效,而后者给了我Error: Discrete value supplied to continuous scale。我在做什么错了?

1 个答案:

答案 0 :(得分:2)

您可以添加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_distillershape = 19(尽管在图例中摆脱了val2键,但不确定如何解决该问题):< / p>

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