I am using RStudio: Version 1.0.136, and I try to understand why knitr renders the histograms entailed in the commands below. Any help is appreciated.
min_ct<-as.numeric(min(hist(myfdata[myfdata$slope>low & myfdata$slope<up, ]$dy, breaks = bi)$counts))
Screenshot of 4 rendered graphics, which are not explicitly generated.
It's not a knitr issue. Calling hist causes a histogram to be rendered even if you assign the output to a variable. In the console try x = hist(rnorm(100)). What gets saved to the variable is a list with the data used to generate the histogram, but the histogram is still printed.
To create bins without printing a histogram, use the cut function to create the bins, then use table to count number of values by bin. For example, table(cut(rnorm(100), breaks=seq(-3,3,0.5))).
cut has options that affect how it assigns bins, so take a look at the help (?cut) for more info. In particular, take note of the right and include.lowest arguments.
Related
When I create a plot from a normal R script, click Export and Copy to Clipboard... I get a nice sized plot which works well to paste in a presentation. The size in the Copy Plot to Clipboard plot shows Width of 1065 and Height of 652. See below -
However when I create the sample plot in an rmarkdown and knit, I get something like this -
How can I play with plot settings in rmarkdown to produce an output like the first screenshot?
Hi there so you should stop using the console interface totally for this. You want more control over the size of plots than that can offer you. Also doing it the way you do it offers no means to autogenerate plots, what happens when you need to run a loop and make 100 images or even just like 10 or 20? That's a lot of unnecessary manual labor
Here's how you can do this....
dev.off() # this clears the plotting function
jpeg("filename", width = 100, hieght = "100") # note you can use png or
# other commands, this creates an image file for you to call a plot function on
plot(data) # any plotting function may be called here, ggplots pheatmap etc..
dev.off() # this causes R to save the image of the plot to file
Controlling image size in Rmarkdown, there is documentation on how to do this which is located here: https://bookdown.org/yihui/rmarkdown-cookbook/figure-size.html
I recommend doing what I showed above, supplying the code in markdown but not necessarily running it every time you knit the file. Instead its better to make the plot you want once then load it into markdown from a saved location so you can fiddle with the size easier. It will save you a ton of time running the same computation unnecessarily multiple time each time you knit. Again supply the code you used to make the plot in the Rmarkdown for completeness sake but don't actually run it. Making an Rmarkdown file will require you to knit multiple times repeatedly as you fiddle with the settings on your images and the text itself. Best to avoid running computation as much as possible, especially if your plots take a while to generate (heatmaps for instance can be a little heavy).
I have a txt file called values.txt that has 29172 numbers in a single column. I wish to make a histogram from this by entering the commands below:
val = read.table("values.txt", col.name = c("col1"))
hist(val$col1)
But this gives a weird histogram like the below. What's the matter with the code?
Check what's the actual range of your values via
summary(val$col1)
If the histogram shows a seemingly empty range (as in your shown example which goes to -15000) then typically you have outliers in that range. Hence, I would assume that summary tells you there is a minimum value somewhere in -15000 as the x-axis captures by default the full range of values.
You can try to specify xlim=c(-500,1000) as an option into hist() to "zoom in". Moreover, you may want to specify breaks = 500 to configure the bin size afterwards.
Alternatively I suggest working with ggplot's geom_histogram
library(ggplot2)
ggplot(val) + geom_histogram(vars(col1))
I want to display a plot in the julia language (using iJulia).
But is doesn't show the plot.
Here is a minimal working example of what I tried:
using Plots
function testplotting()
x=[1,2,3,4]
y1=[1,2,3,4]
y2=[1,2,3,4]
plt=plot(x,y)
plot!(x,y2)
return plt
end
plt=testplotting()
display(plt)
println("finished")`
But is doesn't show the plot..
Without the line where I add the extra line to plot the other array it works, but I want to plot multiple variables at the same time.
Can anyone explain why it doesn't display or how to fix it?
Try plot!(plt, xlabel = "blabla").
plot! with a single argument implicitly modifies the "current" plot, which it gets by querying the current plot in global scope. Given that you are in a function scope, the global scope has no access to the plot you just created. Thus you need to specify which plot you want to modify.
My specific issue is evident when I output 3 plots to a html report using grid.arrange, sometimes the last plot (which is a tableGrob) is cropped.
I am using 3 separate grid.arrange() statements to place a boxplot, a time series plot and a tableGrob in a html report using knitr.
Code being used to output is:
grid.arrange(p1,top=main)
grid.arrange(p2TS)
if(nrow(over5)>0){
tg=tableGrob(over5,theme=tt3,rows = NULL)
tg$widths = unit(rep(1/4, ncol(tg)), "npc")
grid.arrange(tg)
}
This displays perfectly when the tg object has less than roughly 10 rows of data.
However in testing larger datasets I found that the grid.arrange(tg) outputs in a fixed height "window" in the report which is not desirable.
My question is how can I change the height of the grid.arrange(tg) ouput to match the height of the tg object contained inside (with some top and bottom margin)??
I would appreciate understanding the mechanism by which these properties are changed so I can fine tune my output. I would have a 3x1 grid display all 3 outputs in one line of code if possible but all plots get condensed and unreadable so that is why I have 3 separate grid.arrange() statements. I Guess my real issue is I don't understand how there properties are manipulated here.
Help is greatly appreciated.
I found a workaround to use kable which prints a HTML table to the report instead of grid.arrange() with tableGrob() :
grid.arrange(p1,top=main)
grid.arrange(p2TS)
if(nrow(tg)>0){
print(knitr::kable(tg))
}
Also as I was running this code within a for loop I needed to use a workaround on this issue page which requires me to wrap the kable in a print() statement.
I made a plot in R and I want to repeat all the commands (like plot(), legend() or line()) that were carried out for this plot, with some minor changes. For example I want to set the axes to logarithmic scale and change the title of the plot.
In gnuplot I would use the replot command.
plot ...
set title "The same plot with logarithmic axes"
set logscale
replot
Is something like this possible in R. The only thing that comes to my mind of doing this (besides changing the values manually and re-run the lines of codes) would be setting up a function, that asks for all parameters that might be changed by the user.
Thanks for your help,
Sven
R uses a pen and paper graphics model - once the plot has been drawn on the device that is it. If you want to change some aspect of the plot, you need to replay the graphics function calls that produce the plot with the changes made to the code.
Depending on what you are really doing there are two options:
If this is just for you, write code in a text editor / IDE that knows R and can send chunks of code at a time to R. That way the code to produce the figure is recorded in a separate script which you can paste into/send to R making the changes you need each time to the script.
If you are going to be doing this often, then write yourself a wrapper plotting function that encapsulates the plot code you want but allows you to pass in arguments to alter the aspects you want.
Lattice and ggplot2 are a little different as they are based on grid graphics and create objects that when printed produce a plot on the device. One can manipulate that object to alter what is drawn, and with grid one can push and pop things on to / off a viewport.