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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:54:05+00:00 2026-06-18T04:54:05+00:00

I have a raster file and I want to plot as a histogram ,i

  • 0

I have a raster file
and I want to plot as a histogram ,i did it using hist() as shown below. but i would like to plot using ggplot2 which plot it in a better way for publication.

conne <- file("C:\\fined.bin","rb")
r = raster(y)
hist(r, breaks=30, main="SMD_2010",
        xlab="Pearson correlation", ylab="Frequency", xlim=c(-1,1))

I tried this:

  qplot(rating, data=r, geom="histogram")

Error:

            ggplot2 doesn't know how to deal with data of class RasterLayer

I need to plot something like:

http://docs.ggplot2.org/0.9.3/geom_histogram-28.png

  • 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-18T04:54:06+00:00Added an answer on June 18, 2026 at 4:54 am

    As a fast solution You can use the result of hist

    f <- hist(r, breaks=30)
    dat <- data.frame(counts= f$counts,breaks = f$mids)
    ggplot(dat, aes(x = breaks, y = counts)) + 
      geom_bar(stat = "identity",fill='blue',alpha = 0.8)+
      xlab("Pearson correlation")+ ylab("Frequency")+
      scale_x_continuous(breaks = seq(-1,1,0.25),  ## without this you will get the same scale
                       labels = seq(-1,1,0.25))    ## as hist (question picture)
    

    PS : Maybe you need to use scale_x_discrete to get better axis look

    EDIT to add gradient fill

    ggplot(dat, aes(x = breaks, y = counts, fill =counts)) + ## Note the new aes fill here
      geom_bar(stat = "identity",alpha = 0.8)+
      xlab("Pearson correlation")+ ylab("Frequency")+
      scale_x_continuous(breaks = seq(-1,1,0.25),
                       labels = seq(-1,1,0.25))+
      scale_fill_gradient(low="blue", high="red")            ## to play with colors limits
    

    enter image description here

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

Sidebar

Related Questions

Lets say I have some raster data I want to write to a file..
I have a game with a big raster map Now we are using jpeg
I have a 2D array from which I want to produce a contour plot
I have a raster file (basically 2D array) with close to a million points.
I have a output array as below. I want to loop through the array
I have a 2D raster of say 200pixels x 200pixels I want to subdivide
So if I have a class like: CustomVal I want to be able to
So I have my raster file r <- raster('ras') and a shapefile abys <-
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
I have a set of PDFs in normal RGB colour. They would benefit from

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.