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

Incorrect 'plot_background' area (with empty space capture) #918

Closed
OLarionova-HORIS opened this issue Nov 1, 2023 · 3 comments
Closed
Assignees
Milestone

Comments

@OLarionova-HORIS
Copy link
Contributor

from lets_plot import *
LetsPlot.setup_html()

import numpy as np
np.random.seed(42)

data = {'x': np.random.randint(10, size=50)}

ggplot(data, aes(x='x')) + \
    geom_bar() + \
    coord_fixed(0.5) + \
    theme(plot_background=element_rect(size=2, fill='light_grey'))

The filled area corresponds to the overall size of the plot:
Screenshot 2023-11-01 at 13 48 40

It's more correct not to take the empty space around - use the plotting area with all titles and margins.
The expected result will be like this:
Screenshot 2023-11-01 at 13 48 25

@alshan alshan modified the milestones: Backlog, New Nov 2, 2023
@alshan alshan modified the milestones: New, 2024Q1 Dec 29, 2023
@IKupriyanov-HORIS IKupriyanov-HORIS self-assigned this Mar 11, 2024
@IKupriyanov-HORIS
Copy link
Collaborator

IKupriyanov-HORIS commented Mar 14, 2024

from lets_plot import *
LetsPlot.setup_html()

margins = theme(plot_margin=40)

p = ggplot() \
    + geom_point(aes(x=[10, 20, 30], y=[0, 10, 20])) \
    + theme(plot_background=element_rect(size=6, fill='light_grey', color='black'))

gggrid([
    p + ggtitle('Default'),
    p + margins + ggtitle('plot_margin=40'),
    p + margins + coord_fixed(0.5) + ggtitle('plot_margin=40 + coord_fixed()'),
    p + margins + coord_fixed(0.5) + facet_grid(x='x') + ggtitle('plot_margin=40 + facet_grid')
])

Before:
image

After:
image

@IKupriyanov-HORIS
Copy link
Collaborator

IKupriyanov-HORIS commented Mar 14, 2024

Looks like background should be expanded by the margin.

ggplot2 version for reference:

df <- data.frame(
    x = c(10, 10, 20, 20, 30, 30),
    y = c(1, 10, 2, 8, 3, 5)
)

p = ggplot(df) + 
    geom_point(aes(x=x, y=y)) + 
    theme(
        plot.margin=margin(2, 2, 2, 2, "cm"), 
        panel.background=element_rect(fill='orange'), 
        plot.background=element_rect(fill='grey')
    ) + 
    facet_grid(cols=vars(x)) + 
    ggtitle("Default")
p

image

p + coord_fixed() + ggtitle("coord_fixed()")

image

@IKupriyanov-HORIS
Copy link
Collaborator

from lets_plot import *
LetsPlot.setup_html()

margins = theme(plot_margin=[5, 0, 0, 40])

p = ggplot() \
    + geom_point(aes(x=[0, 1, 2], y=[1, 2, 3])) \
    + theme(
        plot_background=element_rect(size=5, fill='light_grey', color='grey'),
        panel_background=element_rect(fill='orange'),
        panel_grid_minor=element_line(),
    )

dump_plot(gggrid([
    p + ggtitle('Default'),
    p + coord_fixed(0.3) + ggtitle('coord_fixed()'),
    p + margins + ggtitle('plot_margin=[5, 0, 0, 40]'),
    p + margins + coord_fixed(0.3) + ggtitle('plot_margin=[5, 0, 0, 40] + coord_fixed()'),
], ncol=2))

Result:
image

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

3 participants