S M Shamimul Hasan
S M Shamimul Hasan

Reputation: 6664

How to regenerate histogram in R?

PAC's answer to this post shows how to export histogram data from R to CSV.

> x  <- rnorm(1000)
> h  <- hist(x)
> h
$breaks
[1] -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5  0.0  0.5  1.0  1.5  2.0  2.5  3.0  3.5  4.0

$counts
[1]   1   5  23  38 104 154 208 191 130  85  39  17   4   0   1

$density
[1] 0.002 0.010 0.046 0.076 0.208 0.308 0.416 0.382 0.260 0.170 0.078 0.034 0.008 0.000 0.002

$mids
[1] -3.25 -2.75 -2.25 -1.75 -1.25 -0.75 -0.25  0.25  0.75  1.25  1.75  2.25  2.75  3.25  3.75

$xname
[1] "x"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"
> out  <- data.frame(mid = h$mids, counts = h$counts)
> write.table(out, file = "export.csv", row.names = FALSE, sep = ",")

Suppose I have stored a histogram's breaks, counts, density, mids values in a CSV file by using the above mentioned approach. Now, how to load the CSV file in RStudio and regenerate the histogram from the CSV file values. Please let me know. Thanks in advance.

Upvotes: 0

Views: 159

Answers (4)

Allan Cameron
Allan Cameron

Reputation: 174278

Well, if you really want to write a histogram as a csv, and to be able to completely reconstitute it from that csv, you can. I would probably use one of the other methods here myself, but it might be useful to see how it could be done:

We start by defining a function that saves a histogram to a csv by first of all making it into a dataframe. We have to pad out all the histogram's data members so they are the same length, but we'll reverse this easily when we reload the csv:

# Save a histogram object to csv
hist2csv <- function(h, csv_path)
{
  df <-  data.frame(breaks = h$breaks,   counts = c(h$counts, 1),
                    density = c(h$density, 1), mids = c(h$mids, 1),
                    xname = rep(h$xname, length(h$breaks)), 
                    equidist = rep(T, length(h$breaks)))

  write.csv(df, csv_path)
}

The loading function just reverses this process and ensures that the resultant list is classed as a histogram:

# Load a histogram object from csv
csv2hist <- function(csv_path)
{
  df         <- read.csv(csv_path)
  h          <- as.list(df)
  h$counts   <- h$counts[-length(h$breaks)]
  h$density  <- h$density[-length(h$breaks)]
  h$mids     <- h$mids[-length(h$breaks)]
  h$xname    <- h$xname[1]
  h$equidist <- h$equidist[1]
  class(h)   <- "histogram"

  return(h)
}

So now you can do

x  <- rnorm(1000)
h  <- hist(x)

# Save csv
hist2csv(h, "hist.csv")

# Load csv as histogram
new_h <- csv2hist("hist.csv") 

# Plot as normal
plot(new_h)

enter image description here

Upvotes: 5

mtotof
mtotof

Reputation: 69

If you need to regenerate the histogram to reuse it for a presentation, you could also simply print it as a png file.

png('histogram_plot.png', width=2000, height = 1000) 
#or whatever size works for the histogram
dev.off()

Upvotes: 0

r2evans
r2evans

Reputation: 160607

If you're looking to reconstitute it solely from the numbers, then perhaps:

h <- hist(mtcars$disp)

histogram

wid <- min(diff(h$mids))/2
plot(NA, type='n', xlim=range(h$mids) + wid*c(-1, 1), ylim = c(0, max(h$counts)), frame.plot = FALSE)
rect(h$mids - wid, 0, h$mids + wid, h$counts)

reconstituted histogram

(Labels notwithstanding ...)

Upvotes: 3

akrun
akrun

Reputation: 887501

If it is to regenerate the plot, it would be better to save it as .RData

save(h, file = "file1.RData")

On a new R session

load("file1.RData")
ls()
#[1] "h"
plot(h)

enter image description here

Upvotes: 1

Related Questions