我想创建一个饼图,在某个位置显示一些标签,表示比例测试是否重要。我已设法创建一个基本的饼图,我已粘贴下面的代码。我还发布了所需的结果(使用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。
答案 0 :(得分:2)
这个怎么样?我使用geom_text并指定x = 1.6
来获取饼图半径之外的标签。另外,只是一个样式提示,但是你在theme()中有很多不必要的参数,并且你不需要在加载tidyverse之后将ggplot2::
放在ggplot函数之前。另外,如果您希望饼图中间没有小空点,则可以width = 1
将geom_col
放入 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)))
。
Example of a match:
column1 = 'theredroof'
column2 = 'theredroof'
Example of non match:
column1 = 'theredroof'
column2 = 'thegreenroof'
Example of exclusion:
(I don't want it to consider this in the where statement at all)
column1 = 'theredroof'
column2 = 0
OR
column1 = 0
column2 = 0