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

  • SEARCH
  • Home
  • 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 8019663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:27:23+00:00 2026-06-04T21:27:23+00:00

I would like to reproduce the plot immediately below by using ggplot2. I can

  • 0

I would like to reproduce the plot immediately below by using ggplot2. I can come close, but cannot remove the top and right borders. Below I present several attempts using ggplot2, including several suggestions found on or via Stackoverflow. Unfortunately I have not been able to get those suggestions to work.

I am hoping someone may be able to correct one or more of the code snippets below.

Thank you for any suggestions.

# desired plot
a <- seq(1,20)
b <- a^0.25
plot(a,b, bty = "l")


library(ggplot2)

df <- as.data.frame(cbind(a,b))

# 1. ggplot2 default
ggplot(df, aes(x = a, y = b)) + geom_point()

# 2. removes background color
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black'))

# 3. also removes gridlines
none <- theme_blank()
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black')) + opts(panel.grid.major = none, panel.grid.minor = none)

# 4. does not remove top and right border
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black')) + opts(panel.grid.major = none, panel.grid.minor = none) + opts(panel.border = none)

# 5. does not remove top and right border
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black')) + opts(panel.grid.major = none, panel.grid.minor = none) + opts(axis.line = theme_segment())

# 6. removes x and y axis in addition to top and right border
# http://stackoverflow.com/questions/5458409/remove-top-and-right-border-from-ggplot2
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black')) + opts(panel.grid.major = none, panel.grid.minor = none) + opts(panel.background=theme_rect(colour=NA))

# 7. returns error when attempting to remove top and right border
# https://groups.google.com/group/ggplot2/browse_thread/thread/f998d113638bf251
#
# Error in el(...) : could not find function "polylineGrob"
#
theme_L_border <- function(colour = "black", size = 1, linetype = 1) { 
   structure( 
     function(x = 0, y = 0, width = 1, height = 1, ...) { 
       polylineGrob( 
         x=c(x+width, x, x), y=c(y,y,y+height), ..., default.units = "npc", 
         gp=gpar(lwd=size, col=colour, lty=linetype), 
       ) 
     }, 
     class = "theme", 
     type = "box", 
     call = match.call() 
   )
}

ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black')) + opts(panel.grid.major = none, panel.grid.minor = none) + opts( panel.border = theme_L_border())
  • 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-04T21:27:26+00:00Added an answer on June 4, 2026 at 9:27 pm

    EDIT Ignore this answer. There are now better answers. See the comments. Use + theme_classic()

    EDIT

    This is a better version. The bug mentioned below in the original post remains (I think). But the axis line is drawn under the panel. Therefore, remove both the panel.border and panel.background to see the axis lines.

    library(ggplot2)
    a <- seq(1,20)
    b <- a^0.25
    df <- data.frame(a,b)
    
    ggplot(df, aes(x = a, y = b)) + geom_point() +
      theme_bw() +
      theme(axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank()) 
    

    enter image description here

    Original post
    This gets close. There was a bug with axis.line not working on the y-axis (see here), that appears not to be fixed yet. Therefore, after removing the panel border, the y-axis has to be drawn in separately using geom_vline.

    library(ggplot2)
    library(grid)
    
    a <- seq(1,20)
    b <- a^0.25
    df <- data.frame(a,b)
    
    p = ggplot(df, aes(x = a, y = b)) + geom_point() +
       scale_y_continuous(expand = c(0,0)) +
       scale_x_continuous(expand = c(0,0)) +
       theme_bw() +
       opts(axis.line = theme_segment(colour = "black"),
            panel.grid.major = theme_blank(),
            panel.grid.minor = theme_blank(),
            panel.border = theme_blank()) +
        geom_vline(xintercept = 0)
    p
    

    The extreme points are clipped, but the clipping can be undone using code by baptiste.

    gt <- ggplot_gtable(ggplot_build(p))
    gt$layout$clip[gt$layout$name=="panel"] <- "off"
    grid.draw(gt)
    

    enter image description here

    Or use limits to move the boundaries of the panel.

    ggplot(df, aes(x = a, y = b)) + geom_point() +
       xlim(0,22) +  ylim(.95, 2.1) +
       scale_x_continuous(expand = c(0,0), limits = c(0,22)) +
       scale_y_continuous(expand = c(0,0), limits = c(.95, 2.2)) +   
       theme_bw() +
       opts(axis.line = theme_segment(colour = "black"),
            panel.grid.major = theme_blank(),
            panel.grid.minor = theme_blank(),
            panel.border = theme_blank()) +
        geom_vline(xintercept = 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to reproduce UIScrollView's flick-to-scroll behavior, but I don't want to use
i would like to reproduce this http header call with ASIHTTPRequest. how i can
I would like to reproduce this effect : How can I do with xaml
I would like to reproduce the following in the simplest way (probably using a
I would like to ask you how can I insert JPanel into JOptionPane and
I juste watch this effect : alt text http://grab.by/5UBM I would like to reproduce
Matlab is displaying a black border around a plot and I would like to
I would like to know if anyone can help me out with a problem
I'm using JavaMail to send email requests to an SMTP server. I would like
I would like to plot a simple graphic. I have a dat set with

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.