Effective way of plotting additional points to an exisiting plot

前端 未结 2 1361
太阳男子
太阳男子 2021-01-25 08:49

In my case, there are 100 unique (X, Y) points with each having an ID and belongs a Type. In these 100 points, 20 points have values for t

2条回答
  •  野的像风
    2021-01-25 09:22

    Updated Answer

    To address the first comment: Some points are plotted more than once because there are multiple rows in the data with the same X Y coordinates. You can remove duplicate points using the code below. We first order the points based on the ordering of Val so that the duplicates will come from Other points, rather than from D_1 or D_4 points (though if your real data contains cases where a D_1 and a D_4 point have the same X and Y coordinates, only the D_1 point will be plotted).

    ggplot(df %>% 
             mutate(Val=fct_other(Val,keep=c("D_1","D_4"))) %>% 
             arrange(Val) %>% 
             filter(!duplicated(.[,c("X","Y")])), 
           aes(X,Y,col=Val, size=Val)) + 
      geom_point() +
      scale_colour_manual(values=c(D_1=hcl(15,100,65),D_4=hcl(195,100,65),Other="grey70")) +
      scale_size_manual(values=c(D_1=3, D_4=3, Other=1)) +
      theme_bw() 
    

    If you want to plot all D_1 and D_4 points, even if they have the same X and Y coordinates, you could do this:

    df %>% 
       mutate(Val=fct_other(Val,keep=c("D_1","D_4"))) %>% 
       arrange(X, Y, Val) %>% 
       filter((c(1,diff(X)) != 0 & c(1, diff(Y)) !=0) | Val != 'Other')
    

    Then you could use different point marker sizes to ensure that overplotted D_1 and D_4 points are both visible.

    Original Answer

    What about collapsing all the other levels of Val like this:

    library(tidyverse)
    library(forcats)
    
    ggplot(df %>% mutate(Val=fct_other(Val,keep=c("D_1","D_4"))), aes(X,Y,col=Val)) + 
      geom_point() +
      scale_colour_manual(values=c(D_1=hcl(15,100,65),D_4=hcl(195,100,65),Other="grey70")) +
      theme_bw()
    

    You could also use size to make the desired points stand out more. For this particular data set, this approach also ensures that we can see a couple of D_1 and D_4 points that were hidden behind grey points in the previous plot.

    ggplot(df %>% mutate(Val=fct_other(Val,keep=c("D_1","D_4"))), aes(X,Y,col=Val, size=Val)) + 
      geom_point() +
      scale_colour_manual(values=c(D_1=hcl(15,100,65),D_4=hcl(195,100,65),Other="grey70")) +
      scale_size_manual(values=c(D_1=3, D_4=3, Other=1)) +
      theme_bw()
    

提交回复
热议问题