ggrepel on polar pie chart: “Viewport has zero dimension(s)

Hi everyone - I’m trying to plot a pie chart, but I keep getting the same error when I add the ggrepel labels. I’ve searched a lot and asked classmates, but I still can’t fix it. Could someone help?

ggplot(dados_pizza, aes(ymax = ymax, ymin = ymin, xmax = 1, xmin = 0, fill = discriminacao)) +
geom_rect(color = "grey20") +
coord_polar(theta = "y") +
labs(
title = paste("Receitas Municipais", " - ", ano_escolhido),
fill = "Conta"
) +
scale_fill_manual(
values = paleta_gradual(n_distinct(dados_pizza$discriminacao)),
labels = vetor_legenda_uniao
) +

geom_text(
data = subset(dados_pizza, prop >= 0.05),
aes(x = 0.5, y = ymid, label = prop_label),
inherit.aes = FALSE,
color = "white", size = 3
) +
#this is where the error happens
ggrepel::geom_text_repel(
** data = subset(dados_pizza, 0.01 < prop & prop < 0.05),**
** aes(x = 1, y = ymid*0.5, label = prop_label),**
** inherit.aes = FALSE,**
** size = 3,**
** color = "black",**
** nudge_x = 0.5,
** box.padding = 0.2,**
** point.padding = 0.2,**
** max.overlaps = 5**
** )** +
theme_void()

Error:

Error in grid.Call(C_convert, x, as.integer(whatfrom), as.integer(whatto), : **
** Viewport has zero dimension(s)

You may have to make the plot window bigger. Click on the "viewer" tab, place your cursor on the left divider and pull to enlarge the window. You may need to do the same with the top divider.

It this does not work can you give us some sample data?

A handy way to supply data is to use the dput() function. Do dput(mydata) where "mydata" is the name of your dataset. For really large datasets probably dput(head(mydata, 100) is enough data. Paste it here between

```

```

Hi! Thank you so much! It worked, and now the code is running smoothly without that error notification. However, the arrows pointing to the percentages look a bit off, almost as if each label is being assigned to the wrong slice. Does that make sense?

The strange thing is that the code runs smoothly for other datasets.

I’ll attach a Google Doc with the issue:

Do you have any suggestions?

I think we really need to see your data. As I mentioned before perhaps the best way to supply data is to use the dput() function. Do dput(mydata) where "mydata" is the name of your dataset. For really large datasets probably dput(head(mydata, 100). Paste it here between

```

```
BTW, you can supply code this way as well though the google document approach seems fine.

It might be a good idea to include the plot you are getting to give us an idea of what you are seeing.

Got it!

This is the data:

structure(list(discriminacao = structure(1:8, levels = c("1.7.1.1.00.0.0 - Transferências Decorrentes de Participação na Receita da União", 
"1.7.1.3.00.0.0 -Transferências de Recursos do Sistema Único de Saúde ¿ SUS ", 
"1.7.1.2.00.0.0 -Transferências das Compensações Financeiras pela Exploração de Recursos Naturais", 
"1.7.1.5.00.0.0 - Transferências de Recursos de Complementação da União ao Fundo de Manutenção e Desenvolvimento da Educação Básica e de Valorização dos Profissionais da Educação ¿ FUNDEB ", 
"1.7.1.4.00.0.0 -Transferências de Recursos do Fundo Nacional do Desenvolvimento da Educação ¿ FNDE¿ ", 
"1.7.1.9.00.0.0 - Outras Transferências de Recursos da União e de suas Entidades", 
"1.7.1.6.00.0.0 - Transferências de Recursos do Fundo Nacional de Assistência Social ¿ FNAS", 
"1.7.1.7.00.0.0 - Transferências de Convênios da União e de Suas Entidades"
), class = "factor"), valor_total = c(180591395553.95, 76019818793.45, 
36732139317.1099, 25956559286.8799, 12287822871.17, 7474717386.57999, 
3418135417.50998, 1651919240.26), prop = c(0.524772845998589, 
0.220902754188076, 0.106738359432512, 0.0754260602922128, 0.0357066612141223, 
0.0217204629487394, 0.0099326141511511, 0.00480024177459824), 
    prop_label = c("52.5%", "22.1%", "10.7%", "7.5%", "3.6%", 
    "2.2%", "1.0%", "0.5%"), ymax = c(1, 0.475227154001411, 0.254324399813335, 
    0.147586040380824, 0.072159980088611, 0.0364533188744888, 
    0.0147328559257493, 0.00480024177459824), ymin = c(0.475227154001411, 
    0.254324399813335, 0.147586040380824, 0.072159980088611, 
    0.0364533188744888, 0.0147328559257493, 0.00480024177459824, 
    0), ymid = c(0.262386422999294, 0.635224223092627, 0.79904477990292, 
    0.890126989765283, 0.94569335051845, 0.974406912599881, 0.990233451149826, 
    0.997599879112701)), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame"))

Here is the plot:

1 Like

Thanks, the data seems to have arrived in good shape.

We seem to be missing a colour palate and a variable.

Error in paleta_gradual(n_distinct(dados_pizza$discriminacao)) : 
  could not find function "paleta_gradual"

and

Error: object 'ano_escolhido' not found

At the moment I am getting the same error you were getting even with the Viewer fully extended. I'm on a fairly light weight laptop and I may be hitting a memory problem. As a quick and dirty solution I stripped off the text in discriminacao keeping the account codes and I can now plot but I am getting the same problem with the labels that you have.

I'll keep poking around but I wonder, do you need a pie chart? It might be easier and better to use some other type of chart.

Thank you!

For 'ano_escolhido' you can set as "2022", and I can also sent the color palate here:

  paleta_gradual <- colorRampPalette(
    c("#2ca02c", "#1f77b4", "#800080", "#ff7f0e", "#FFD700")
  )

I think you might need one vectors that I have been using to set the labels:

vetor_legenda_uniao <- c(
  "Transferências Decorrentes de Participação na Receita da União",
  "Transferências das Compensações Financeiras pela Exploração de Recursos Naturais",
  "Transferências de Recursos do Sistema Único de Saúde ¿ SUS ",
  "Transferências de Recursos do Fundo Nacional do Desenvolvimento da Educação ¿ FNDE¿ ",
  "Transferências de Recursos de Complementação da União ao Fundo de Manutenção e Desenvolvimento da Educação Básica e de Valorização dos Profissionais da Educação ¿ FUNDEB ",
  "Transferências de Recursos do Fundo Nacional de Assistência Social ¿ FNAS",
  "Transferências de Convênios da União e de Suas Entidades",
  "Outras Transferências de Recursos da União e de suas Entidades"
)

Unfortunately, I do need the pie chart... :melting_face: It’s part of the project to generate pie chart visualizations at the federal, state, and municipal levels.

The code ran smoothly with the federal data; the problem seems to be with the municipal level so far... I haven't checked for state level yet.

I was afraid of something like that. Personally I think we should abolish pie charts with more than 3 slices.

"The code ran smoothly with the federal data".

Can you send that data as a dput()? We might be able to find a difference in the two data sets that is the problem.

It might be a good idea to start a new subject stressing the ggrepel problem since the stated problem here is solved.

Hi!

Sorry for not sending the data yet. I wasn't working with the federal data so I am asking my teammate to send it to me.

Hi!

Here is the data that it's apparently working with the code:

structure(list(discriminacao = structure(4:1, levels = c("1.1 - Receita Administrada pela RFB"
"1.3 - Arrecadação Líquida para o RGPS", "1.4 - Receitas Não Administradas pela REB",
"1.2 - Incentivos Fiscais"), class = "factor"), valor_total j c(56635025.22,
387651709377-12, 535709903233.87, 1390000448256.26), prop = с(2.44810960162796e-05,
0. 16756660178523, 0.600842575848565), prop_label prop_label = c("0.0%",
*16.8%", *23.2%"
, "60.1%"), утах = с(2.44810960162796е-05, 0.167591082881246,
0.399157424151435, 1), ymin = c(0, 2.44810960162796e-05, 0.167591082881246,
0.399157424151435), ymid = с(1.22405480081398e-05, 0.0838077819886313,
0.283374253516341, 0.699578712075717)), row-names = c (NA, -4L
"tbl",
). class = c("tbl_df", "tbl", "data. frame"))

This is how it's supposed to look like:

Thanks

We seem to have some problems in the Federal data.

discriminacao is showing an error but I am not sure why. However it is missing a **, **

We have

prop_label prop_label = c("0.0%",
*16.8%", *23.2%"
, "60.1%"

when I think it should be

prop_label = c("0.0%",
"16.8%", "23.2%"
, "60.1%")

I think

valor_total j 

should be

valor_total = 
утах = с(2.44810960162796е-05, 0.167591082881246,
0.399157424151435, 1)

is showing an error,

This number is the problem but I do not see why.

2.44810960162796е-05

In any case we have a corrupt data set.

Hi,

Thank you for letting me know and thank you for your help!

I asked my teammate to send me the RDS file and I'll try to use dput() command myself. I can also send you the RDS file if that works better for you.

Thanks. I think your dput() should be fine.

Sorry for the delay. Here is the dput()

structure(list(discriminacao = structure(4:1, levels = c("1.1 - Receita Administrada pela RFB", 
"1.3 - Arrecadação Líquida para o RGPS", "1.4 - Receitas Não Administradas pela RFB", 
"1.2 - Incentivos Fiscais"), class = "factor"), valor_total = c(56635025.22, 
387651709377.12, 535709903233.87, 1390000448256.26), prop = c(2.44810960162796e-05, 
0.16756660178523, 0.231566341270188, 0.600842575848565), prop_label = c("0.0%", 
"16.8%", "23.2%", "60.1%"), ymax = c(2.44810960162796e-05, 0.167591082881246, 
0.399157424151435, 1), ymin = c(0, 2.44810960162796e-05, 0.167591082881246, 
0.399157424151435), ymid = c(1.22405480081398e-05, 0.0838077819886313, 
0.283374253516341, 0.699578712075717)), row.names = c(NA, -4L
), class = c("tbl_df", "tbl", "data.frame"))

I got the data. Four rows of 7 variables. It seems to run correctly but I am only showing three slices of pie as for 1.2 - Incentivos Fiscais , I think prop = 0.0000244811 is simply rounding to zero.

At the moment I don't see anything particularly different between the two datasets but I'll keep poking around.
In any case, this is not working

# Load packages -----------------------------------------------------------
library(tidyverse)

# Define colour palate,  vector  & add ano_escolhido <- "1756" ------------
 paleta_gradual <- colorRampPalette(
    c("#2ca02c", "#1f77b4", "#800080", "#ff7f0e", "#FFD700")
  )
 
 vetor_legenda_uniao <- c(
  "Participação na Receita da União",
  "Compensações Financeiras",
  "Recursos do Sistema Único",
  "Recursos do Fundo Nacional",
  "Recursos de Complementação",
  "Recursos do Fundo Nacional",
  "Convênios da União e de Suas Entidades",
  "Recursos da União e de suas Entidades"
)

ano_escolhido <- "1756"

# Load raw_data and rename ------------------------------------------------
dados_pizza <-  read_csv("dados_pizza.csv")

plot1 <- ggplot(dados_pizza, aes(ymax = ymax, ymin = ymin, xmax = 1, xmin = 0, fill = discriminacao)) +
geom_rect(color = "grey20") +
coord_polar(theta = "y") +
labs(
title = paste("Receitas Municipais", " - ", ano_escolhido),
fill = "Conta"
) +
scale_fill_manual(
values = paleta_gradual(n_distinct(dados_pizza$discriminacao)),
labels = vetor_legenda_uniao
) + geom_text(data = subset(dados_pizza, prop >= 0.05),
aes(x = 0.5, y = ymid, label = prop_label),
inherit.aes = FALSE,
color = "white", size = 3
)  +
theme_void()

plot1

I am not having much luck.

I am converting the municipal and federal data sets to data.tables simply because I prefer the syntax. It seems to make no difference if I use a tibble or a data.table.

So far, I have managed to get all the labels to appear, but not in the correct places! I have no idea why these numbers are clustering the way they do unless some numbers are just too close to each other that the algorithm cannot separate them.

I'll keep plugging away at this but I would suggest starting a now thread empathizing the problem is with the {ggplot} piechart. I just never use pie charts so I am likely overlooking something obvious.

# load packages -----------------------------------------------------------
pacman::p_load(data.table, tidyverse,  ggrepel)

# Load data ---------------------------------------------------------------
DT1 <- fread("dados_pizza.csv")

# ggplot2 piechart --------------------------------------------------------

 plot1 <- ggplot(DT1, aes(x = "", y = prop, fill = disc, label = prop_label)) + 
  geom_bar(stat = "identity", width = 1) + geom_text_repel( max.overlaps = Inf) +
  coord_polar("y") + 
  theme_void( )
  

# ggplot2 scatter plot ----------------------------------------------------

plot2 <- ggplot(DT1, aes(disc, prop,label = prop_label)) +
  geom_point() + geom_text_repel()


# Simple base graphics piechart -------------------------------------------

DT1[, pie(prop, labels = prop_label)]

Here is a try with {ggpubr} rather than {ggplot}. It's not good but I think is is better than what I was getting with {ggplot}

# load packages -----------------------------------------------------------
pacman::p_load(data.table, tidyverse, ggpubr,
janitor, tinytable, here, numform, pacman)


# Load data, trim "discriminacao" and drop columns ------------------------
DTD <- fread("dados_pizza.csv")
DTD[, disc := substr(discriminacao, start = 1, stop = 14)] 

DT <- DTD[, .(disc, valor_total, prop, prop_label)]


# Plot  -------------------------------------------------------------------

plot1 <- ggpie(DT, "prop", label = "prop_label", lab.pos = c("in"), fill = "disc", 
             legend.title = "Conta") +
              theme(legend.position = "right")
 


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.