R - ggplot2 中每个条形的名称
R - Name of each bar in ggplot2
我想在每个条形图下显示每个条形图的名称(在我的例子中,名称是 "Round",它们恰好是 1、2、... 12)
这是我当前的代码:
ggplot(data=draft1, aes(x = Round, y = mean.age)) +
geom_bar(stat = "identity", fill = "steelblue", color = "black", width = 0.7) +
ylab("Average age of retirement") + ylim(c(0,40)) +
ggtitle("Average age of retirement by rounds of all players") +
geom_text(aes(label = mean.age), position=position_dodge(width=0.9), vjust = -0.5)
这是当前输出:
将你的Round
设置为一个因素
ggplot(data=draft1, aes(x = factor(Round), y = mean.age)) +
或使用scale_x_continuous()
ggplot(data=draft1, aes(x = Round, y = mean.age)) +
... +
scale_x_continuous(breaks=(seq(1:12)))
只需为 x 添加一个离散比例:
library(ggplot2)
draft1 = data.frame(Round = seq(1, 12), mean.age = sample(29:32))
ggplot(data=draft1, aes(x = Round, y = mean.age)) +
geom_bar(stat = "identity", fill = "steelblue", color = "black", width = 0.7) +
ylab("Average age of retirement") + ylim(c(0,40)) +
ggtitle("Average age of retirement by rounds of all players") +
geom_text(aes(label = mean.age), position=position_dodge(width=0.9), vjust = -0.5) +
scale_x_discrete()
我想在每个条形图下显示每个条形图的名称(在我的例子中,名称是 "Round",它们恰好是 1、2、... 12)
这是我当前的代码:
ggplot(data=draft1, aes(x = Round, y = mean.age)) +
geom_bar(stat = "identity", fill = "steelblue", color = "black", width = 0.7) +
ylab("Average age of retirement") + ylim(c(0,40)) +
ggtitle("Average age of retirement by rounds of all players") +
geom_text(aes(label = mean.age), position=position_dodge(width=0.9), vjust = -0.5)
这是当前输出:
将你的Round
设置为一个因素
ggplot(data=draft1, aes(x = factor(Round), y = mean.age)) +
或使用scale_x_continuous()
ggplot(data=draft1, aes(x = Round, y = mean.age)) +
... +
scale_x_continuous(breaks=(seq(1:12)))
只需为 x 添加一个离散比例:
library(ggplot2)
draft1 = data.frame(Round = seq(1, 12), mean.age = sample(29:32))
ggplot(data=draft1, aes(x = Round, y = mean.age)) +
geom_bar(stat = "identity", fill = "steelblue", color = "black", width = 0.7) +
ylab("Average age of retirement") + ylim(c(0,40)) +
ggtitle("Average age of retirement by rounds of all players") +
geom_text(aes(label = mean.age), position=position_dodge(width=0.9), vjust = -0.5) +
scale_x_discrete()