如何避免重叠注释?
How to avoid overlaying annotations?
我正在尝试在 geom_histogram()
ggplot 中绘制一些注释。见下图。这些注释是每个 bin、每个组的直方图计数。但是,当计数相似时,我不知道如何区分不同的注释。我只知道用 vjust
或 hjust
修复注释,但我想知道是否有相对的方法。我认为没有必要举例。对于更有经验的人来说,只看我的代码可能会很容易。
这是我用过的代码:
bind_rows(
RN_df %>% mutate(type='RN'),
RVNM_df %>% mutate(type='RVM')
) %>% group_by(hash) %>%
summarise(n_eps = n(), genre, type) %>%
ggplot(aes(x = n_eps, fill = genre)) +
geom_histogram(binwidth = 1) +
stat_count(aes(y=..count..,label=..count.., colour = genre),geom="text",vjust= -1, hjust = 0.5, size = 3) +
facet_wrap(~type)
这是我的输出图像:
您可以使用 geom_text_repel
来自 ggrepel
:
library(ggrepel)
ggplot(df, aes(x = n_eps, fill = genre)) +
geom_histogram(binwidth = 1) +
geom_text_repel(aes(label = ..count..), stat = 'count',
position = position_stack(vjust = 0.5), direction = 'y') +
facet_wrap(~type)
我正在尝试在 geom_histogram()
ggplot 中绘制一些注释。见下图。这些注释是每个 bin、每个组的直方图计数。但是,当计数相似时,我不知道如何区分不同的注释。我只知道用 vjust
或 hjust
修复注释,但我想知道是否有相对的方法。我认为没有必要举例。对于更有经验的人来说,只看我的代码可能会很容易。
这是我用过的代码:
bind_rows(
RN_df %>% mutate(type='RN'),
RVNM_df %>% mutate(type='RVM')
) %>% group_by(hash) %>%
summarise(n_eps = n(), genre, type) %>%
ggplot(aes(x = n_eps, fill = genre)) +
geom_histogram(binwidth = 1) +
stat_count(aes(y=..count..,label=..count.., colour = genre),geom="text",vjust= -1, hjust = 0.5, size = 3) +
facet_wrap(~type)
这是我的输出图像:
您可以使用 geom_text_repel
来自 ggrepel
:
library(ggrepel)
ggplot(df, aes(x = n_eps, fill = genre)) +
geom_histogram(binwidth = 1) +
geom_text_repel(aes(label = ..count..), stat = 'count',
position = position_stack(vjust = 0.5), direction = 'y') +
facet_wrap(~type)