Search code examples
rplotaxis-labels

R plotting factor with values on y-axis


f <- as.factor(sample( rep(c("a", "b", "c"), 3)))

plot(1:9,f)

gives the values 1.0..3.0 on the y-axis.

How do I get the values of f ("a", "b" and "c") on the y-axis?


Solution

  • f <- as.factor(sample( rep(c("a", "b", "c"), 3)))
    

    R base

    plot(1:9, f, yaxt = "n")
    axis(2, 1:3, levels(f))
    

    lattice

    see fdetsch answer
    

    ggplot2

    library(ggplot2)
    qplot(seq_along(f), f)