我有以下样例散点图:

enter image description here

我想go 掉"项目绩效"图例方块上的黑点.我只想让图例显示不同 colored颜色 的线条类型及其标签.有人知道怎么做吗?我第一次使用ggnewScale来显示这个同时使用geom_point和geom_vline的散点图的图例.我不知道如何在大范围内操纵这些传说.

以下是我的代码:

#sample data
df <- data.frame(cohort=c("20-21","20-21","20-21","20-21","21-22","21-22","21-22","21-22","22-23","22-23","22-23","22-23"),
                             sat=c(1220, 1020, 850, 1160, 920, 970, 1170, 830, 730, 1200, 1090, 880),
                             project=c(2.5, 2.2, 2.6, 2.8, 2.9, 3.0,3.0, 2.8, 2.0, 2.5, 1.8, 2.0),
                             pass_sat=c("Met Threshold", "Met Threshold", "Did Not Meet Threshold", "Did Not Meet Threshold",
                                        "Met Threshold", "Did Not Meet Threshold", "Did Not Meet Threshold","Met Threshold",
                                        "Met Threshold", "Did Not Meet Threshold", "Did Not Meet Threshold","Met Threshold"))

threshold <- data.frame(x = c(2, 2.7), group = c("pass", "scholarship eligible"),
                        color = c("red", "darkgreen"), line=c("dashed", "solid"), stringsAsFactors = FALSE) 

group.colors <- c("Overall" ="#FFD580", "ELA" = "#CBC3E3", "Math" = "lightyellow", "Essential Skills"="lightpink")

#sample plot
#total sat against total project score 
ggplot(df, aes(x = project, y = sat)) + 
  geom_point() +
  facet_grid(.~cohort) +
  scale_x_continuous(limits = c(1, 3), breaks = seq(1, 3, by = 0.5)) +
  geom_smooth(method = "lm", se = F) +
  labs(x = "Total Project score", y = "Total SAT") + 
  stat_cor(method = "pearson", size = 2.6, digits= 2) +
  geom_vline(
    aes(xintercept = x, colour = group, linetype = group),
    threshold
  )+ 
  scale_linetype_manual(
    name = "Project Performance",
    values = c(pass = "solid", "scholarship eligible" = "dashed")
  ) +
  scale_color_manual(
    name = "Project Performance",
    values = c(pass = "red", "scholarship eligible" = "darkgreen")
  ) +
  new_scale_color() +
  geom_point(aes(colour = pass_sat),
             show.legend = TRUE) +
  scale_color_manual(name = "SAT Performance", values = c("Met Threshold" = "purple", "Did Not Meet Threshold" = "black")) +
  labs(title= "Total SAT by Overall Project Score, by Year")

谢谢!

推荐答案

两种方式:

  1. 你有geom_point twice,但我相信第二个是你想要的,因为第一个没有aes(.).(我认为这意味着你是双重标绘点...虽然看不见,但仍在发生).如果注释掉第一个geom_point(),并从第二个中删除show.legend=TRUE,它就可以工作.

    ggplot(df, aes(x = project, y = sat)) + 
      # geom_point() +                                                    # REMOVE
      facet_grid(.~cohort) +
      scale_x_continuous(limits = c(1, 3), breaks = seq(1, 3, by = 0.5)) +
      geom_smooth(method = "lm", se = F) +
      labs(x = "Total Project score", y = "Total SAT") + 
      ggpubr::stat_cor(method = "pearson", size = 2.6, digits= 2) +
      geom_vline(
        aes(xintercept = x, colour = group, linetype = group),
        threshold
      )+ 
      scale_linetype_manual(
        name = "Project Performance",
        values = c(pass = "solid", "scholarship eligible" = "dashed")
      ) +
      scale_color_manual(
        name = "Project Performance",
        values = c(pass = "red", "scholarship eligible" = "darkgreen")
      ) +
      ggnewscale::new_scale_color() +
      geom_point(aes(colour = pass_sat)) +                                # UPDATE
      scale_color_manual(name = "SAT Performance", values = c("Met Threshold" = "purple", "Did Not Meet Threshold" = "black")) +
      labs(title= "Total SAT by Overall Project Score, by Year")
    

    enter image description here

  2. 在第一个色标生效时,在调用new_scale_color()之前加上guides(..)override.aes.(如果您按新比例执行after,则第二个图例中的点将被删除,而不是您所要求的.)

    ggplot(df, aes(x = project, y = sat)) + 
      geom_point() +
      facet_grid(.~cohort) +
      scale_x_continuous(limits = c(1, 3), breaks = seq(1, 3, by = 0.5)) +
      geom_smooth(method = "lm", se = F) +
      labs(x = "Total Project score", y = "Total SAT") + 
      ggpubr::stat_cor(method = "pearson", size = 2.6, digits= 2) +
      geom_vline(
        aes(xintercept = x, colour = group, linetype = group),
        threshold
      )+ 
      scale_linetype_manual(
        name = "Project Performance",
        values = c(pass = "solid", "scholarship eligible" = "dashed")
      ) +
      scale_color_manual(
        name = "Project Performance",
        values = c(pass = "red", "scholarship eligible" = "darkgreen")
      ) +
      guides(color = guide_legend(override.aes = list(shape = NA))) +     # NEW
      new_scale_color() +
      geom_point(aes(colour = pass_sat),
                 show.legend = TRUE) +
      scale_color_manual(name = "SAT Performance", values = c("Met Threshold" = "purple", "Did Not Meet Threshold" = "black")) +
      labs(title= "Total SAT by Overall Project Score, by Year")
    

    enter image description here

R相关问答推荐

提取rame中对应r中某个变量的n个最小正值和n个最大负值的条目

计算R中的威布尔分布的EDF

如何使用`ggplot2::geom_segment()`或`ggspatial::geom_spatial_segment()`来处理不在格林威治中心的sf对象?

如何计算R数据集中每个女性的子元素数量?

derrr summarise每个组返回多行?

整数成随机顺序与约束R?

使用across,starts_with和ifelse语句变更多个变量

移除仪表板Quarto中顶盖和车身之间的白色区域

使用rest从header(h2,h3,table)提取分层信息

更新R中的数据表(使用data.table)

QY数据的处理:如何定义QY因素的水平

将项粘贴到向量中,并将它们分组为x的倍数,用空格分隔

如果COLSUM为>;0,则COLNAME为向量

自定义交互作用图的标签

R-找出存在其他变量的各种大小的所有组合

以R表示的NaN值的IS.NA状态

子样本间系数检验的比较

在R中添加要打印的垂直线

如何捕获这个shiny 的、可扩展的react 性用户输入矩阵作为另一个react 性对象,以便进一步操作?

如何在一种 colored颜色 中设置数值变量的 colored颜色 和高于阈值的 colored颜色 点?