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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:12:40+00:00 2026-05-17T17:12:40+00:00

I am trying to use R to create a raster image from a matrix

  • 0

I am trying to use R to create a raster image from a matrix of data. However, I am getting some weird artifacts on the edge of my image.

The code I am using is as follows:

# From the example for rasterImage(). A 3 pixel by 5 pixel b/w checkerboard.
testImage <- as.raster(0:1, nrow=3, ncol=5)

testImage
     [,1]      [,2]      [,3]      [,4]      [,5]     
[1,] "#000000" "#FFFFFF" "#000000" "#FFFFFF" "#000000"
[2,] "#FFFFFF" "#000000" "#FFFFFF" "#000000" "#FFFFFF"
[3,] "#000000" "#FFFFFF" "#000000" "#FFFFFF" "#000000"

png('test.png', width=5, height=3, units='px')

# Just want the image, no margins, boarders or other fancy stuff.
par(mar = c(0,0,0,0) )
plot.new()
plotArea = par('fig')

rasterImage(testImage, plotArea[1], plotArea[3],
  plotArea[2], plotArea[4], interpolate = FALSE )

dev.off()

This was executed in R 2.12.0 on OS X but I get the same output from R 2.11.0.


The output I am getting is the following (scaled from 5×3 to 150×90)

R output

The pixels in the corners should be black which suggests some form of interpolation is occurring.


The output I am expecting to see is:

Expected output

Any suggestions on why my code fails to faithfully produce a raster image from a matrix?


Intended Use

This is for a package I am working on so I would like to stay within the R base packages if possible so as not to introduce additional dependencies. The package implements a graphics device, so if anyone has a C level solution picks up from the info passed by GERaster() in src/main/engine.c and creates a PNG using only the R libraries, I would be willing to give that a shot as well.


Solutions for OS X

As pointed out by nico, the errant behavior is the result of antialiasing. The plot behaves as expected if png() is told to use an output method for which antialiasing can be disabled, such as Cairo graphics:

png('test.png', width=5, height=3, units='px', type='cairo', antialias=NULL)

On OS X the default backend for png() is Quartz, however png(..., type='quartz') currently ignores directives set by quartz.options(). Faithful output can be produced natively on OS X if the device is started by calling quartz() directly instead of using png():

quartz(file='test.png', type='png', width=5, height=3, dpi=1, antialias=FALSE)

Windows Thinks Differently

The following output is generated on Windows:

Windows output

According to an answer given by Paul Murrell (the benevolent dictator of R graphics
devices) on the R-help mailing list:

This is a rounding (truncation) problem. Working on a fix.

This behavior on Windows should not be noticeable unless the raster image contains a very small number of pixels.

  • 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-17T17:12:40+00:00Added an answer on May 17, 2026 at 5:12 pm

    Your code works as intended for me… (R 2.11.1 running under Fedora Core 13).
    It appears to be an antialias problem

    This code does the trick

    png('test.png', width=5, height=3, units='px', type='cairo', antialias=NULL)
    

    The default antialias options can be set in X11.options

    From ?X11.options

    antialias: for cairo types, the type of anti-aliasing (if any) to be
          used.  One of ‘c("default", "none", "gray", "subpixel")’.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this code I'm trying to use to export data from Excel to
Trying to use Jbuilder to create some JSON views for my app, but the
I am trying to use 'git log --pretty=tformat' to create xml file log. However
I'm trying to use get_or_create() for some fields in my forms, but I'm getting
I am trying to use inheritance to create a class derived from runtime_error ,
I'm trying to use MvcHtmlString.Create to create a JavaScript variable. However the output is
I am trying to use xlwt to create MS-Excel files from the contents of
Here's the code I'm trying to use to create a trigger: -- Dumping structure
I'm trying to use the 'edge.create' callback function of the Facebook javascript SDK to
I'm trying to use MPXJ to create project file from a hierarchy that exists

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.