Geom_text 条形图上的标签问题
Geom_text issue with label on a bar chart
基于鸢尾花数据集,我正在尝试创建一个条形图,其中每个物种的平均萼片长度作为每个条形图的标签。
可重现的例子:
#load data
iris1 <- as.data.frame(iris)
#packages
library(ggplot2) #visualizations
#create bar chart with average Sepal Length for each Species as the label
ggplot(data = iris1, aes(x = iris1$Species, y = iris1$Sepal.Length)) +
geom_bar(stat = "summary", fun.y ="mean", fill = "light blue") +
labs(title = "Sepal Length by Species", x = "Species", y = "Sepal Length") +
geom_text(label = iris1$Sepal.Length)
输出:
ggplot(data = iris1, aes(x = iris1$Species, y = iris1$Sepal.Length)) +
geom_bar(stat = "summary", fun.y ="mean", fill = "light blue") +
labs(title = "Sepal Length by Species", x = "Species", y = "Sepal Length") +
geom_text(label = mean(iris1$Sepal.Length))
输出:
我希望该图表显示每个条形图(setosa、versicolor 和 virginica)的平均萼片长度的 1 个标签。
你可以试试这个:
ggplot(iris1, aes(x = Species, y = Sepal.Length)) +
stat_summary(fun.y=mean, geom="bar", fill = "light blue") +
stat_summary(aes(label=..y..), fun.y=mean, geom="text", size=8)
输出:
基于鸢尾花数据集,我正在尝试创建一个条形图,其中每个物种的平均萼片长度作为每个条形图的标签。
可重现的例子:
#load data
iris1 <- as.data.frame(iris)
#packages
library(ggplot2) #visualizations
#create bar chart with average Sepal Length for each Species as the label
ggplot(data = iris1, aes(x = iris1$Species, y = iris1$Sepal.Length)) +
geom_bar(stat = "summary", fun.y ="mean", fill = "light blue") +
labs(title = "Sepal Length by Species", x = "Species", y = "Sepal Length") +
geom_text(label = iris1$Sepal.Length)
输出:
ggplot(data = iris1, aes(x = iris1$Species, y = iris1$Sepal.Length)) +
geom_bar(stat = "summary", fun.y ="mean", fill = "light blue") +
labs(title = "Sepal Length by Species", x = "Species", y = "Sepal Length") +
geom_text(label = mean(iris1$Sepal.Length))
输出:
我希望该图表显示每个条形图(setosa、versicolor 和 virginica)的平均萼片长度的 1 个标签。
你可以试试这个:
ggplot(iris1, aes(x = Species, y = Sepal.Length)) +
stat_summary(fun.y=mean, geom="bar", fill = "light blue") +
stat_summary(aes(label=..y..), fun.y=mean, geom="text", size=8)
输出: