Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hole in the center of a pie chart with ggplot

I cant figure out, why ggplot plots a whole in the middle of the following plot:

enter image description here

Here the data and the code:

dat15 <- data.frame("Insgesamt" = c(64, 20, 13, 3),
              "18-29" = c(41, 25, 27, 7),
              "30-44" = c(58, 25, 12, 5),
              "45-59"=c(69, 20, 10, 1),
              "60+" = c(76, 14, 9, 1),
              "Arbeiter" = c(57, 34, 9, 0),
              "Angestellte" = c(69, 17, 11, 3),
              "Beamte" = c(72, 12, 11, 5),
              "Selbstständige" = c(69, 23, 5, 3),
              "unter 1000" = c(47, 30, 19, 4),
              "1000-2000" = c(59, 24, 15, 2),
              "2000-3000" = c(72, 15, 10, 3),
              "3000+" = c(68, 19, 10, 3),
              "seit Geburt" = c(65, 19, 12, 4),
              "zugez. vor 20" = c(72, 17, 9, 2),
              "zugez. in 20" = c(46, 28, 19, 7),
              row.names = c("zum Vorteil", "zum Nachteil", "keine Veränderung", "weiß nicht"))

dat15 <- melt(dat15)
dat15$type = c("zum Vorteil", "zum Nachteil", "keine Veränderung", "weiß nicht")
dat15.1 <- dat15[c(1:4),]
dat15.1$labelpos <- cumsum(dat15.1$value) - dat15.1$value / 2


plot15.1 <- ggplot()  + 
    theme_m(base_family = family,base_size=size) + xlab("") + ylab("") 

plot15.1 <- plot15.1 + 
    geom_bar(ata = dat15.1, aes(x = dat15.1$variable, y = dat15.1$value, 
        fill=dat15.1$type), stat = 'identity')

plot15.1 <- plot15.1 + coord_polar("y", start = 0)
like image 380
Martin Schmelzer Avatar asked Dec 01 '14 15:12

Martin Schmelzer


2 Answers

It will work if you add the argument width = 1 to geom_bar:

ggplot()  + 
  geom_bar(data = dat15.1, aes(x = variable, y = value, fill = type), 
           stat = 'identity', width = 1) + 
  coord_polar("y", start = 0)

enter image description here

like image 199
Sven Hohenstein Avatar answered Oct 19 '22 17:10

Sven Hohenstein


Another approach, based on examples in the help manual, is to set the x to 1, a so-called 'dummy' value:

ggplot()  + 
    geom_bar(data = dat15.1, aes(x = 1, y = value, fill = type), 
             stat = 'identity') + 
    coord_polar("y", start = 0)

Sometimes, it is convenient to use aes_string() instead:

ggplot()  + 
    geom_bar(data = dat15.1, aes_string(x = 1, y = 'value', fill = 'type'), 
             stat = 'identity') + 
    coord_polar("y", start = 0)

As of May 2016, the output is exactly as shown by Sven Hohenstein in the accepted answer.

I cannot comment on whether it is more appropriate to have x = 1 inside the aes() or to have width = 1 inside the geom_bar().

like image 39
PatrickT Avatar answered Oct 19 '22 18:10

PatrickT