tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.49k stars 2.02k forks source link

Faceting error in ggplot2/ggrepel: Viewport has zero dimension(s) #2402

Closed cinkova closed 6 years ago

cinkova commented 6 years ago

I am getting this error message when faceting a plot by some variables: Error in grid.Call(C_convert, x, as.integer(whatfrom), as.integer(whatto), : Viewport has zero dimension(s) This error has already been reported e.g. here as well as in Issue #2277, which you closed due to missing reprex. So here is my reprex instead, producing the same error. I have tried this both in RMarkdown and in the console.

While I understand that finding a general solution can be hard, I would highly appreciate any workaround for this particular data case. Many thanks for considering this issue!

library(ggplot2)
library(ggrepel)
depteachers <-  read.csv("http://bit.ly/2mC8106", fileEncoding = "UTF-8")
head(depteachers,2)

p <- ggplot(depteachers, aes(x = 1, y = 1, size = Count, label = teacher, col = school)) +
geom_text_repel(min.segment.length = 0, segment.size = 0, force = 30, segment.colour = NA) +
scale_size(range = c(1, 3), guide = FALSE) +
scale_y_continuous(breaks = NULL, limits = c(0,1)) +
scale_x_continuous(breaks = NULL, limits = c(0,900)) +
labs(x = '', y = '') 
this_works <- p + facet_wrap(~ school, ncol = 2)
this_works #this plot prints
this_fails <- p + facet_wrap(~ dept_lemr, ncol = 2) #some gg object gets created, no warnings
this_fails #this plot fails to print and throws the error message
reprex()
batpigandme commented 6 years ago

Hi @cinkova , could you please turn this into a reprex using the reprex package? One of the nicest features is that it automatically generates and uploads the figure/plots, which makes it easier to follow the issue, and see what's going on without having to run the code every time. Thanks.

batpigandme commented 6 years ago

The help section for the Tidyverse (https://www.tidyverse.org/help/) explains how to make a reprex and (imho) gives a clearer explanation of the copy paste step. Whatever is rendered in your viewer is already on your clipboard after you run the reprex, so you literally just come back here and paste, and your whole reprex will be there. It's also important to try to get the issue down to a "minimal" form (that way it's easier to troubleshoot, and, in doing so, you'll be able to tell what's related to your dataset, as opposed to being a bug).

cinkova commented 6 years ago
library(ggplot2)
library(ggrepel)
depteachers <-  read.csv("http://bit.ly/2mC8106", fileEncoding = "UTF-8")
head(depteachers,2)
#>   X                  dept_lemr school         teacher Count
#> 1 1 andragogika personál řídit     uk Bártlová_Tereza     2
#> 2 2   anglický jazyk didaktika     uk    Malá_Markéta     2

p <- ggplot(depteachers, aes(x = 1, y = 1, size = Count, label = teacher, col = school)) +
geom_text_repel(min.segment.length = 0, segment.size = 0, force = 30, segment.colour = NA) +
scale_size(range = c(1, 3), guide = FALSE) +
scale_y_continuous(breaks = NULL, limits = c(0,1)) +
scale_x_continuous(breaks = NULL, limits = c(0,900)) +
labs(x = '', y = '') 
this_fails <- p + facet_wrap(~ dept_lemr, ncol = 2) #some gg object gets created, no warnings
this_fails #this plot fails to print and throws the error message
#> Error in grid.Call(C_convert, x, as.integer(whatfrom), as.integer(whatto), : Viewport has zero dimension(s)

hadley commented 6 years ago

Thanks for providing some code that illustrates the problem — it's a great start 😄 However, your code is currently a bit too complicated: there's quite a bit of extraneous stuff in there that doesn't seem directly related to the problem. Can you please try and simplify your example some more? The more minimal you can make the reprex, the faster I can identify the problem and fix it.

In this case you could start by removing all the scale and label adjustments and making the sample data simpler (and inlining it in the reprex)

cinkova commented 6 years ago

I'm sorry for the complicated code. This time I have minimized it to a point where the plot would not even convey all aesthetic mapping. I hope my code is useful now. The simplified data look like this (I have removed the school variable and rownames) :

#head(depteachers,2)
# dept_lemr                                     teacher     Count
# andragogika personál řídit             Bártlová_Tereza      2
# anglický jazyk didaktika                 Malá_Markéta       2

Now this is the plotting:

library(ggplot2)
library(ggrepel)
depteachers <-  read.csv("http://bit.ly/2mC8106", fileEncoding = "UTF-8")
p <- ggplot(depteachers, aes(x = 1, y = 1, label = teacher)) +
geom_text_repel()# +
p + facet_wrap(~ dept_lemr)

Interestingly, when I left out the commands on scales and the color aesthetics, the faceting sometimes worked (again, I tried both RStudio RMarkdown and Console). You even see it working in reprex, although it had thrown an error message in both RMarkdown and Console.

However, the facets were smaller than the labels, so the plot was useless. It always failed to print anything whenever I restricted the number of wrapping columns (I tried 4 and 10) -- see the reprex below.

library(ggplot2)
library(ggrepel)
depteachers <-  read.csv("http://bit.ly/2mC8106", fileEncoding = "UTF-8")
#head(depteachers,2)
# dept_lemr                             teacher          Count
# andragogika personál řídit            Bártlová_Tereza     2
# anglický jazyk didaktika              Malá_Markéta        2

p <- ggplot(depteachers, aes(x = 1, y = 1, label = teacher)) +
geom_text_repel()# +

p + facet_wrap(~ dept_lemr, ncol = 4)
#> Error in grid.Call(C_convert, x, as.integer(whatfrom), as.integer(whatto), : Viewport has zero dimension(s)

batpigandme commented 6 years ago

Because the plot contains 132 facets, it takes a while to produce. In some cases, the command prompt returns before the plot shows up in full, but if you wait a few more seconds, it shows up for me. I think, perhaps due to its size, I'm not having much success in the way of getting reprex to do the auto upload of the figure.

The key thing you have to control is the size of the labels/text in your geom_text_repel(), which is part of ggrepel. For example (and, yes, I know this is ridiculously small), but when I ran this (note that the text size for the labels is controlled using size = 1, you can always see the parameters by running ?function_name):

library(ggplot2)
library(ggrepel)
depteachers <-  read.csv("http://bit.ly/2mC8106", fileEncoding = "UTF-8")
p <- ggplot(depteachers, aes(x = 1, y = 1, label = teacher)) +
  geom_text_repel(size = 1)
p + facet_wrap(~ dept_lemr) +
  theme_classic(base_size = 8)  # controls rest of font size
# note: the theme doesn't have to be classic that's for ease of reading

This is what generates in the Plot pane:

image

Once you zoom, and make the window big enough (which, in this case, is pretty darn big— so, again, you have to give it a few seconds to generate), you get this:

image

Running your code as-is, in essence, does the exact same thing, it just has larger text, and, thus, is a tighter fit (if it doesn't look that way, it's because I've expanded the window to ~the full height of my screen, which is big):

library(ggplot2)
library(ggrepel)
depteachers <-  read.csv("http://bit.ly/2mC8106", fileEncoding = "UTF-8")
p <- ggplot(depteachers, aes(x = 1, y = 1, label = teacher)) +
geom_text_repel()# +
p + facet_wrap(~ dept_lemr)

image image

As for ncol = 4, I'm getting an error as well if I have my RStudio panes laid out as I notmally do, but I'm not all that surprised, given you're asking the Viewport to deal with 33 rows of faceted plots. If I change up the layout (making the bottom pane really big — this is on a 27" monitor), it doesn't error out:

image
cinkova commented 6 years ago

Dear Mara, thanks a lot! Now it's working. Silvie

From: "Mara Averick" notifications@github.com To: "tidyverse/ggplot2" ggplot2@noreply.github.com Cc: "Silvie Cinkova" cinkova@ufal.mff.cuni.cz, "Mention" mention@noreply.github.com Sent: Sunday, 21 January, 2018 13:40:36 Subject: Re: [tidyverse/ggplot2] Faceting error in ggplot2/ggrepel: Viewport has zero dimension(s) (#2402)

Because the plot contains 132 facets, it takes a while to produce. In some cases, the command prompt returns before the plot shows up in full, but if you wait a few more seconds, it shows up for me. I think, perhaps due to its size, I'm not having much success in the way of getting reprex to do the auto upload of the figure.

The key thing you have to control is the size of the labels/text in your geom_text_repel() , which is part of [ https://github.com/slowkow/ggrepel | ggrepel ] . For example (and, yes, I know this is ridiculously small), but when I ran this (note that the text size for the labels is controlled using size = 1 , you can always see the parameters by running ?function_name ): library( ggplot2 ) library( ggrepel ) depteachers <- read.csv( " http://bit.ly/2mC8106 " , fileEncoding = " UTF-8 " ) p <- ggplot( depteachers , aes( x = 1 , y = 1 , label = teacher )) + geom_text_repel( size = 1 ) p + facet_wrap( ~ dept_lemr )

  • theme_classic( base_size = 8 ) # controls rest of font size # note: the theme doesn't have to be classic that's for ease of reading

This is what generates in the Plot pane: [ https://user-images.githubusercontent.com/831732/35194146-59e88358-fe7c-11e7-933e-1e824cea9754.png ]

Once you zoom, and make the window big enough (which, in this case, is pretty darn big— so, again, you have to give it a few seconds to generate), you get this: [ https://user-images.githubusercontent.com/831732/35194155-8a544c16-fe7c-11e7-8287-263b6f91f510.png ]

Running your code as-is, in essence, does the exact same thing, it just has larger text, and, thus, is a tighter fit (if it doesn't look that way, it's because I've expanded the window to ~the full height of my screen, which is big): library( ggplot2 ) library( ggrepel ) depteachers <- read.csv( " http://bit.ly/2mC8106 " , fileEncoding = " UTF-8 " ) p <- ggplot( depteachers , aes( x = 1 , y = 1 , label = teacher )) + geom_text_repel() # + p + facet_wrap( ~ dept_lemr )

[ https://user-images.githubusercontent.com/831732/35194162-b4d4a2ce-fe7c-11e7-9e7c-41c501855466.png ] [ https://user-images.githubusercontent.com/831732/35194171-e46f2d74-fe7c-11e7-99ff-5513a757e154.png ]

As for ncol = 4 , I'm getting an error as well if I have my RStudio panes laid out as I notmally do, but I'm not all that surprised, given you're asking the Viewport to deal with 33 rows of faceted plots. If I change up the layout (making the bottom pane really big — this is on a 27" monitor), it doesn't error out: [ https://user-images.githubusercontent.com/831732/35194227-00d87410-fe7e-11e7-9209-eb8ab348c979.png ]

— You are receiving this because you were mentioned. Reply to this email directly, [ https://github.com/tidyverse/ggplot2/issues/2402#issuecomment-359245430 | view it on GitHub ] , or [ https://github.com/notifications/unsubscribe-auth/AGqdNdyCWkAskAE1RZ0uY02pTZmMFKZYks5tMzBEgaJpZM4Rdm90 | mute the thread ] .

-- Silvie Cinková, Ph.D. Institute of Formal and Applied Linguistics Faculty of Mathematics and Physics Charles University in Prague Malostranské nám. 25 118 00 Praha 1 Czech Republic Phone: +420 22191 4224 Mobile phone: +420 723 464 449 Web: http://ufal.mff.cuni.cz/~cinkova

lock[bot] commented 6 years ago

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/