rhombidodecahedron
rhombidodecahedron

Reputation: 7922

Making an R histogram plot from a saved hist() call

In R, one can do

x <- rnorm(100, 0, 1) # generate some fake data
hgram <- hist(x, plot=F)
plot(hgram$mids, hgram$counts)

One can further specify a plot type, such as 'h' or 's'. However, these don't really come out looking like a proper histogram. How can one make a nice looking histogram this way?

Upvotes: 0

Views: 1252

Answers (1)

dmb
dmb

Reputation: 567

Thought to add my inputs about making decent looking histograms in R (using your "x" from your question).

Using Base R

# histogram with colors and labels
hist(x, main = "Histogram of Fake Data", xlab = paste("x (units of measure)"), border = "blue", col = "green", prob = TRUE)
# add density
lines(density(x)) 
# add red line at 95th percentile
abline(v = quantile(x, .95), col = "red")

Using Plotly

install.packages("plotly")
library(plotly)
# basic Plotly histogram
plot_ly(x = x, type = "histogram")

The plotly result should open in a browser window with a variety of interactive controls. More plotly capabilities are available on their website at: https://plot.ly/r/histograms/#normalized-histogram

Upvotes: 1

Related Questions