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

Unequal panel border size #670

Closed
etiennebr opened this issue Sep 21, 2012 · 22 comments
Closed

Unequal panel border size #670

etiennebr opened this issue Sep 21, 2012 · 22 comments

Comments

@etiennebr
Copy link

Since I haven't found a solution, I think it could be considered as a bug.

I have a double thick line on the bottom and right borders of the panel when I plot the following code. In fact, it seems like it appears in every device, only that in vector devices it's less obvious.

library("ggplot2")
qplot(mpg, wt, data=mtcars) + opts(panel.border = theme_rect(size = 1))
@wch
Copy link
Member

wch commented Sep 21, 2012

This bug was fixed in version 0.9.2.

@wch wch closed this as completed Sep 21, 2012
@wch
Copy link
Member

wch commented Sep 21, 2012

The bug had to do with the order that panel elements were drawn. Also, you'll probably need to use fill=NA in the new version. See #659 for more info.

@etiennebr
Copy link
Author

Ok. I've updated to 0.9.2.1, but still have the same issue. Am I doing it right ?

qplot(mpg, wt, data=mtcars) + theme(panel.border=element_rect(fill=NA, size = 1))

@wch
Copy link
Member

wch commented Sep 21, 2012

That should be right... Can you post a picture somewhere, say on imgur.com?

@etiennebr
Copy link
Author

It's pretty much the same than the one in the thread...
http://imgur.com/wORTW

2012/9/21 Winston Chang notifications@github.com

That should be right... Can you post a picture somewhere, say on imgur.com
?


Reply to this email directly or view it on GitHubhttps://github.com//issues/670#issuecomment-8772368.

@wch
Copy link
Member

wch commented Sep 21, 2012

I think this is probably happening because of the lack of anti-aliasing when this is rendered with the Windows graphics device. (This image looks like it was made in Windows.) The lines, pre-rendering, don't align perfectly with the pixels locations in Windows.

See this for more information:
http://stackoverflow.com/questions/6023179/anti-aliasing-in-r-graphics-under-windows-as-per-mac

If you don't want to use that solution, you may be able to get it to look better by fiddling with different values for the line thickness. You might also try different values for png(res=xxx), or ggsave(dpi=xxx), where xxx is replaced with some value for pixels per inch (the default is 72, I believe).

@etiennebr
Copy link
Author

I'm not sure it is only anti-aliasing. When I render the plot with

library("cairoDevice")
Cairo_svg("test.svg", width = 4, height = 4)
qplot(mpg, wt, data=mtcars) + theme(panel.border=element_rect(fill=NA, size = 1))
dev.off()

And open the graph in Inkscape, the borders are still unequal (same for pdf). However, If I ungroup the elements, at a certain point the borders appear equal. It seems that one of the clip path is not correctly aligned with the width of the stroke (clip-path="url(#clip2)" in this case).
I'll give it a try on linux, but I think it is more related to plot size (it's more obvious in small plots) than OS.

@etiennebr
Copy link
Author

I confirm that I observe the same behavior if I render under linux (ubuntu).

@wch wch reopened this Sep 24, 2012
@nfdisco
Copy link

nfdisco commented Jan 30, 2013

I can confirm this bug in 0.9.3 on Linux.
Here is the output generated with

qplot(1, 1) + theme(panel.border=element_rect(fill=NA))

in different formats:
https://sites.google.com/site/nfdisco/home/panel.border.svg
https://sites.google.com/site/nfdisco/home/panel.border.png
https://sites.google.com/site/nfdisco/home/panel.border.pdf

@etiennebr
Copy link
Author

I thought I replied to this thread, however it seems I can't find my reply.
So here's a similar observation, since it might help.

