r - ggplot donut chart

标签 r ggplot2 graphics tikz donut-chart

嗨,我真的用谷歌搜索了很多,没有任何快乐。如果存在网站,将很高兴获得对网站的引用。我很难理解 Hadley documentation on polar coordinates而且我知道饼图/ donut chart 本质上被认为是邪恶的。

也就是说,我想做的是

  • 创建一个像 tikz ring chart 这样的圆环图/环形图(所以中间是空的馅饼)此处显示
  • 在顶部添加第二层圆圈(带有 alpha=0.5 左右),显示第二个(可比较的)变量。

  • 为什么?我正在寻找显示财务信息。第一个环是成本(分解),第二个是总收入。然后的想法是添加+ facet=period为每个审查期间显示收入和支出的趋势以及两者的增长。

    任何想法将不胜感激

    注意:如果尝试使用 MWE,则完全任意
    donut_data=iris[,2:4]
    revenue_data=iris[,1]
    facet=iris$Species
    

    那将类似于我正在尝试做的事情..谢谢

    最佳答案

    我没有你的问题的完整答案,但我可以提供一些代码,可以帮助你开始使用 ggplot2 制作环形图.

    library(ggplot2)
    
    # Create test data.
    dat = data.frame(count=c(10, 60, 30), category=c("A", "B", "C"))
    
    # Add addition columns, needed for drawing with geom_rect.
    dat$fraction = dat$count / sum(dat$count)
    dat = dat[order(dat$fraction), ]
    dat$ymax = cumsum(dat$fraction)
    dat$ymin = c(0, head(dat$ymax, n=-1))
    
    p1 = ggplot(dat, aes(fill=category, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) +
         geom_rect() +
         coord_polar(theta="y") +
         xlim(c(0, 4)) +
         labs(title="Basic ring plot")
    
    p2 = ggplot(dat, aes(fill=category, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) +
         geom_rect(colour="grey30") +
         coord_polar(theta="y") +
         xlim(c(0, 4)) +
         theme_bw() +
         theme(panel.grid=element_blank()) +
         theme(axis.text=element_blank()) +
         theme(axis.ticks=element_blank()) +
         labs(title="Customized ring plot")
    
    
    library(gridExtra)
    png("ring_plots_1.png", height=4, width=8, units="in", res=120)
    grid.arrange(p1, p2, nrow=1)
    dev.off()
    

    enter image description here

    感想:
  • 如果您发布一些结构良好的示例数据,您可能会得到更有用的答案。您提到使用 iris 中的一些列dataset (一个好的开始),但我无法看到如何使用该数据来制作环形图。例如,您链接到的环形图显示了多个类别的比例,但 iris[, 2:4]也不是 iris[, 1]是分类的。
  • 您要“在顶部添加第二层圆圈”:您的意思是将第二个环直接叠加在第一个环的顶部吗?或者您希望第二个环位于第一个环的内部还是外部?您可以添加第二个内部环,例如 geom_rect(data=dat2, xmax=3, xmin=2, aes(ymax=ymax, ymin=ymin))
  • 如果您的 data.frame 有一个名为 period 的列, 你可以使用 facet_wrap(~ period)用于刻面。
  • 使用 ggplot2最容易的是,您会希望您的数据为“长格式”; melt()来自 reshape2包可能对转换数据有用。
  • 制作一些条形图进行比较,即使您决定不使用它们。例如,尝试:
    ggplot(dat, aes(x=category, y=count, fill=category)) + geom_bar(stat="identity")
  • 关于r - ggplot donut chart ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13615562/

    相关文章:

    r - data.table:为什么不能总是总是直接传递列名?

    r - ggplot2:轴上的花括号?

    r - 将命名参数列表传递给函数?

    r - 迭代列中的唯一值时如何避免 for 循环 [R]

    r - geom_density 返回比预期更多的观测值

    r - 更改 ggplot 堆积面积图中的堆积顺序

    r - geom_map "map_id"函数如何工作?

    java - 使用隐式 Graphics 参数调用 Java 方法,例如 repaint() 或访问 JPanel 中的 Graphics

    vb.net - VB.NET中的渐变填充

    css - 各种 Adob​​e Fireworks CS4 查询