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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:54:36+00:00 2026-06-15T06:54:36+00:00

once again Im confronted with a complicated ggplot. I want to plot different plottypes

  • 0

once again Im confronted with a complicated ggplot. I want to plot different plottypes within one plot using facet grid.

I hope I can make my point clear using the following example:
I want to produce a plot similar to the first picture but the upper plot should look like the second picture.
I already found the trick using the subset function but I can’t add vertical lines to only one plot let alone two or three (or specify the color).

CODE:

a <- rnorm(100)
b <- rnorm(100,8,1)
c <- rep(c(0,1),50)


dfr <- data.frame(a=a,b=b,c=c,d=seq(1:100))
dfr_melt <- melt(dfr,id.vars="d")

#I want only two grids, not three
ggplot(dfr_melt,aes(x=d,y=value)) + facet_grid(variable~.,scales="free")+
geom_line(subset=.(variable=="a")) + geom_line(subset=.(variable=="b"))

#Upper plot should look like this
ggplot(dfr,aes(x=d,y=a)) + geom_line() + geom_line(aes(y=c,color="c"))+
geom_hline(aes(yintercept=1),linetype="dashed")+
geom_hline(aes(yintercept=-2),linetype="dashed")

Example1

Example2

  • 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-15T06:54:39+00:00Added an answer on June 15, 2026 at 6:54 am

    If I understand your question correctly, you just need to a variable column to dfr in order to allow the faceting to work:

    dfr$variable = "a"
    ggplot(subset(dfr_melt, variable=="a"),aes(x=d,y=value)) +  
      facet_grid(variable~.,scales="free")+
      geom_line(data=subset(dfr_melt,variable=="a"))  + 
      geom_line(data=subset(dfr_melt, variable=="b")) + 
      geom_line(data=dfr, aes(y=c, colour=factor(c))) + 
      geom_hline(aes(yintercept=1),linetype="dashed")+
      geom_hline(aes(yintercept=-2),linetype="dashed")
    

    Notice that my plot doesn’t have the zig-zig line, this is because I changed:

      #This is almost certainly not what you want
      geom_line(data=dfr, aes(y=c, colour="c"))
    

    to

      #I made c a factor since it only takes the values 0 or 1
      geom_line(data=dfr, aes(y=c, colour=factor(c)))
      ##Alternatively, you could have
      geom_line(data=dfr, aes(y=c), colour="red") #or
      geom_line(data=dfr, aes(y=c, colour=c)) #or
    

    enter image description here

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

Sidebar

Related Questions

Once again, I'm at a loss with IE7 on this one. I'm making a
Once again I have hit the wall. How to replace escape characters using regular
My apologies once again for asking another very junior question. For one reason or
Once again I have written some Javascript using jQuery, and it works in every
Once again one of those: Is there an easier built-in way of doing things
once again I've got a question. Since I am using Google Web Toolkit (GWT)
Once again having problems being a new to javascript. I want to get the
Once again I'm receiving structs via UDP from a C++ programm, Now I ported
Once again, lists are boggling my mind. <div id=headbottom> <ul id=headnavbutton> <li id=button1 class=headnavbutton></li>
Once again my silliness has struck. I would like to thank everyone who helped

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.