Effective way of plotting additional points to an exisiting plot

前端 未结 2 1360
太阳男子
太阳男子 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()
    

    0 讨论(0)
  • 2021-01-25 09:38

    Building a bit on eipi10's post

    library(tidyverse)
    library(forcats)
    theme_set(theme_bw(base_size=12)+ 
            theme(panel.grid.major = element_blank(),
                  panel.grid.minor = element_blank()))
    
    df <- data_frame(X=rnorm(100,0,1), Y=rnorm(100,0,1), 
                 ID=paste(rep("ID", 100), 1:100, sep="_"),
                 Type=rep("ID",100),
                 Val=c(rep(c('Type1','Type2'),30),
                       rep(c('Type3','Type4'),20)))
    
    f.df <- function(x, type){
              type = deparse(substitute(type))
              df[sample(1:100,20), 1:3] %>% 
              mutate(Type=rep(type, 20),
                     Val=paste(rep(type, 20),
                         sample(1:6,20, replace=T), sep="_"))
    }
    
    dat1 <- f.df(df, CT)
    dat2 <- f.df(df, D)
    dat3 <- f.df(df, OP)
    
    df2 <- bind_rows(df, dat1, dat2, dat3)
    
    df2 %>% 
       mutate(Group = fct_other(Val,keep=c("D_1","D_4"))) %>% 
       ggplot(aes(X,Y,color=Group)) + geom_point() +
       scale_colour_manual(values=c(D_1=hcl(15,100,65),D_4=hcl(195,100,65), 
                           Other="grey70")) 
    
    0 讨论(0)
提交回复
热议问题