[英]How can I use facet_wrap using pie charts?
Seasons Frequency1 Frequency2
DJF 497 500
JJA 999 700
MAM 695 2000
SON 245 1000
嗨,我正在使用此数据框制作多个饼图(一个使用Frequency1,另一个使用Frequency2)。 我知道如何使用melt和facet_wrap(〜id)。 但是这里我必须合并数据(不知道如何表达)。 我不想按季节进行Facet_wrap。
如何以可以并排制作两个饼图的方式融合数据。 我只使用Frequency1制作了一个。
谢谢!
这是一种使用ggplot2
带有面切割的饼图的方法。
数据:
dat <- read.table(text = "Seasons Frequency1 Frequency2
DJF 497 500
JJA 999 700
MAM 695 2000
SON 245 1000", header = TRUE)
将数据转换为长格式:
library(reshape2)
dat2 <- melt(dat)
创建图:
library(ggplot2)
ggplot(dat2, aes(x = "", y = value, fill = Seasons)) +
geom_bar(stat = "identity", width = 1, position = position_fill()) +
coord_polar(theta = "y") +
facet_wrap( ~ variable)
如果您真的想融化并使用构面,则:
library(reshape2)
molten_df <- melt(your_df, id.var='Seasons')
现在,melt_df将具有一个称为“变量”的列,您可以将其用于构面:
+facet_grid(facets=.~variable)
但是,如果只需要并排两个图表,则也可以将它们排列在一起。 我发现gridExtra软件包对此很方便:
library(gridExtra) # will also load the grid package. install these if needed.
# however you are creating your charts
plot1 <- ... #your code here
plot2 <- ... #your code here
# side by side
grid.arrange (plot1, plot2, ncol=2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.