2014-12-01 47 views
3

中心我無法弄清楚,爲什麼ggplot地塊整體在以下情節的中間:孔在餅圖與ggplot

enter image description here

這裏的數據和代碼:

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) 

回答

9

如果添加參數width = 1geom_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

+0

謝謝你......這是我最後一次檢查的東西-.-' – 2014-12-02 10:58:47

1

另一種方法的基礎上,在幫助手冊的例子,是設置x 1,所謂的「虛擬」值:

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

有時,使用方便aes_string()相反:

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

截至2016年5月,其輸出結果與SvenHehenstein在接受的答案中所顯示的完全相同。

我不能評論在aes()以內有x = 1還是在geom_bar()以內有width = 1更合適。

+0

由於''ggplot2''經常更新,這兩種方法中的一種可能會在將來變得過時,所以我想它可能是有助於列出第二種方法。 – PatrickT 2016-05-17 19:31:08