从使用 ggplot2 创建的多面条形图中删除重复的类别标签
Remove repeated category labels from faceted bar plot created with ggplot2
我正在尝试使用 ggplot2 在 R 中创建多面条形图。我设法创建了情节,但我不知道如何正确地注释它。请考虑以下 R 代码:
library(ggplot2)
library(reshape)
result <- c(0.05, 0.06, 0.08, 0.04, 0.05, 0.09, 1.05, 0.75, 1.4, 1.45)
group <- c("group.1", "group.1", "group.2", "group.1", "group.2", "group.1", "group.1", "group.2", "group.2", "group.2")
char_b <- c("b.1", "b.2", "b.2", "b.2", "b.1", "b.2", "b.2", "b.1", "b.1", "b.1")
char_c <- c("c.1", "c.1", "c.2", "c.2", "c.3", "c.3", "c.4", "c.4", "c.5", "c.5")
char_d <- c("d.1", "d.2", "d.1", "d.1", "d.2", "d.2", "d.1", "d.2", "d.2", "d.2")
approach <- c("method a", "method a", "method a", "method a", "method a", "method b", "method b" , "method b", "method b", "method b")
my_data <- data.frame(result, group, char_b, char_c, char_d, approach, stringsAsFactors=TRUE)
my_data <- melt(my_data, id=c("result","group","approach"))
df_plot <- ggplot(my_data, aes(x=variable, y=result, fill=value)) +
geom_bar(stat="identity") +
geom_text(aes(label = value),
position = position_stack(vjust = 0.5)) +
facet_wrap(approach ~ group, scales="free_x") +
theme(
legend.position="none",
axis.title.y = element_blank(),
strip.text.x = element_text(margin = margin(.05, 0, .05, 0, "cm"))
) +
coord_flip()
df_plot
以上代码产生以下结果:
如您所见,此图的问题在于存在重复标签(例如,d.1、c.1 和 b.2 的标签在方法 a 组的图中出现两次。 1).我想知道是否可以为类别的每个级别只显示一个标签。我认为出现这个问题是因为我必须重塑数据框以创建小平面;尽管如此,我还是没能解决。
非常感谢您的帮助。
祝福,
你可以去 tidyverse
。但诀窍是事先总结数据。
library(tidyverse)
data.frame(result, group, char_b, char_c, char_d, approach, stringsAsFactors=TRUE) %>%
pivot_longer(cols = -c("result","group","approach")) %>%
group_by(approach, group, name, value) %>%
summarise(result = sum(result)) %>%
ggplot(aes(name, result, fill = value)) +
geom_col(show.legend = F) +
geom_text(aes(label = value),
position = position_stack(vjust = 0.5)) +
coord_flip() +
facet_wrap(approach ~ group, scales="free_x")
基础R
你可以试试
aggregate(result ~ variable + approach + group + value, my_data, sum)
或者干脆
aggregate(result ~ ., my_data, sum)
我正在尝试使用 ggplot2 在 R 中创建多面条形图。我设法创建了情节,但我不知道如何正确地注释它。请考虑以下 R 代码:
library(ggplot2)
library(reshape)
result <- c(0.05, 0.06, 0.08, 0.04, 0.05, 0.09, 1.05, 0.75, 1.4, 1.45)
group <- c("group.1", "group.1", "group.2", "group.1", "group.2", "group.1", "group.1", "group.2", "group.2", "group.2")
char_b <- c("b.1", "b.2", "b.2", "b.2", "b.1", "b.2", "b.2", "b.1", "b.1", "b.1")
char_c <- c("c.1", "c.1", "c.2", "c.2", "c.3", "c.3", "c.4", "c.4", "c.5", "c.5")
char_d <- c("d.1", "d.2", "d.1", "d.1", "d.2", "d.2", "d.1", "d.2", "d.2", "d.2")
approach <- c("method a", "method a", "method a", "method a", "method a", "method b", "method b" , "method b", "method b", "method b")
my_data <- data.frame(result, group, char_b, char_c, char_d, approach, stringsAsFactors=TRUE)
my_data <- melt(my_data, id=c("result","group","approach"))
df_plot <- ggplot(my_data, aes(x=variable, y=result, fill=value)) +
geom_bar(stat="identity") +
geom_text(aes(label = value),
position = position_stack(vjust = 0.5)) +
facet_wrap(approach ~ group, scales="free_x") +
theme(
legend.position="none",
axis.title.y = element_blank(),
strip.text.x = element_text(margin = margin(.05, 0, .05, 0, "cm"))
) +
coord_flip()
df_plot
以上代码产生以下结果:
如您所见,此图的问题在于存在重复标签(例如,d.1、c.1 和 b.2 的标签在方法 a 组的图中出现两次。 1).我想知道是否可以为类别的每个级别只显示一个标签。我认为出现这个问题是因为我必须重塑数据框以创建小平面;尽管如此,我还是没能解决。
非常感谢您的帮助。
祝福,
你可以去 tidyverse
。但诀窍是事先总结数据。
library(tidyverse)
data.frame(result, group, char_b, char_c, char_d, approach, stringsAsFactors=TRUE) %>%
pivot_longer(cols = -c("result","group","approach")) %>%
group_by(approach, group, name, value) %>%
summarise(result = sum(result)) %>%
ggplot(aes(name, result, fill = value)) +
geom_col(show.legend = F) +
geom_text(aes(label = value),
position = position_stack(vjust = 0.5)) +
coord_flip() +
facet_wrap(approach ~ group, scales="free_x")
基础R
你可以试试
aggregate(result ~ variable + approach + group + value, my_data, sum)
或者干脆
aggregate(result ~ ., my_data, sum)