用R将条形图转换成饼形图

时间:2022-12-03 17:56:08

I have following data and code:

我有以下数据和代码:

dd
  grp categ condition value
1   A     X         P     2
2   B     X         P     5
3   A     Y         P     9
4   B     Y         P     6
5   A     X         Q     4
6   B     X         Q     5
7   A     Y         Q     8
8   B     Y         Q     2
> 
> 
dput(dd)
structure(list(grp = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L), .Label = c("A", "B"), class = "factor"), categ = structure(c(1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("X", "Y"), class = "factor"), 
    condition = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("P", 
    "Q"), class = "factor"), value = c(2, 5, 9, 6, 4, 5, 8, 2
    )), .Names = c("grp", "categ", "condition", "value"), out.attrs = structure(list(
    dim = structure(c(2L, 2L, 2L), .Names = c("grp", "categ", 
    "condition")), dimnames = structure(list(grp = c("grp=A", 
    "grp=B"), categ = c("categ=X", "categ=Y"), condition = c("condition=P", 
    "condition=Q")), .Names = c("grp", "categ", "condition"))), .Names = c("dim", 
"dimnames")), row.names = c(NA, -8L), class = "data.frame")


ggplot(dd, aes(grp,value, fill=condition))+geom_bar(stat='identity')+facet_grid(~categ)

用R将条形图转换成饼形图

How can I convert this bar chart to pie chart? I want 4 pies here with their sizes corresponding to heights of respective bars here. I tried following but they did not work:

如何将这个条形图转换成饼形图?我想要4个派它们的大小对应于这里各条的高度。我试着跟随,但没有成功:

ggplot(dd, aes(grp,value, fill=condition))+geom_bar(stat='identity')+facet_grid(~categ)+coord_polar()
ggplot(dd, aes(grp,value, fill=condition))+geom_bar(stat='identity')+facet_grid(~categ)+coord_polar('y')

I also tried to make pie chart similar to Pie charts in ggplot2 with variable pie sizes but I am not able to manage with my data. Thanks for your help.

我还尝试做一个饼图,类似于ggplot2中的饼图,但我无法处理我的数据。谢谢你的帮助。

2 个解决方案

#1


1  

Using the same idea as in the link you posted, you could add a column size do your dataframe that would be the sum of the values for each group, and use that as the width argument:

使用与您发布的链接相同的思想,您可以添加一个列大小的dataframe,即每个组的值的总和,并将其作为宽度参数:

library(dplyr)
dd<-dd %>% group_by(categ,grp) %>% mutate(size=sum(value))
ggplot(dd, aes(x=size/2,y=value,fill=condition,width=size))+geom_bar(position="fill",stat='identity')+facet_grid(grp~categ)+coord_polar("y")

用R将条形图转换成饼形图

#2


1  

You want the group and category both to be variables for the grid, and not inside any plot. Here are two different layouts. X ought to be any single item, string, or something else.

您希望组和类别都是网格的变量,而不是任何绘图中的变量。这里有两种不同的布局。X应该是任何单个项、字符串或其他东西。

ggplot(dd, aes(x=factor(1),y=value,
 fill=condition))+geom_bar(stat='identity')+
 facet_grid(~grp+categ)+coord_polar("x")
ggplot(dd, aes(x=factor(1),y=value,
 fill=condition))+geom_bar(stat='identity')+
 facet_grid(grp~categ)+coord_polar("x")

Something strange happened with the top opening here, maybe its just my interface. Should get you going enough though!

顶部的开口发生了一些奇怪的事情,也许这只是我的界面。不过,你应该走得够远的!

用R将条形图转换成饼形图

用R将条形图转换成饼形图

#1


1  

Using the same idea as in the link you posted, you could add a column size do your dataframe that would be the sum of the values for each group, and use that as the width argument:

使用与您发布的链接相同的思想,您可以添加一个列大小的dataframe,即每个组的值的总和,并将其作为宽度参数:

library(dplyr)
dd<-dd %>% group_by(categ,grp) %>% mutate(size=sum(value))
ggplot(dd, aes(x=size/2,y=value,fill=condition,width=size))+geom_bar(position="fill",stat='identity')+facet_grid(grp~categ)+coord_polar("y")

用R将条形图转换成饼形图

#2


1  

You want the group and category both to be variables for the grid, and not inside any plot. Here are two different layouts. X ought to be any single item, string, or something else.

您希望组和类别都是网格的变量,而不是任何绘图中的变量。这里有两种不同的布局。X应该是任何单个项、字符串或其他东西。

ggplot(dd, aes(x=factor(1),y=value,
 fill=condition))+geom_bar(stat='identity')+
 facet_grid(~grp+categ)+coord_polar("x")
ggplot(dd, aes(x=factor(1),y=value,
 fill=condition))+geom_bar(stat='identity')+
 facet_grid(grp~categ)+coord_polar("x")

Something strange happened with the top opening here, maybe its just my interface. Should get you going enough though!

顶部的开口发生了一些奇怪的事情,也许这只是我的界面。不过,你应该走得够远的!

用R将条形图转换成饼形图

用R将条形图转换成饼形图