It looks like the cliping window is too tight. In your svg, (
https://sites.google.com/site/nfdisco/home/panel.border.svg) the clip path
named clip20 doesn't allow the full stroke of the frame :



And the frame is


So on top and left side, you end up with a smaller stroke. in the svg, if
you suppress that clip-path, you get an nice uniform frame. However I
couldn't find the criminal in ggplot2 code.

Etienne

2013/1/30 nfdisco notifications@github.com

I can confirm this bug in 0.9.3 on Linux.
Here is the output generated with

qplot(1, 1) + theme(panel.border=element_rect(fill=NA))

in different formats:
https://sites.google.com/site/nfdisco/home/panel.border.svg
https://sites.google.com/site/nfdisco/home/panel.border.png
https://sites.google.com/site/nfdisco/home/panel.border.pdf


Reply to this email directly or view it on GitHubhttps://github.com//issues/670#issuecomment-12899692.

@wch
Copy link
Member

wch commented Jan 30, 2013

The SVG and PNG have unequal border sizes, but the PDF looks OK to me. When I run the same code on my Mac, the lines all look the same. Not sure what's causing this... Can you post images with thicker lines:

qplot(1, 1) + theme(panel.border=element_rect(fill=NA, size=2))

@etiennebr
Copy link
Author

I think it's just less obvious, but it's still there :

qplot(1, 1) + theme(panel.border=element_rect(fill=NA, size=2))

https://www.dropbox.com/s/y09nztarublyzzv/panel.border.tick.pdf
https://www.dropbox.com/s/u9fqu7ztgaa56c3/panel.border.tick.png
https://www.dropbox.com/s/9xnudgusyezricp/panel.border.tick.svg

As you mentionned, pdf doesn't seem to be affected.

Etienne

2013/1/30 Winston Chang notifications@github.com

The SVG and PNG have unequal border sizes, but the PDF looks OK to me.
When I run the same code on my Mac, the lines all look the same. Not sure
what's causing this... Can you post images with thicker lines:

qplot(1, 1) + theme(panel.border=element_rect(fill=NA, size=2))


Reply to this email directly or view it on GitHubhttps://github.com//issues/670#issuecomment-12912613.

@nfdisco
Copy link

nfdisco commented Jan 30, 2013

The PDF is affected. In the one I uploaded, the lines should be the same width as the major grid lines and instead they are thinner. It seems that is not affected because all four sides are affected, so they all look the same.

I noticed that which sides of the rectangle are affected changes in a seemingly random fashion depending on things such as the line width, the size of the plot, maybe also the order of the arguments passed to theme...

@nfdisco
Copy link

nfdisco commented Jan 31, 2013

Hi, the problem is self-evident in the following picture:
https://sites.google.com/site/nfdisco/home/panel.border.clip.png
which has been generated with

ggplot(NULL, aes(xmax=1, xmin=0, ymax=1, ymin=0)) +
  geom_rect(size=3, colour='black', fill=NA) +
  theme(panel.border=element_rect(size=3, colour='black', fill=NA, linetype=2))

(The inner rectangle is for reference.) The panel border is clipped. The code responsible for this can be found in facet-null.r:

  layout <- gtable_matrix("layout", all,
    widths = unit.c(grobWidth(axis_v), unit(1, "null")),
    heights = unit.c(unit(aspect_ratio, "null"), grobHeight(axis_h)),
    respect = respect, clip = c("off", "off", "on", "off"),
    z = matrix(c(3, 2, 1, 4), ncol = 2, byrow = TRUE)
  )

Change the "on" to "off" and the clipping disappears:
https://sites.google.com/site/nfdisco/home/panel.border.noclip.png

@etiennebr
Copy link
Author

The origin of the problem seem to be to be that frame and clip don't share
the same coordinates.
How did you plot that second figure ? I couldn't figure how to turn off the
clip.

2013/1/31 nfdisco notifications@github.com

Hi, the problem is self-evident in the following picture:
https://sites.google.com/site/nfdisco/home/panel.border.clip.png
which has been generated with

ggplot(NULL, aes(xmax=1, xmin=0, ymax=1, ymin=0)) +
geom_rect(size=3, colour='black', fill=NA) +
theme(panel.border=element_rect(size=3, colour='black', fill=NA, linetype=2))

(The inner rectangle is for reference.) The panel border is clipped. The
code responsible for this can be found in facet-null.r:

layout <- gtable_matrix("layout", all,
widths = unit.c(grobWidth(axis_v), unit(1, "null")),
heights = unit.c(unit(aspect_ratio, "null"), grobHeight(axis_h)),
respect = respect, clip = c("off", "off", "on", "off"),
z = matrix(c(3, 2, 1, 4), ncol = 2, byrow = TRUE)
)

Change the "on" to "off" and the clipping disappears:
https://sites.google.com/site/nfdisco/home/panel.border.noclip.png


Reply to this email directly or view it on GitHubhttps://github.com//issues/670#issuecomment-12949054.

@nfdisco
Copy link

nfdisco commented Jan 31, 2013

But why do you want to clip? With clipping the line width will always be wrong, even if the clip area and frame are perfectly aligned.
For the second plot you have to download the source code, change the "clip" argument in the code referenced in my previous comment, reinstall the package and restart R. On Linux:

wget http://cran.r-project.org/src/contrib/ggplot2_0.9.3.tar.gz
tar xzf ggplot2_0.9.3.tar.gz
<<change the file>>
tar czf ggplot2.tar.gz ggplot2
R CMD INSTALL ggplot2.tar.gz

@etiennebr
Copy link
Author

But why do you want to clip?

Is it possible to still keep sharp corners ? I don't really like the round
corners :)

