创建一个使用ggplot2的饼图。

huangapple go评论69阅读模式
英文:

creating a pie of pie chart with ggplot2

问题

我一直在使用ggplot2制作各种图表。我有兴趣尝试制作一个嵌套饼图(我认为是这样称呼的)。它的样子会是这样的:
创建一个使用ggplot2的饼图。

我知道如何制作单个饼图,但不知道如何添加第二个饼图。如果能找到一种方法在一个楔形上再添加第三个饼图就更好了。我不排斥使用除了ggplot2以外的其他包,尽管我在Python方面并不是很熟练!

英文:

I have been using ggplot2 to make all sorts of plots. I am interested in trying to make a pie of pie chart (I think that is what it is called). It would look something like this:
创建一个使用ggplot2的饼图。

I know how to make a single pie chart, but can't figure out how to do it with adding the 2nd pie to it. It would be a bonus if there was a way to add a 3rd pie coming off from a another wedge. I'm not opposed to using other packages than ggplot2, although I'm not very proficient with python!

答案1

得分: 1

这似乎不是我知道的标准绘图类型,据我所知,没有可以实现这种设置的包。你_可以_使用ggplot2scatterpie来实现,但连接线可能需要通过试验来确定。此外,你需要确保你的数据格式正确,但由于你没有提供任何示例数据,所以很难知道你如何做到这一点,我只能在这里为你提供一个简单的演示:

library(ggplot2)
library(scatterpie)

df <- data.frame(A = c(1, 0), B = c(20, 0), C = c(5, 0),
                 D = c(50, 0), E = c(40, 0),
                 F = c(0, 20), G = c(0, 2), H = c(0, 40), 
                 I = c(0, 10), J = c(0, 35),
                 group = c('A', 'B'),
                 xpos = c(1, 5), ypos = c(1, 2), size = c(2, 1))

ggplot(df) + 
  geom_scatterpie(aes(x = xpos, y = ypos, r = size, group = group), 
                  data = df, cols = LETTERS[1:10], color = 'gray30') + 
  geom_path(data = data.frame(x = c(2.84, 4.38), y = c(1.85, 2.8)), aes(x, y),
            color = 'gray80') +
  geom_path(data = data.frame(x = c(2.99, 4.58), y = c(1.32, 1.08)), aes(x, y),
            color = 'gray80') +
  coord_equal() +
  theme_void() +
  theme(legend.position = 'none')

创建一个使用ggplot2的饼图。

英文:

This isn't really a standard plot type as far as I know, so to my knowledge there is no package that allows such a set-up. You could do it using ggplot2 and scatterpie, though I'm afraid the lines to connect the segments are a case of trial-and-error. You also need your data to be in the correct format, but since you have not provided any sample data, it's difficult to know how you would achieve this and I can only give you a simple demo here:

library(ggplot2)
library(scatterpie)

df &lt;- data.frame(A = c(1, 0), B = c(20, 0), C = c(5, 0),
                 D = c(50, 0), E = c(40, 0),
                 F = c(0, 20), G = c(0, 2), H = c(0, 40), 
                 I = c(0, 10), J = c(0, 35),
                 group = c(&#39;A&#39;, &#39;B&#39;),
                 xpos = c(1, 5), ypos = c(1, 2), size = c(2, 1))

ggplot(df) + 
  geom_scatterpie(aes(x = xpos, y = ypos, r = size, group = group), 
                  data = df, cols = LETTERS[1:10], color = &#39;gray30&#39;) + 
  geom_path(data = data.frame(x = c(2.84, 4.38), y = c(1.85, 2.8)), aes(x, y),
            color = &#39;gray80&#39;) +
  geom_path(data = data.frame(x = c(2.99, 4.58), y = c(1.32, 1.08)), aes(x, y),
            color = &#39;gray80&#39;) +
  coord_equal() +
  theme_void() +
  theme(legend.position = &#39;none&#39;)

创建一个使用ggplot2的饼图。

huangapple
  • 本文由 发表于 2023年5月25日 22:58:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定