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

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

Open
kaushalmodi opened this issue Oct 24, 2020 · 6 comments
Open

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

kaushalmodi opened this issue Oct 24, 2020 · 6 comments

Comments

@kaushalmodi
Copy link

kaushalmodi commented Oct 24, 2020

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
Copy link
Owner

Vindaar commented Oct 24, 2020

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
Copy link
Owner

Vindaar commented Oct 30, 2020

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
Copy link
Owner

Vindaar commented Oct 31, 2020

#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
Copy link
Author

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
Copy link
Owner

Vindaar commented Oct 31, 2020

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:

  • either change the size of the final plot
  • or change the aspect ratio of the plot viewport
    Having either of the two (the first one is crazy, the second would be ok) as a default seemed to extreme to me.

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
Copy link
Author

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! :)

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