【问题标题】:Hole in the center of a pie chart with ggplot带有ggplot的饼图中心的孔
【发布时间】:2017-05-24 22:12:27
【问题描述】:

我想不通,为什么ggplot 在下面的情节中间绘制了一个整体:

这里是数据和代码:

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)

【问题讨论】:

    标签: r ggplot2 pie-chart


    【解决方案1】:

    如果您将参数width = 1 添加到geom_bar,它将起作用:

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

    【讨论】:

    • 谢谢老兄...那是我最不想检查的东西-.-'
    【解决方案2】:

    根据帮助手册中的示例,另一种方法是将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 月,输出与 Sven Hohenstein 在接受的答案中显示的完全相同。

    我无法评论在aes() 中包含x = 1 还是在geom_bar() 中包含width = 1 是否更合适。

    【讨论】:

    • 由于ggplot2 经常更新,这两种方法中的一种将来可能会过时,所以我认为列出第二种方法可能会很有用。
    猜你喜欢
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2016-10-06
    相关资源
    最近更新 更多