How to get the actual data from the function hist - r

I am very new to R, so I apologize if this is a basic question.
Is there any way to have the data behind the graph the function "hist" produces?
I don't need the graphic, I just the data.
In general, it would be nice if I have the option to only get the data behind the functions that produce graphs and prevent drawing the actual plots.
Thank you,

There is no way to obtain the original data behind the function hist.
If you are referring just to the data required to generate the plot, they are stored in hist(x)$mids and hist(x)$count, which contains respectively the midpoints and the counts. If you want just the data without drawing the plot, you can call this function on the object hist:
dataHist<-function(y){
rbind(y$mids,y$counts)
}

Try using hist(*yourvectorname*, plot = FALSE)

Related

Converting a NULL Picture Value to an Object in R [duplicate]

In ggplot2, one can easily save a graphic into a R object.
p = ggplot(...) + geom_point() # does not display the graph
p # displays the graph
The standard function plot produces the graphic as a void function and returns NULL.
p = plot(1:10) # displays the graph
p # NULL
Is it possible to save a graphic created by plot in an object?
base graphics draw directly on a device.
You could use
1- recordPlot
2- the recently introduced gridGraphics package, to convert base graphics to their grid equivalent
Here's a minimal example,
plot(1:10)
p <- recordPlot()
plot.new() ## clean up device
p # redraw
## grab the scene as a grid object
library(gridGraphics)
library(grid)
grid.echo()
a <- grid.grab()
## draw it, changes optional
grid.newpage()
a <- editGrob(a, vp=viewport(width=unit(2,"in")), gp=gpar(fontsize=10))
grid.draw(a)
I am very late to this, but it was the first question which showed up when I searched for the question. So I'd like to add my solution for future viewers who come across the question.
I solved this by using a function instead of an object. For example, suppose we want to compare two beta distributions with different parameters. We can run:
z1<-rbeta(10000,5,5)
z2<-rbeta(10000,20,20)
plotit<-function(vector,alpha,beta){
plot(density(vector),xlim=c(0,1))
abline(v=alpha/(alpha+beta),lty="longdash")
}
And save the plots as functions rather than objects.
z.plot1<-function(){plotit(z1,5,5)}
z.plot2<-function(){plotit(z2,20,20)}
Next, we can call each plot as we want by simply calling the two plots as functions rather than objects.
z.plot1()
plots the first plot and
z.plot2()
plots the second.
Hope that helps someone who stumbles across this later!
You can use the active binding feature of the pryr package if you don't want to directly change the values of the object created.
library(pryr)
a %<a-% plot(1:10,1:10)
Each time you type a on the console the graph will be reprinted on the screen. The %<a-% operator will rerun the script every time (in case of one graph this is not a problem I think). So essentially every time you use a the code will be rerun resulting in your graph which of course you can manipulate (overlay another plot on top) or save using png for example. No value itself will be stored in a however. The value will still be NULL.
I don't know if the above is what you are looking for but it might be an acceptable solution.
library(ggplot2)
# if mygraph is a plot object
ggsave("myplot1.png",mygraph)
# if the plot is in a list (e.g. created by the Bibliometrics package)
ggsave("myplot1.png",mygraphs[[1]])

R Plot of Two Detrended Series Shows Line Chart Rather Than Scatterplot

I have a set of data which are within the exact same time frame, with the exact same number of points. I have detrended both so comovement can be analyzed. When I plot them against each other the graph attempts to create a line chart including dates.
plot
This is what the series look like in the environment:
environment variables
This is what the data looks like:
data screenshot
I would like this in a scatterplot measuring against both variables, just points and no lines or dates in the plot.
So I sorta figured it out but it's super botched and I do not recommend anyone else to do this.
Essentially, I bound the two datasets together doing:
testvar <- cbind(dewagerealM, dewagerealF)
I was then able to select all the data on the left and the right, then plot them against each other like so:
plot(testvar[1:23,1], testvar[1:23,2])
This seems to have worked but it's not pretty and definitely not what should be done but it seems to have gotten the job done.
The easiest way to do this is to use the options xy.lines and xy.labels set to FALSE
plot(dewagerealM, dewagerealF, type = 'p',xy.lines = FALSE, xy.labels = FALSE)
Since you are plotting time series (ts) type objects, the help function help("plot.ts") can give you more details on the options you can use to plot these objects.

How to store two plot in the same variable? [duplicate]

