geom_text 在饼图中的某个位置
geom_text at certain position in a pie chart
我想创建一个饼图,在特定位置显示一些标签,表示比例检验是否显着。我设法创建了一个基本的饼图,并粘贴了下面的代码。我还发布了所需的结果(使用 MS Paint 软件创建),并希望在创建此图时能得到任何帮助。
library(tidyverse)
# defining the dataframe
df <-
data.frame(
condition = c('x', 'y', 'z'),
cat = rep(c('a', 'b'), 3),
freq = c(60, 34, 44, 40, 66, 56)
)
# computing percentages
df <-
df %>% group_by(condition) %>% mutate(label = freq / sum(freq) * 100)
# creating a pie chart
ggplot2::ggplot(data = df, mapping = aes('', freq, fill = cat)) +
facet_grid(". ~ condition") +
geom_col(position = 'fill') +
geom_label(aes(label = label), position = position_fill(vjust = 0.5)) +
coord_polar(theta = 'y') +
ggplot2::scale_y_continuous(breaks = NULL) +
ggplot2::theme_grey() +
ggplot2::theme(
panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
strip.text.x = element_text(size = 14, face = "bold"),
strip.text.y = element_text(size = 14, face = "bold"),
strip.text = element_text(size = 14, face = "bold"),
legend.text = element_text(size = 14),
legend.title = element_text(size = 14, face = "bold"),
legend.title.align = 0.5,
legend.text.align = 0.5,
legend.direction = "horizontal",
legend.position = "bottom",
legend.key = element_rect(size = 5),
legend.key.size = unit(1.5, "lines"),
legend.margin = margin(5, 5, 5, 5),
legend.box.margin = margin(5, 5, 5, 5),
panel.border = element_rect(
colour = "black",
fill = NA,
size = 1
),
plot.subtitle = element_text(
color = "black",
size = 14,
hjust = 0.5
),
plot.title = element_text(
color = "black",
size = 16,
face = "bold",
hjust = 0.5
)
) +
ggplot2::guides(fill = guide_legend(override.aes = base::list(colour = NA)))
由 reprex package (v0.2.0) 创建于 2018-03-21。
这是所需结果的样子(请注意,文本始终位于顶部并与饼图的中心对齐)-
这个怎么样?我使用 geom_text 并指定 x = 1.6
来获取饼图半径之外的标签。另外,这只是一个样式提示,但是在 theme() 中有很多不必要的参数,并且在加载 tidyverse 后不需要在 ggplot 函数之前放置 ggplot2::
。此外,如果您不希望饼图的中心有一个小的空点,您可以在调用 geom_col
.
时添加 width = 1
library(tidyverse)
df <-
data.frame(
condition = c('x', 'y', 'z'),
cat = rep(c('a', 'b'), 3),
freq = c(60, 34, 44, 40, 66, 56),
sig =c("***", NA, "ns", NA, "**", NA)
) %>%
group_by(condition) %>%
mutate(label = freq / sum(freq) * 100)
ggplot(data = df, mapping = aes('', freq, fill = cat)) +
facet_wrap(~ condition, nrow = 1) +
geom_col(position = 'fill', width = 1) +
geom_label(aes(label = label), position = position_fill(vjust = 0.5)) +
geom_text(aes(label = sig, x = 1.6), position = position_fill(vjust = 1)) +
coord_polar(theta = 'y') +
theme_grey() +
theme(panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
strip.text = element_text(size = 14, face = "bold"),
strip.background = element_rect(color = "black", size = 1),
legend.text = element_text(size = 14),
legend.title = element_text(size = 14, face = "bold"),
legend.position = "bottom",
legend.key.size = unit(1.5, "lines"),
panel.border = element_rect(colour = "black", fill = NA, size = 1)) +
guides(fill = guide_legend(override.aes = list(colour = NA)))
Here's the result
我想创建一个饼图,在特定位置显示一些标签,表示比例检验是否显着。我设法创建了一个基本的饼图,并粘贴了下面的代码。我还发布了所需的结果(使用 MS Paint 软件创建),并希望在创建此图时能得到任何帮助。
library(tidyverse)
# defining the dataframe
df <-
data.frame(
condition = c('x', 'y', 'z'),
cat = rep(c('a', 'b'), 3),
freq = c(60, 34, 44, 40, 66, 56)
)
# computing percentages
df <-
df %>% group_by(condition) %>% mutate(label = freq / sum(freq) * 100)
# creating a pie chart
ggplot2::ggplot(data = df, mapping = aes('', freq, fill = cat)) +
facet_grid(". ~ condition") +
geom_col(position = 'fill') +
geom_label(aes(label = label), position = position_fill(vjust = 0.5)) +
coord_polar(theta = 'y') +
ggplot2::scale_y_continuous(breaks = NULL) +
ggplot2::theme_grey() +
ggplot2::theme(
panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
strip.text.x = element_text(size = 14, face = "bold"),
strip.text.y = element_text(size = 14, face = "bold"),
strip.text = element_text(size = 14, face = "bold"),
legend.text = element_text(size = 14),
legend.title = element_text(size = 14, face = "bold"),
legend.title.align = 0.5,
legend.text.align = 0.5,
legend.direction = "horizontal",
legend.position = "bottom",
legend.key = element_rect(size = 5),
legend.key.size = unit(1.5, "lines"),
legend.margin = margin(5, 5, 5, 5),
legend.box.margin = margin(5, 5, 5, 5),
panel.border = element_rect(
colour = "black",
fill = NA,
size = 1
),
plot.subtitle = element_text(
color = "black",
size = 14,
hjust = 0.5
),
plot.title = element_text(
color = "black",
size = 16,
face = "bold",
hjust = 0.5
)
) +
ggplot2::guides(fill = guide_legend(override.aes = base::list(colour = NA)))
由 reprex package (v0.2.0) 创建于 2018-03-21。
这是所需结果的样子(请注意,文本始终位于顶部并与饼图的中心对齐)-
这个怎么样?我使用 geom_text 并指定 x = 1.6
来获取饼图半径之外的标签。另外,这只是一个样式提示,但是在 theme() 中有很多不必要的参数,并且在加载 tidyverse 后不需要在 ggplot 函数之前放置 ggplot2::
。此外,如果您不希望饼图的中心有一个小的空点,您可以在调用 geom_col
.
width = 1
library(tidyverse)
df <-
data.frame(
condition = c('x', 'y', 'z'),
cat = rep(c('a', 'b'), 3),
freq = c(60, 34, 44, 40, 66, 56),
sig =c("***", NA, "ns", NA, "**", NA)
) %>%
group_by(condition) %>%
mutate(label = freq / sum(freq) * 100)
ggplot(data = df, mapping = aes('', freq, fill = cat)) +
facet_wrap(~ condition, nrow = 1) +
geom_col(position = 'fill', width = 1) +
geom_label(aes(label = label), position = position_fill(vjust = 0.5)) +
geom_text(aes(label = sig, x = 1.6), position = position_fill(vjust = 1)) +
coord_polar(theta = 'y') +
theme_grey() +
theme(panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
strip.text = element_text(size = 14, face = "bold"),
strip.background = element_rect(color = "black", size = 1),
legend.text = element_text(size = 14),
legend.title = element_text(size = 14, face = "bold"),
legend.position = "bottom",
legend.key.size = unit(1.5, "lines"),
panel.border = element_rect(colour = "black", fill = NA, size = 1)) +
guides(fill = guide_legend(override.aes = list(colour = NA)))
Here's the result