如何在 ggplot2 中的分组条形图列上定位标签
How to position labels on grouped bar plot columns in ggplot2
我在分组条形图上定位百分比和计数标签时遇到问题。
标签目前堆叠在一起:
我认为这是因为我一直在参考堆叠条形图的示例代码。我尝试将 position=position_dodge(width=1)
添加到 geom_text
以取消堆叠标签,但我收到以下警告:
Warning: Ignoring unknown aesthetics: position
Don't know how to automatically pick scale for object of type PositionDodge/Position/ggproto/gg. Defaulting to continuous.
Error: Aesthetics must be valid data columns. Problematic aesthetic(s): position = position_dodge(width = 1).
Did you mistype the name of a data column or forget to add stat()?
这是我使用泰坦尼克号数据集的代码:
data("titanic_train")
head(titanic_train, 6)
library(dplyr)
library(ggplot2)
titanic_train$Survived <- as.factor(titanic_train$Survived)
summary = titanic_train %>% group_by(Survived, Sex) %>% tally %>% mutate(pct = n/sum(n))
ggplot(summary, aes(x=Sex, y=n, fill=Survived)) + geom_bar(stat="identity", position="dodge") + geom_text(aes(label=paste0(sprintf("%1.1f", pct*100),"%\n", n)), colour="black")
我该如何解决这个问题?
您可以将 position = position_dodge(width = 1)
添加到您的 geom_text
调用中,但不在 aes
中。您的错误是由于试图将 position...
放入 aes
.
引起的
library(dplyr)
library(ggplot2)
library(titanic)
ggplot(summary, aes(x = Sex, y = n, fill = Survived)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(label = paste0(sprintf("%1.1f", pct * 100), "%\n", n)),
colour = "black",
position = position_dodge(width = 1)) +
coord_cartesian(ylim = c(0, 550))
我想分享一个示例,您可以使用您的数据复制相同的示例
数据
df <- data.frame(
x = factor(c(1, 1, 2, 2)),
y = c(1, 3, 2, 1),
grp = c("a", "b", "a", "b")
)
情节
ggplot(data = df, aes(x, y, group = grp)) +
geom_col(aes(fill = grp), position = "dodge") +
geom_text(
aes(label = y, y = y + 0.05),
position = position_dodge(0.9),
vjust = 0
)
我在分组条形图上定位百分比和计数标签时遇到问题。
标签目前堆叠在一起:
我认为这是因为我一直在参考堆叠条形图的示例代码。我尝试将 position=position_dodge(width=1)
添加到 geom_text
以取消堆叠标签,但我收到以下警告:
Warning: Ignoring unknown aesthetics: position Don't know how to automatically pick scale for object of type PositionDodge/Position/ggproto/gg. Defaulting to continuous. Error: Aesthetics must be valid data columns. Problematic aesthetic(s): position = position_dodge(width = 1). Did you mistype the name of a data column or forget to add stat()?
这是我使用泰坦尼克号数据集的代码:
data("titanic_train")
head(titanic_train, 6)
library(dplyr)
library(ggplot2)
titanic_train$Survived <- as.factor(titanic_train$Survived)
summary = titanic_train %>% group_by(Survived, Sex) %>% tally %>% mutate(pct = n/sum(n))
ggplot(summary, aes(x=Sex, y=n, fill=Survived)) + geom_bar(stat="identity", position="dodge") + geom_text(aes(label=paste0(sprintf("%1.1f", pct*100),"%\n", n)), colour="black")
我该如何解决这个问题?
您可以将 position = position_dodge(width = 1)
添加到您的 geom_text
调用中,但不在 aes
中。您的错误是由于试图将 position...
放入 aes
.
library(dplyr)
library(ggplot2)
library(titanic)
ggplot(summary, aes(x = Sex, y = n, fill = Survived)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(label = paste0(sprintf("%1.1f", pct * 100), "%\n", n)),
colour = "black",
position = position_dodge(width = 1)) +
coord_cartesian(ylim = c(0, 550))
我想分享一个示例,您可以使用您的数据复制相同的示例
数据
df <- data.frame(
x = factor(c(1, 1, 2, 2)),
y = c(1, 3, 2, 1),
grp = c("a", "b", "a", "b")
)
情节
ggplot(data = df, aes(x, y, group = grp)) +
geom_col(aes(fill = grp), position = "dodge") +
geom_text(
aes(label = y, y = y + 0.05),
position = position_dodge(0.9),
vjust = 0
)