ggplot 中的列和行中的多个饼图 facet_wrap
Multiple pies facet_wrap in columns and rows in ggplot
我想要一个包含三个主要列(变量 Letter_ph)和 4 行(Variable_tr)的图,其中包含由 [=24= 中的值填充的完整饼图].
比如这个
但是我有这个,请问有什么帮助吗?谢谢
data <- tibble::tibble(
value = c(18.3,47.2,53.5,8.6,61.4,114.24,43.7,0,82.7,9.017,18.49,35.79,3.76,25.2,56.5,21.3,0,42.36,18.14,0,47.69,2.4,3.1,10.0,1.18,4.1,9.1,3.1,5.0,21.2,1.5,6.6,18.6,6.4,0,12.5),
Letter_ph = c("I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F"),
Variable_tr = rep(c(rep("A",3),rep("B",3),rep("C",3),rep("A",3),rep("B",3),rep("C",3),rep("A",3),rep("B",3),rep("C",3),rep("A",3),rep("B",3),rep("C",3))),
Type_tx = rep(c(rep("T1",9),rep("T2",9),rep("T3",9), rep("T4", 9))))
ggplot( data, aes( x = "", y = Variable_tr, fill=Type_tx ) ) +
geom_bar( stat = "identity" ) +
facet_wrap( ~ Variable_tr, ncol=4 ) +
theme_classic()+
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())+
coord_polar(theta='y', start = pi / 3)+
xlab("Letter ph (IMF)")
您可以使用 facet_grid
:
ggplot( data, aes( x = "", y=value,fill=Type_tx ) ) +
geom_bar( stat='Identity') +
facet_grid( vars(Letter_ph),vars(Variable_tr) ) +
theme_classic()+
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())+
coord_polar(theta='y', start = pi / 3)+
xlab("Letter ph (IMF)")+ylab('Variable_tr')
除了将因变量更改为“值”并更正 facet_wrap()
语句外,如果您想要完整的馅饼(谁不想要?),那么它们的总和必须相同。
通过将数据旋转得更宽,将每个 T1:T4 除以行的总和,然后再次旋转长轴来绘制。
data %>%
pivot_wider(names_from = Type_tx, values_from = value) %>%
rowwise() %>%
mutate(across(T1:T4, ~ ./sum(T1, T2, T3, T4))*100) %>%
pivot_longer(-c(Letter_ph, Variable_tr), names_to = "Type_tx") %>%
ggplot(aes( x = "", y = value, fill=Type_tx ) ) +
geom_bar(stat = "identity" ) +
facet_wrap(Letter_ph ~ Variable_tr) +
theme_classic()+
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())+
coord_polar(theta='y', start = pi / 3)+
xlab("Letter ph (IMF)")
我想要一个包含三个主要列(变量 Letter_ph)和 4 行(Variable_tr)的图,其中包含由 [=24= 中的值填充的完整饼图].
比如这个
但是我有这个,请问有什么帮助吗?谢谢
data <- tibble::tibble(
value = c(18.3,47.2,53.5,8.6,61.4,114.24,43.7,0,82.7,9.017,18.49,35.79,3.76,25.2,56.5,21.3,0,42.36,18.14,0,47.69,2.4,3.1,10.0,1.18,4.1,9.1,3.1,5.0,21.2,1.5,6.6,18.6,6.4,0,12.5),
Letter_ph = c("I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F","I", "M", "F"),
Variable_tr = rep(c(rep("A",3),rep("B",3),rep("C",3),rep("A",3),rep("B",3),rep("C",3),rep("A",3),rep("B",3),rep("C",3),rep("A",3),rep("B",3),rep("C",3))),
Type_tx = rep(c(rep("T1",9),rep("T2",9),rep("T3",9), rep("T4", 9))))
ggplot( data, aes( x = "", y = Variable_tr, fill=Type_tx ) ) +
geom_bar( stat = "identity" ) +
facet_wrap( ~ Variable_tr, ncol=4 ) +
theme_classic()+
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())+
coord_polar(theta='y', start = pi / 3)+
xlab("Letter ph (IMF)")
您可以使用 facet_grid
:
ggplot( data, aes( x = "", y=value,fill=Type_tx ) ) +
geom_bar( stat='Identity') +
facet_grid( vars(Letter_ph),vars(Variable_tr) ) +
theme_classic()+
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())+
coord_polar(theta='y', start = pi / 3)+
xlab("Letter ph (IMF)")+ylab('Variable_tr')
除了将因变量更改为“值”并更正 facet_wrap()
语句外,如果您想要完整的馅饼(谁不想要?),那么它们的总和必须相同。
通过将数据旋转得更宽,将每个 T1:T4 除以行的总和,然后再次旋转长轴来绘制。
data %>%
pivot_wider(names_from = Type_tx, values_from = value) %>%
rowwise() %>%
mutate(across(T1:T4, ~ ./sum(T1, T2, T3, T4))*100) %>%
pivot_longer(-c(Letter_ph, Variable_tr), names_to = "Type_tx") %>%
ggplot(aes( x = "", y = value, fill=Type_tx ) ) +
geom_bar(stat = "identity" ) +
facet_wrap(Letter_ph ~ Variable_tr) +
theme_classic()+
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())+
coord_polar(theta='y', start = pi / 3)+
xlab("Letter ph (IMF)")