代码之家  ›  专栏  ›  技术社区  ›  hmg

几何点和几何线的绘图顺序不同

  •  0
  • hmg  · 技术社区  · 7 年前

    当使用手动设置因子级别的顺序时,我不能让geom_点和geom_线以相同的顺序绘制数据。

    df <- data.frame(A=c(rep(c(5, 10, 15, 20, 25), 2)),
                     B=c(81, 86, 89, 94, 99, 81, 86, 89, 94, 100),
                     C=c(rep(0, 5), rep(1, 5)))
    df$C <- factor(df$C, levels=c(1,0))
    
    colors=c("red", "black")
    
    ggplot(df, aes(A, B)) +
        geom_point(aes(color=C)) +
        geom_line(aes(color=C)) +
        scale_color_manual(values=colors)
    

    enter image description here

    我想最后画出C的0,这是几何线,但几何点不是。我不明白为什么。我怎样才能让他们两个合作呢?如果我不设置df$C的因子级别,而是使用dplyr::arrange(desc(C)),它仍然不能解决差异。

    我使用自己的调色板,但即使使用默认的颜色,我得到这个问题。

    2 回复  |  直到 7 年前
        1
  •  3
  •   Jon Spring    7 年前

    将这一行添加到数据帧准备中,以便对数据帧进行排序 C geom_point .

    df <- df[order(df$C),]
    

    enter image description here

        2
  •  -1
  •   Community Mohan Dere    6 年前

    你可以改变进来的颜色

    library(tidyverse)
    
    colors=c("black", "red")
    
    data.frame(A = c(rep(c(5, 10, 15, 20, 25), 2)),
               B = c(81, 86, 89, 94, 99, 81, 86, 89, 94, 100),
               C = c(rep(0, 5), rep(1, 5))) %>% 
      mutate(C = factor(C)) %>% 
      arrange(C) %>% 
    ggplot(aes(A, B, color=C)) +
      geom_point() +
      geom_line() +
      scale_color_manual(values=colors) + 
      guides(color = guide_legend(reverse=TRUE))
    

    enter image description here