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 6988741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:07:16+00:00 2026-05-27T19:07:16+00:00

I cannot figure out why the following function is not working. I will create

  • 0

I cannot figure out why the following function is not working. I will create data for the example if necessary. Please let me know. But it will take a lot of time and hence, I wanted to ask first without the data, in case, the problem is obvious that you can see without the data.

Please see the code below. If I use the function

plotReturns(8, "1930-01-01", "1940-12-31", "savehere.pdf")

the file savehere.pdf is created, but I cannot open it. I get an error saying

There was an error opening this document. This file cannot be opened because it has no pages.

But, if instead of using the function, I manually go through each step in that function code (replacing the variable names by the values used above as arguments) then file savehere.pdf is created fine and I can open it.

So, it appears there is nothing wrong with any of my specific commands. But then why does the function not work when called as a function?

Thanks for your help.

plotReturns = function(decileValue, startDate, endDate, fileName) {
  # Keep data from specific decile
  specificdecile <- merged.data[merged.data$decile_correct == decileValue,]

  #filter the data to get rows within the specified dates
  specificdecileAndYears <- specificdecile[
                            ((specificdecile$rdate >= as.Date(startDate)) &
                              (specificdecile$rdate <= as.Date(endDate))),]
  #keep the necessary columns:rdate, decile_correct, vwret_bottomup, vwret_CRSP
  specificdecileAndYears <- specificdecileAndYears[c("rdate", "decile_correct", 
                                                     "vwret_bottomup", "vwret_CRSP")]
  # Melt the data for plotting
  melted.data <- melt(specificdecileAndYears, id=c("rdate","decile_correct"))

  # Use melted data for plotting

  # Set the plot title
  title <- paste("Plot for decile", decileValue)

  # Specifing colors to be used for line plots
  myColors <- c("steelblue", rgb(1,0.5,0.3,0.5))
  fileName <- fileName

  pdf(fileName, width=8, height=5)

  # scale_color_manual is to use custom colors specified in myColors above.
  # The first argument of scale_color_manual specifies the title of the legend,
  # which is set to empty here.
  ggplot(melted.data, aes(x=rdate, y=value, color=variable)) + geom_line() +
      opts(legend.position=c(0.85,0.2), legend.background=theme_rect(col=0), title=title) +
      scale_color_manual("", values=myColors) +
      ylab("Return") + xlab("")

  # turn device off
  dev.off() 
}
  • 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-05-27T19:07:17+00:00Added an answer on May 27, 2026 at 7:07 pm

    It’s in the FAQ.

    http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f

    You need to print lattice and ggplot objects when you are inside functions. They are both types of “grid graphics” which typically get built up in sections of commands rather than always working as side-effects on graphics devices. That model is especially pertinent for ggplot where it is typical to add features using the “+” plotting operator. However, Lattice graphics function also return lists so they, too, can be appended-to with functions like trellis.focus and update.trellis

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

Sidebar

Related Questions

I can not figure out why the following AJAX code will only load the
I cannot figure out how to write the following query using the DbFinderPlugin 1.2.2
I cannot seem to figure out I am getting the following error in IE
I cannot figure out why g++ is giving the following errors. I believe that
I cannot figure out how to change the title bar icon (the icon in
I cannot figure out a way to disable a container AND its children in
I cannot figure out how to enable per-session instances for my WCF service while
I cannot figure out how to make a C# Windows Form application write to
I cannot figure out why I get this error during check-in. I checked in
I cannot figure out how to convert this code from C# to VB.net. It

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.