r - ggplot中没有边框的圆形

标签 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=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/

相关文章:

r - 用加权点在 ggplot 散点图上绘制 NA

R C5.0 获取每片叶子的规则和概率

r - ggplot中的热图,每组不同的颜色

r - if else 条件在 ggplot 中添加一个额外的层

javascript - 将鼠标悬停在 ApexCharts.js 中的图例系列上时如何显示图例工具提示

python - 在 matplotlib 中更改图例的标题字体大小

r - 设置键时data.table如何对字符串排序

r - R中表格上方的动态标签

当有多个时,删除 ggplot2 中的额外图例

javascript - Highcharts:向图例添加额外的文本