Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7844413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:52:01+00:00 2026-06-02T16:52:01+00:00

I have been unable to find a simple analog for plotting a line graph

  • 0

I have been unable to find a simple analog for plotting a line graph from a table object in ggplot2. Given the elegance and utility of the package, I feel I must be missing something quite obvious. As an illustration consider a data frame with yearly observations:

dat<-data.frame(year=sample(c("2001":"2010"),1000, replace=T))

And a quick time series plot in base R:

plot(table(dat$year), type="l")

Switching to qplot, returns the error “attempt to apply a non-function”:

qplot(table(dat$year), geom="line")

ggplot2 requires a data frame. Fair enough. But this returns the same error.

qplot(year, data=dat, geom="line")

After some searching and fiddling, I abandoned qplot, and came up with the following approach which involves specifying a line geometry, binning the counts, and dropping final values to avoid plotting zeros.

ggplot(dat, aes(year) ) + geom_line(stat = "bin", binwidth=1, drop=TRUE)

It seems like rather a long walk around the block. And it is still not entirely satisfactory, since the bins don’t align precisely with the mid-year values on the x-axis. Where have I gone wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:52:02+00:00Added an answer on June 2, 2026 at 4:52 pm

    Maybe still more complicated than you want, but:

    qplot(Var1,Freq,data=as.data.frame(table(dat$year)),geom="line",group=1)
    

    (the group=1 is necessary because the Year variable (Var1) is returned as a factor …)

    If you didn’t need it as a one-liner you could use ytab <- as.data.frame(table(dat$year)) first to extract the table and convert it to a data frame …

    Following Brian Diggs’s answer, if you’re willing to construct a bit more fortify machinery you can condense this a bit more:

    A utility function that converts a factor to numeric if possible:

    conv2num <- function(x) {
        xn <- suppressWarnings(as.numeric(as.character(x)))
        if (!all(is.na(xn))) xn else x
    }
    

    And a fortify method that turns the table into a data frame and then tries to make the columns numeric:

    fortify.table <- function(x,...) {
        z <- as.data.frame(x)
        facs <- sapply(z,is.factor)
        z[facs] <- lapply(z[facs],conv2num)
        z
    }
    

    Now this works almost as you would like it to:

    qplot(Var1,Freq,data=table(dat$year),geom="line")
    

    (It would be nice/easier if there were a table option to preserve the numeric nature of cross-classifying factors …)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a simple question, but i have been unable to find a
this seems like such a simple problem but I have been unable to find
This is a pretty simple question, but I have been unable to find anyway
I have been unable to find any documentation on the .build method in Rails
I have been unable to find any decent documentation on this function. The code
I have been unable to find any documentation on properly closing database connections in
After an extensive search I have been unable to find any information on this
I have been searching but unable to find a proper list. Can someone please
I think it might have been asked before but i was unable to find
I have need to return multiple results from a subquery and have been unable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.