【问题标题】:Legend for scatter-line graph in ggplot2 (without colour)ggplot2中散点图的图例(无颜色)
【发布时间】:2021-10-29 13:02:21
【问题描述】:

对此非常陌生,如果有明显的答案,我们深表歉意。我正在尝试将图例添加到具有 2 个 y 变量的散点图;我知道这可以使用颜色来完成,但是我理想情况下希望保持黑白,并通过线型/点来定义图例中的变量。有没有办法做到这一点?

ggplot(birds, aes(distance)) +geom_point(aes(y=individuals_AC)) +geom_point(aes(y=species_AC, shape=17)) +geom_line(aes(y=individuals_AC)) +geom_line(aes(y=species_AC, linetype="dashed")) + scale_shape_identity() + scale_linetype_identity() + theme_classic()

【问题讨论】:

  • 请分享您的代码和数据

标签: r ggplot2 legend


【解决方案1】:
library(tidyverse)

#create some dummy data
df <- tibble(
  x = runif(10),
  y = runif(10),
  type = rep(c("a", "b"), 5)
)

#plot it with a different shape for each type
df %>% 
  ggplot(aes(x, y, shape = type)) + 
  geom_point()

【讨论】:

    猜你喜欢
    • 2010-11-26
    • 2021-05-25
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多