In ggplot2, one can easily save a graphic into a R object.
p = ggplot(...) + geom_point() # does not display the graph
p # displays the graph
The standard function plot produces the graphic as a void function and returns NULL.
p = plot(1:10) # displays the graph
p # NULL
Is it possible to save a graphic created by plot in an object?
base graphics draw directly on a device.
You could use
1- recordPlot
2- the recently introduced gridGraphics package, to convert base graphics to their grid equivalent
Here's a minimal example,
plot(1:10)
p <- recordPlot()
plot.new() ## clean up device
p # redraw
## grab the scene as a grid object
library(gridGraphics)
library(grid)
grid.echo()
a <- grid.grab()
## draw it, changes optional
grid.newpage()
a <- editGrob(a, vp=viewport(width=unit(2,"in")), gp=gpar(fontsize=10))
grid.draw(a)
I am very late to this, but it was the first question which showed up when I searched for the question. So I'd like to add my solution for future viewers who come across the question.
I solved this by using a function instead of an object. For example, suppose we want to compare two beta distributions with different parameters. We can run:
z1<-rbeta(10000,5,5)
z2<-rbeta(10000,20,20)
plotit<-function(vector,alpha,beta){
plot(density(vector),xlim=c(0,1))
abline(v=alpha/(alpha+beta),lty="longdash")
}
And save the plots as functions rather than objects.
z.plot1<-function(){plotit(z1,5,5)}
z.plot2<-function(){plotit(z2,20,20)}
Next, we can call each plot as we want by simply calling the two plots as functions rather than objects.
z.plot1()
plots the first plot and
z.plot2()
plots the second.
Hope that helps someone who stumbles across this later!
You can use the active binding feature of the pryr package if you don't want to directly change the values of the object created.
library(pryr)
a %<a-% plot(1:10,1:10)
Each time you type a on the console the graph will be reprinted on the screen. The %<a-% operator will rerun the script every time (in case of one graph this is not a problem I think). So essentially every time you use a the code will be rerun resulting in your graph which of course you can manipulate (overlay another plot on top) or save using png for example. No value itself will be stored in a however. The value will still be NULL.
I don't know if the above is what you are looking for but it might be an acceptable solution.
library(ggplot2)
# if mygraph is a plot object
ggsave("myplot1.png",mygraph)
# if the plot is in a list (e.g. created by the Bibliometrics package)
ggsave("myplot1.png",mygraphs[[1]])

changing default colours when using the plot function of the R package mixtools

I have a plotting problem with curves when using mixtools
Using the following R code
require(mixtools)
x <- c(rnorm(10000,8,2),rnorm(10000,18,5))
xMix <- normalmixEM(x, lambda=NULL, mu=NULL, sigma=NULL)
plot(xMix, which = 2, nclass=25)
I get a nice histogram, with the 2 normal curves estimated from the model superimposed.
The problem is with the default colours (i.e. red and green), which I need to change for a publication to be black and grey.
One way I thought to doing this was first to produce the histogram
hist(xMix$x, freq=FALSE, nclass=25)
and then add the lines using the "curve" function.
....... but I lost my way, and couldn't solve it
I would be grateful for any pointers or the actual solution
thanks
PS. Note that there is an alternative work-around to this problem using ggplot:
Any suggestions for how I can plot mixEM type data using ggplot2
but for various reasons I need to keep using the base graphics
You can also edit the colours directly using the col2 argument in the mixtools plotting function
For example
plot(xMix, which = 2, nclass=25, col2=c("dimgrey","black"))
giving the problem a bit more thought, I managed to rephrase the problem and ask the question in a much more direct way
Using user-defined functions within "curve" function in R graphics
this delivered two nice solutions of how to use the "curve" function to draw normal distributions produced by the mixture modelling.
the overall answer therefore is to use the "hist" function to draw a histogram of the raw data, then the "curve" function (incorporating the sdnorm function) to draw each normal distribution. This gives total control of the colours (and potentially any other graphic parameter).
And not to forget - this is where I got the code for the sdnorm function - and other useful insights
Any suggestions for how I can plot mixEM type data using ggplot2
Thanks as always to StackOverflow and the contributors who provide such helpful advice.

R - Adding series to multiple plots

I have the following plot:
plot.ts(returns)
I have another dataframe ma_sd which contains the rolling SD from moving averages of the above returns. The df is structured exactly like returns. Is there a simple way to add each line to the corresponding plots?
lines(1:N, ma_sd) seemed intuitive, but it does not work.
Thanks
The only way I can see you doing this is to plot them separately. This code is a bit clunky but will allow you full flexibility to be able to specify labels and axis ranges. You can build on this.
par(mfrow=c(3,1),oma=c(5,4,4,2),mar=c(0,0,0,0))
time<-as.data.frame(matrix(c(1:length(returns[,1])),length(returns[,1]),3))
plot(time[,1],returns[,1],type='l',xaxt='n')
points(time[,1],ma_sd[,1],type='l',col='red')
plot(time[,2],returns[,2],type='l',xaxt='n')
points(time[,2],ma_sd[,2],type='l',col='red')
plot(time[,3],returns[,3],type='l')
points(time[,3],ma_sd[,3],type='l',col='red')

Resources