如何让我的图例点蓝色和红色而不是红色?

时间:2021-04-07 14:06:43

标签: r ggplot2 legend scatter-plot

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

两个数据集的图,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()

2 个答案:

答案 0 :(得分:3)

这里的主要问题是您使用点的颜色名称设置颜色,但也使用 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 年 4 月 7 日创建

答案 1 :(得分: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 年 4 月 7 日创建

相关问题