如何在ggplot中手动编写轴标签

时间:2017-11-06 02:29:49

标签: r ggplot2

我希望在不同的时间间隔内显示0-1的比例,例如:

df <- data.frame(num=20*abs(rnorm(100)), y=rbinom(100,1,0.5))
df$num <- cut(df$num, breaks = c(0,10,20,30,Inf))
ggplot(data=aggregate(df$y, by=list(df$num), mean),
              aes(x=Group.1,y=x))+geom_col()+ylim(0,1)

但我想在x轴上用0,10,20改变标签......就在它们之间。 如何用不同数量的条形图重写轴标签,或者如何通过ggplot完成整个进度?

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以尝试将此绘图保存为对象,并确保x变量是分类

df$num <- factor(df$num, labels=c(0,10,20,"inf"))

p + scale_x_discrete(labels=c("val1" = "0", "val2" = "10",
                              "val3" = "20", "val4" = "inf"))