JBT
JBT

Reputation: 8746

how to make qplot produce a line plot with one vector of data

qplot uses histogram by default when you only provide one vector of data. For example, qplot(1:100) will produce a histogram. However, adding geom="line" won't work. So, my question is, how can I use qplot to generate the same kind of plot as the command plot(1:100), which is a line plot? Thanks in advance.

I know you can manually add a dummy index as the x, but that seems cluttered. Is there a cleaner way?

Upvotes: 2

Views: 266

Answers (1)

mnel
mnel

Reputation: 115392

When I run plot(1:100), I get a points plot with x = seq_along(y), y = 1:100

to get the same plot using qplot, all you need to run is

plot(1:100)

qplot(y = 1:100)

enter image description here

Within qplot there is

if (missing(x)) {
                aesthetics$x <- bquote(seq_along(.(y)), aesthetics)
            }
            geom[geom == "auto"] <- "point

which deals with this.

qplot(1:100) will be parsed as qplot(x = 1:100) (using positional matching), and is dealt with by

  if (missing(y)) {
        geom[geom == "auto"] <- "histogram"
        if (is.null(ylab)) 
            ylab <- "count"
    }

Upvotes: 4

Related Questions