【问题标题】:How do I get my legend dots blue and red instead that both are red?如何让我的图例点变成蓝色和红色,而不是两者都是红色?
【发布时间】:2023-04-07 01:03:01
【问题描述】:

我试图在我的图例中得到一个红点和一个蓝点,这不是我使用的数据,而是一个可重复的示例,

两个数据集的图出口,example是其中之一,example1。

这是我的代码:

    if (!require("pacman")) install.packages("pacman")
    pacman::p_load_gh("trinker/wakefield")
    
    
    


    example <- r_data_frame(
        n = 100,
        id,
        iq,
        age,
        height
        )
  
 
    example1 <- r_data_frame(
        n = 100,
        id,
        iq,
        age,
        height)
    
    
   
    
    library(ggplot2)
    color_names <- c("example", "example1")
    color_values <- c("blue", "red")
    names(color_values) <- color_names
    ggplot() + 
       
 #These points need to be blue and in the legend as well.     
        geom_point(data=example, aes(x=ID, y=Height,
        fill ="example"), 
        colour="darkblue", size=1) +
    
   #These points need to be red and red in the legend 
      geom_point(data=example1, aes(x=ID, y=Height, 
        fill ="example1"), colour = "red"
        , size=1) +
      
      # plot configuration scales, theme, etc...
      scale_colour_manual(values = color_values) +
      scale_fill_manual(values = color_values) +
      theme_bw()

【问题讨论】:

    标签: r ggplot2 legend scatter-plot


    【解决方案1】:

    Joel Kandiah's answer 更进一步 (+1),我会使用更自然的方式让 ggplot2 发挥其奇妙的美感 - 如果您的数据框包含相同的变量,请使用长格式。只需将它们绑定在一起即可。

    避免头痛和硬编码,还可以减少您的代码。

    library(wakefield)
    library(tidyverse)
    
    example <- r_data_frame(n = 100, id, iq, age, height)
    #> Warning: `tbl_df()` was deprecated in dplyr 1.0.0.
    #> Please use `tibble::as_tibble()` instead.
    example1 <- r_data_frame(n = 100, id, iq, age, height)
    
    color_values <- c(example = "darkblue", example1 = "red")
    
    bind_rows(mget(ls(pattern = "^example")), .id = "example") %>%
    ggplot() + 
      geom_point(aes(x=ID, y=Height,color = example),  size=1) +
      scale_colour_manual(name = "colour", values = color_values) +
      theme_bw()
    

    reprex package (v1.0.0) 于 2021-04-07 创建

    【讨论】:

      【解决方案2】:

      这里的主要问题是您使用点的颜色名称设置颜色,但还使用 scale_colour_manual 和 scale_fill_manual 覆盖这些设置。

      当您使用geom_point() 时,无需更改填充,除非您使用不同的点类型。

      我建议用示例标记aes() 中的颜色(不仅仅是填充)。我的解决方案删除了​​所有多余的比例,如果您在实际实现中需要它们,我可以将它们添加回来(不是 reprex,因为它在这里是不必要的)。

      我还修改了 colour_values 变量以在一行中包含名称和颜色(而不是您的实现)。

      if (!require("pacman")) install.packages("pacman")
      pacman::p_load_gh("trinker/wakefield")
      
      example <- r_data_frame(
        n = 100,
        id,
        iq,
        age,
        height
      )
      #> Warning: `tbl_df()` was deprecated in dplyr 1.0.0.
      #> Please use `tibble::as_tibble()` instead.
      
      example1 <- r_data_frame(
        n = 100,
        id,
        iq,
        age,
        height)
      
      library(ggplot2)
      color_values <- c("example" = "darkblue", "example1" = "red")
      ggplot() + 
        
        #These points need to be blue and in the legend as well.     
        geom_point(data=example, aes(x=ID, y=Height,color = "example"),  size=1) +
        
        #These points need to be red and red in the legend 
        geom_point(data=example1, aes(x=ID, y=Height, 
                                      color = "example1"), size=1) +
        
        # plot configuration scales, theme, etc...
        scale_colour_manual(name = "colour", values = color_values) +
        theme_bw()
      

      reprex package (v2.0.0) 于 2021-04-07 创建

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-22
        • 1970-01-01
        • 2014-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-04
        • 1970-01-01
        相关资源
        最近更新 更多