gpt4 book ai didi

r - 如何使用饼图来使用facet_wrap?

转载 作者:行者123 更新时间:2023-12-02 21:24:48 27 4
gpt4 key购买 nike

Seasons Frequency1  Frequency2
DJF 497 500
JJA 999 700
MAM 695 2000
SON 245 1000

嗨,我正在使用此数据框制作多个饼图(一个使用Frequency1,另一个使用Frequency2)。我知道如何使用melt和facet_wrap(~id)。但这里我必须汇集数据(不知道如何表达)。我不想按季节进行 Facet_wrap。

如何以可以并排制作两个饼图的方式融合数据。我仅使用Frequency1 制作了一个。

enter image description here

谢谢!

最佳答案

这是一种使用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)

enter image description here

关于r - 如何使用饼图来使用facet_wrap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25372055/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com