R generate 2D histogram from raw data

后端 未结 6 1938
谎友^
谎友^ 2021-02-01 07:47

I have some raw data in 2D, x, y as given below. I want to generate a 2D histogram from the data. Typically, dividing the x,y values into bins of size 0.5, and count the number

6条回答
  •  梦如初夏
    2021-02-01 07:58

    For completeness, you can also use the hist2d{gplots} function. It seems to be the most straightforward for a 2D plot:

    library(gplots)
    
    # data is in variable df
    
    # define bin sizes
    bin_size <- 0.5
    xbins <- (max(df$x) - min(df$x))/bin_size
    ybins <- (max(df$y) - min(df$y))/bin_size
    
    # create plot
    hist2d(df, same.scale=TRUE, nbins=c(xbins, ybins))
    

    Two-dimensional histogram of x,y data

    # if you want to retrieve the data for other purposes
    df.hist2d <- hist2d(df, same.scale=TRUE, nbins=c(xbins, ybins), show=FALSE)
    df.hist2d$counts
    

提交回复
热议问题