ggplot 2 中 y 轴上的折叠因子出现问题
Trouble with collapsing factor on y axis in ggplot 2
我有问题。我的 ggplot 一直将两个同名的向量折叠成一个,我想将它作为两个单独的条形图标记为 QVAR REDIHALER,当然还有其他标签。
data.frame(
brand_name = c("QVAR REDIHALER", "QVAR REDIHALER", "XARELTO", "XIFAXAN", "LATUDA", "TRINTELLIX", "VYVANSE", "NUCYNTA"),
n = c(78, 48, 71, 26, 8, 5, 8, 1))
brand_name1 %>%
ggplot(mapping = aes(x = reorder(brand_name, (-n)), y = n, fill = brand_name)) +
geom_bar(stat = "identity", position = "dodge", ) +
geom_text(aes(label = n), position = position_dodge(width = 0.9), hjust = -1) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(plot.title = element_text(face = "bold"),
axis.text.y = element_text(size = 14, colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank()) +
ylab("Count") +
xlab("") +
labs(fill = "Brand name", title = "Top Rebate Eligable - Brand") +
scale_fill_brewer(palette = "Set2") +
coord_flip()
问题出在箭头指向的地方。我想让“QVAR REDIHALER”在数据框中重复两次,而不是折叠成一根柱子
不能 100% 确定要如何将重复的类别拆分为两列。但我试一试。
一种选择是使用辅助列使重复的类别唯一,例如将数字 1、2、... 添加到每个并将此辅助列映射到 y
。这样做你会得到单独的列。要摆脱轴标签上显示的数字,您可以通过 scale_y_discrete
使用标签功能,即使用例如gsub
删除号码:
library(ggplot2)
library(dplyr)
brand_name1 <- brand_name1 %>%
group_by(brand_name) %>%
mutate(brand_name_uniq = paste0(brand_name, row_number()))
ggplot(brand_name1, mapping = aes(x = reorder(brand_name_uniq, (-n)), y = n, fill = brand_name)) +
geom_bar(stat = "identity", position = "dodge", ) +
geom_text(aes(label = n), position = position_dodge(width = 0.9), hjust = -1) +
scale_x_discrete(labels = ~gsub("\d$", "", .x)) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(plot.title = element_text(face = "bold"),
axis.text.y = element_text(size = 14, colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank()) +
ylab("Count") +
xlab("") +
labs(fill = "Brand name", title = "Top Rebate Eligable - Brand") +
scale_fill_brewer(palette = "Set2") +
coord_flip()
我有问题。我的 ggplot 一直将两个同名的向量折叠成一个,我想将它作为两个单独的条形图标记为 QVAR REDIHALER,当然还有其他标签。
data.frame(
brand_name = c("QVAR REDIHALER", "QVAR REDIHALER", "XARELTO", "XIFAXAN", "LATUDA", "TRINTELLIX", "VYVANSE", "NUCYNTA"),
n = c(78, 48, 71, 26, 8, 5, 8, 1))
brand_name1 %>%
ggplot(mapping = aes(x = reorder(brand_name, (-n)), y = n, fill = brand_name)) +
geom_bar(stat = "identity", position = "dodge", ) +
geom_text(aes(label = n), position = position_dodge(width = 0.9), hjust = -1) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(plot.title = element_text(face = "bold"),
axis.text.y = element_text(size = 14, colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank()) +
ylab("Count") +
xlab("") +
labs(fill = "Brand name", title = "Top Rebate Eligable - Brand") +
scale_fill_brewer(palette = "Set2") +
coord_flip()
问题出在箭头指向的地方。我想让“QVAR REDIHALER”在数据框中重复两次,而不是折叠成一根柱子
不能 100% 确定要如何将重复的类别拆分为两列。但我试一试。
一种选择是使用辅助列使重复的类别唯一,例如将数字 1、2、... 添加到每个并将此辅助列映射到 y
。这样做你会得到单独的列。要摆脱轴标签上显示的数字,您可以通过 scale_y_discrete
使用标签功能,即使用例如gsub
删除号码:
library(ggplot2)
library(dplyr)
brand_name1 <- brand_name1 %>%
group_by(brand_name) %>%
mutate(brand_name_uniq = paste0(brand_name, row_number()))
ggplot(brand_name1, mapping = aes(x = reorder(brand_name_uniq, (-n)), y = n, fill = brand_name)) +
geom_bar(stat = "identity", position = "dodge", ) +
geom_text(aes(label = n), position = position_dodge(width = 0.9), hjust = -1) +
scale_x_discrete(labels = ~gsub("\d$", "", .x)) +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme(plot.title = element_text(face = "bold"),
axis.text.y = element_text(size = 14, colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank()) +
ylab("Count") +
xlab("") +
labs(fill = "Brand name", title = "Top Rebate Eligable - Brand") +
scale_fill_brewer(palette = "Set2") +
coord_flip()