@nfdisco
Copy link

nfdisco commented Jan 31, 2013

Not possible at the moment, but if you want sharp corners this is not the way to do it. Also, be aware that all other rectangular elements in the plot have round corners.

@hadley
Copy link
Member

hadley commented Feb 1, 2013

Clipping keeps all sorts of other elements inside the plot. It's not a trivial change unfortunately.

@nfdisco
Copy link

nfdisco commented Feb 2, 2013

I think I have got a solution, basically add the panel border in the appropriate gtable cell afterwards without clipping. I think this will still clip the other elements inside the plot, although I'm not familiar with gtable so I can be wrong.

--- ggplot2/R/facet-null.r.orig 2013-01-31 16:29:42.037012615 +0100
+++ ggplot2/R/facet-null.r  2013-02-02 17:46:21.506877897 +0100
@@ -46,8 +46,8 @@

   # Flatten layers - we know there's only one panel
   geom_grobs <- lapply(geom_grobs, "[[", 1)
-  panel_grobs <- c(list(bg), geom_grobs, list(fg))
-  
+  panel_grobs <- c(list(bg), geom_grobs)
+
   panel_grob <- gTree(children = do.call("gList", panel_grobs))  
   axis_h <- coord_render_axis_h(coord, range, theme)
   axis_v <- coord_render_axis_v(coord, range, theme)
@@ -63,7 +63,8 @@
     respect = respect, clip = c("off", "off", "on", "off"),
     z = matrix(c(3, 2, 1, 4), ncol = 2, byrow = TRUE)
   )
-  layout$layout$name <- c("axis-l", "spacer", "panel", "axis-b")
+  layout <- gtable_add_grob(layout, fg, t=1, l=2, z=2, clip="off")
+  layout$layout$name <- c("axis-l", "spacer", "panel", "axis-b", "panel-border")

   layout
 }

@nfdisco
Copy link

nfdisco commented Feb 4, 2013

Unfortunately, faceted plots are also affected and those are more difficult to fix.

@hadley
Copy link
Member

hadley commented Feb 24, 2014

This sounds like a great feature, but unfortunately we don't currently have the development bandwidth to support it. If you'd like to submit a pull request that implements this feature, please follow the instructions in the development vignette.

@hadley hadley closed this as completed Feb 24, 2014
@lock lock bot locked as resolved and limited conversation to collaborators Jun 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants