Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new label_placer implementation #23

Open
jamarav opened this issue Jun 19, 2021 · 3 comments
Open

new label_placer implementation #23

jamarav opened this issue Jun 19, 2021 · 3 comments

Comments

@jamarav
Copy link
Contributor

jamarav commented Jun 19, 2021

Hi everyone,

First of all congratulations for this wonderful package. I have been using it for a couple of years with the package ggisoband and it has been very useful to me.
Yesterday I started to use the isolines_grob() function but I didn't get good results. The main proplem was the labels position calculated with the label_placer_minmax() function. Probably, it is a good function for many plots but when I use this function in my data a lot of labels are positioned at the plot margins. It would be very useful to add to this type of functions the possibility of evaluating how far we are from the margin of the graph. This would prevent this type of problem by moving the labels away from the margins.
My solution was to define a new label_placer() function.

label_placer_custom= function(rot_adjuster = angle_halfcircle_bottom(), ...){
  
  placer_fun <- function(line_data, ...) {
  out <- data.frame(x = numeric(0), y = numeric(0), theta = numeric(0))
  line_sections<-unique(line_data$id)
    
    for(i in 1:length(line_sections)){
      
      x<-line_data$x[line_data$id==i]
      y<-line_data$y[line_data$id==i]
      
      x_<-x[round(length(x)/2,0)]
      y_<-y[round(length(y)/2,0)]
      
      xtheta <- c(x[round(length(x)/2,0)-1], x[round(length(x)/2,0)], x[round(length(x)/2,0)+1])
      ytheta <- c(y[round(length(y)/2,0)-1], y[round(length(y)/2,0)], y[round(length(y)/2,0)+1])
      xave <- mean(xtheta)
      yave <- mean(ytheta)
      m <- cbind(xtheta - xave, ytheta - yave)
      v <- svd(m)$v
      
      out[i ,]<-list(x = x_, y = y_, theta = atan2(v[2], v[1]))
      
    }
  out$theta <- rot_adjuster(out$theta)
  out
  }
  
  
  function(lines, labels_data) {
    label_placer_simple(lines, labels_data, placer_fun)
  }
}

This function evaluates each line and it considers the different id subdivisions. Then it positions the label in the middle of each subdivision. Finally, the theta angle is calculated as in function label_placer_minmax().
Rplot01
Rplot02

@clauswilke
Copy link
Collaborator

Thanks for your comments. Just so I fully understand: Is there a specific request for a code change you're making? I provided the ability to add custom label placers because I was expecting that different people would have different requirements for where labels should go.

If the question is whether I'd consider adding this label placer to the code base, I'm open to that in principle. Feel free to open a PR and we can go from there.

@jamarav
Copy link
Contributor Author

jamarav commented Jun 20, 2021

Hi @clauswilke. Thanks for the quick response. I'm going to open a PR for my label_placer() solution. However, in my opinion the label_placer_minmax() it's a really good and smart function, but It could be better if we provide the ability to analyses the distance from the label to the margins. The objective would be to move the labels slightly to avoid being cut off at the margins. I'm not sure if it is possible to add this ability to the actual label_placer_minmax() function.

@clauswilke
Copy link
Collaborator

Feel free to try it out. I haven't thought about this problem in a long time and honestly don't really remember how the function works.

In my opinion, it's a good idea to have a collection of different label placers, as they they may all have different use cases. So I'm happy to add your label placer, and I would keep the current minmax label placer as is, but I'd also be happy to add a variation of the minmax label placer that adds additional constraints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants