lovelyday
lovelyday

Reputation: 1

cant get correct mapping when use ggproto to creat a new geom function

I want draw a multisequence align result in a whole view,so i draw a picture with follow code:

library(ggmsa)
library(Biostrings)
aln_data <- system.file("extdata", "seedSample.fa", package  ="ggmsa")
df <- tidy_msa(aln_data)

###df like this
                        name position character
#1                       Homo        1         -
#2               fascicularis        1         G
#3                    mulatta        1         -
#4                       Homo        2         -
#5               fascicularis        2         T
#6                    mulatta        2         -

max_pos <- df %>% select(position) %>%  max()
height_n <- length(unique(df$name))
df$y<- rep(seq(1,height_n,1),times=max_pos)

#caculate the seq frequce 

fre_list<-list()
for (i in 1:length(unique(df$position))){
  single_pos <- df[df$position == i,]
  most_fre <- names(sort(table(single_pos$character),decreasing = T)[1])
  fre_list[[i]] <- most_fre
}

df$fre <- rep(unlist(fre_list),each=height_n)

df$y <- ifelse(df$character == df$fre,0,df$y)
df$y0 <- ifelse(df$y ==0,0,df$y-1)
ggplot(df)+geom_segment(aes(x=position,xend=position,y=y0,yend=y),color='grey',alpha=0.8)+
theme_bw()+ggplot2::theme(panel.grid=element_blank(),
                            axis.text.x = element_blank(),axis.text.y = element_blank(),
                            axis.ticks=element_blank(),axis.title.x = element_blank(),
                          axis.title.y = element_blank())+
                        scale_y_continuous(expand=c(0,0))+
                        scale_x_continuous(expand=c(0,0))

it works well ,then i want to change it to a new geom function like follow:

StatMsawind <- ggplot2::ggproto('StatMsawind',Stat,required_aes= c('x','y'),
                                
                            compute_group =function(data,scales){
                              View(data)
                              pos_number <- data %>% select(x) %>% max()
                              print(pos_number)
                              height_n <- nrow(data)/pos_number
                              print(height_n)
                              out<- data.frame('y'=rep(seq(1,height_n,1),times=pos_number))
                              out$cha <- data$y
                              fre_list<-list()
                              for (i in 1:pos_number){
                                single_pos <- data[data$x == i,]
                                most_fre <- names(sort(table(single_pos$y),decreasing = T)[1])
                                fre_list[[i]] <- most_fre
                              }
                              
                              out$fre <- rep(unlist(fre_list),each=height_n)
                              
                              out$y <- ifelse(out$cha == out$fre,0,out$y)
                              out$ystart <- ifelse(out$y == 0,0,out$y-1)
                              out
                              print(out)
                          })

stat_msawind <- function(data=NULL,mapping =NULL,geom = 'msawind',position='identity',
                         inherit.aes=TRUE,...){
  ggplot2::layer(stat = StatMsawind, data = data, mapping = mapping, geom = geom,
                 position = position,inherit.aes = inherit.aes,
                 params = list(...))
}


GeomMsawind <- ggplot2::ggproto('GeomMsawind',ggplot2::Geom,
                                required_aes=c('x','y','ystart'),
                                default_aes=aes(color='grey',alpha=0.8),
                                draw_key = draw_key_abline,
                                draw_panel = function(data,panel_scales,coord){
                                  coords <- coord$transform(data,panel_scales)
                                  print(coords)
                                  grid::segmentsGrob(x0=coords$x,x1=coords$x,y0=coords$ystart,y1=coords$y,
                                                     gp=grid::gpar(col=ggplot2::alpha(coords$color,coords$alpha)))
                                })

geom_msawind <- function(data=NULL,mapping =NULL,stat='msawind',position='identity',inherit.aes=TRUE,...){
  ggplot2::layer(data = data,mapping = mapping,stat = stat, geom=GeomMsawind,position=position,inherit.aes = inherit.aes,
                 params = list(color='grey',alpha=0.8,...))
}


ggplot(df,aes(position,character))+geom_msawind()

i get this error:

Warning message:
Computation failed in `stat_msawind()`:
wrong sign in 'by' argument

when i chcek the data in StatMsawind function ,i find that the data shape is no equal to the passing variables df i send in.i can't figure out why it can't stat correct, what should i do to make it correct

thanks very much

Upvotes: 0

Views: 56

Answers (0)

Related Questions