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

ggplot2: One line of panel.background always gets dropped #47

Closed
expersso opened this issue Dec 11, 2015 · 8 comments · Fixed by #55
Closed

ggplot2: One line of panel.background always gets dropped #47

expersso opened this issue Dec 11, 2015 · 8 comments · Fixed by #55

Comments

@expersso
Copy link

library(ggplot2)
library(svglite)

svglite("testplot.svg")
qplot(mtcars$cyl) + theme_bw()
dev.off()

testplot

@expersso expersso changed the title ggplot2: One line of plot.background always gets dropped ggplot2: One line of panel.background always gets dropped Dec 11, 2015
@expersso
Copy link
Author

An even more minimal example:

library(ggplot2)
library(svglite)

svglite("testplot.svg")
ggplot(NULL) + theme(text = element_blank(), 
                     axis.ticks = element_blank(),
                     plot.background = element_blank(),
                     panel.background = element_rect(color = "black"),
                     panel.grid = element_blank())
dev.off()
<?xml version='1.0' encoding='UTF-8' ?>
<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 720.00 576.00'>
<defs>
  <style type='text/css'><![CDATA[
    line, polyline, path, rect, circle {
      fill: none;
      stroke: #000000;
      stroke-linecap: round;
      stroke-linejoin: round;
      stroke-miterlimit: 10.00;
    }
  ]]></style>
</defs>
<rect width='100%' height='100%' style='stroke: none; fill: #FFFFFF;'/>
<polyline points='5.48,570.52 5.48,5.48 714.52,5.48 714.52,570.52 ' style='stroke-width: 1.07; fill: #EBEBEB;' />
</svg>

The polyline only draws three of the four sides.

sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gdtools_0.0.4      svglite_1.0.0      ggplot2_1.0.1.9003

loaded via a namespace (and not attached):
[1] labeling_0.3     colorspace_1.2-6 scales_0.3.0     plyr_1.8.3       tools_3.2.2      gtable_0.1.2     Rcpp_0.12.2     
[8] grid_3.2.2       munsell_0.4.2   

@yixuan
Copy link
Contributor

yixuan commented Feb 3, 2016

I think I figured out the cause of the problem.

The reason why one line was dropped off is that the current svglite does not support clipping, so R will calculate the boundary of the plot and remove elements that are outside of it. Due to some rounding errors one or more border lines get a chance to be removed.

If we do not provide our own clip() device function, there seems to be no way to avoid it.

@hadley hadley mentioned this issue Feb 3, 2016
@hadley
Copy link
Member

hadley commented Feb 3, 2016

@yixuan clipping shouldn't be too hard to implement, right? We can just continue to draw everything, but put it in an element that clips? (Hmmmm, after reading the svg docs a bit this might be hard because we need to declare a <clipPath> at the top of the doc)

@yixuan
Copy link
Contributor

yixuan commented Feb 3, 2016

It seems possible to implement a clip() device function in svglite: https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Clipping_and_masking, with the price that each element will gain another attribute clip-path.

@yixuan
Copy link
Contributor

yixuan commented Feb 3, 2016

I believe we can define multiple <clipPath> in the body of SVG and assign each one an ID. Then the following elements just need to refer to the proper clip path.

@hadley
Copy link
Member

hadley commented Feb 3, 2016

Oh I see you can define it just before you use it:

<g clip-rule="nonzero">
  <clipPath id="MyClip">
    <path d="..." clip-rule="evenodd" />
  </clipPath>
  <rect clip-path="url(#MyClip)" ... />
</g>

@yixuan
Copy link
Contributor

yixuan commented Feb 3, 2016

Yep. I could try to make a PR today to implement this.

@hadley
Copy link
Member

hadley commented Feb 3, 2016

That would be awesome - I want to aim for a release around Feb 12

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

Successfully merging a pull request may close this issue.

3 participants