2016-02-28 73 views
-4

我想每个小节下面的每个小节的名称(在我的例子中,名称是“Round”,它们碰巧是1,2,... 12)R - ggplot2中每个小节的名称

这里是我当前的代码:

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) 

下面是电流输出: enter image description here

回答

1

设置你的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))) 
1

只需添加一个离散规模为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() 

enter image description here

+0

您可能需要使用'limits'或'breaks'参数理清x轴 – SymbolixAU

+1

好点 - 周日早上,没有咖啡:) –