Vindaar / ggplotnim

A port of ggplot2 for Nim
https://vindaar.github.io/ggplotnim
MIT License
176 stars 15 forks source link

Can the long plot title be auto-wrapped? #95

Open kaushalmodi opened 3 years ago

kaushalmodi commented 3 years ago

Hello,

I was just using this package to dynamically construct the plot titles and often the titles would get longer than the image widths and get truncated. Here's a simple example using the first example in the recipe.org:

import ggplotnim, sequtils, seqmath

let x = linspace(0.0, 30.0, 1000)
let y = x.mapIt(pow(sin(it), 2.0))

let df = seqsToDf(x, y)

ggplot(df, aes("x", "y")) +
  geom_line() +
  ggtitle("This is a very long title which gets cropped on the right side as it's longer than the image width.") +
  ggsave("plot.png")

This will create

image

This is using the latest release of ggplotnim, 0.3.18.

I tried using \n and \\n in the string passed to ggtitle but that didn't work.

Can you enable auto-wrapping of the title, and/or allow a way to insert newlines in the title?

Thanks!

Vindaar commented 3 years ago

Hey! Great to hear from you. :)

That's a good point. I've thought about this myself sometimes. I just wasn't sure if breaking the line is the most desired behavior. That is mainly because of the amount of space in the header part by default.

I've already implemented manual line wrapping support for the annotations. So it shouldn't be too hard to apply this here. Auto wrapping should also be possible, because I can determine the width of a string on a given viewport. I suppose I could break at the last space before the width is exceeded.

I'll give this a try! Don't want to make any promises, but I'll put it into the PR that's been open for a week or so (plus some other stuff I have sitting locally already). Sometime next week at the latest.

Vindaar commented 3 years ago

Finally gave it a try to implement this morning. Was indeed pretty easy.

The only thing I did not implement (because I think it's not desired behavior by default) is to extend the region of the header to accommodate multiple lines. So in case one expects multiple lines in the header, it's required to either set a smaller font thane the default (using the titleFont argument of ggtitle) or increase the header region's size using margin(top = <some value in cm>").

I'll push this to the PR now.

Vindaar commented 3 years ago

93 is merged now. Let me know if the behavior is what you expect and if it could be nicer in some way!

See the recipe here: https://github.com/Vindaar/ggplotnim/blob/master/recipes.org#plot-with-a-title-including-line-breaks

kaushalmodi commented 3 years ago

Thanks! I will try this out today and let you know.

Looking at your example (and I know you said that I need to specify the margin manually), one minor suggestion would be to auto-increase the margin as needed. The reason is that I am dynamically creating the title, and probably margin=2 will work for most cases. Just in case only margin=1 is enough, or if more lines are needed, can ggplotnim figure that out automatically?

Vindaar commented 3 years ago

one minor suggestion would be to auto-increase the margin as needed

I thought about that. In principle I can do that fine. The reason why I decided against it is because by increasing the margin I have to do one of two things:

Now that you mention it however, it would probably be a good idea to allow auto resizing by supplying an argument to the ggtitle procedure. I'll implement that in the next few days!

kaushalmodi commented 3 years ago

I updated ggplotnim from HEAD and I confirm the auto-wrapping and margin option. Thanks!

For now, I will start using margin=2 for my plots. But if the auto-margin works out, it would be great! :)