I have been using R Markdown to reproduce R code and report on some data processing. In terms of plotting, there is a difference between what is output to the RStudio IDE and what is rendered via knitr in html. The follwing is the code that applies to both situations
barplot(top10summariseDamage$totalDamage,
names.arg = c('Hurricane Opal','Heavy Rain/Severe Weather',
'Tornadoes, TSTM Wind, Hail', 'Severe Thunderstorm',
'Wild Fires', 'Hail Storm', 'Major Flood',
'Hurricane Opal/High Winds', 'Winter Storm/High Winds',
'Record Cold'),
ylim = c(0, 5*10^10), las = 2, ylab = 'Dollars Damage ($)',
main = "Economic Damage by Event Type")
The following is a dput of the vector being plotted, where I have added commas to the number for additional clarity
c(31728479000, 2.5e+10, 16000002500, 12053600200, 6.241e+09,
2.41e+09, 1.05e+09, 1000010000, 600005000, 5.6e+08)
Which in RStudio correctly produces the following
As you can see the left most bar is less than 4e10. However, when I use knitr to produce an html report, the following is rendered
As you can see the bar values are literally off the scale, even though I increased the y axis by a factor of 10. Any help appreciated.
Related
When I use base R to plot something, then certain characters will not be displayed correctly, e.g.
plot(1:10,1:10, xlab = paste(letters[1:26], collapse =''), ylab = paste(letters[1:26], collapse =''))
produces the following plot:
Characters such as h, n and u are chopped off in the console view - however only on the y-axis. Exporting the plot via png() does not work, either. I have noticed that this problem only occurs when the code is executed in RStudio (Version 2022.07.0 Build 548, Windows 10).
Theplot method for RFspatialGridDataFrame outputs a weird blue "subtitle" when passing main or axis label arguments to the plot function.
install.packages("RandomFields")
library(RandomFields)
model <- RPbernoulli(RMexp(scale = 3), threshold = 1)
set.seed(100)
simulation <- RFsimulate(model, x = 1:138, y = 1:74)
plot(simulation, main = "This is an example title")
The following is a screeshot of the output
Strangely enough, this appears to be a feature, as running other example for the RandomFields documentation shows.
Is there any way of not outputting this blue repeated title? I have tried fiddling with other graphical arguments (such as setting legend = F) but the behavior does not change.
I have contacted the maintainers of the RandomFields package, and it appears to be a setting in RFoptions.
By setting RFoptions(grPrintlevel = 0) before the plots, this behavior can be avoided. More information can be found under ?RFoptions in section 10 regarding graphics. The relevant part regarding grPrintlevel reads
grPrintlevel: integer values 0, 1, 2; only relevant when simulations are
plotted. The higher the more text is shown in the plot.
Default: 1.
Edit: Thank you to Javier for his suggestion. I forgot to mention that I would like to incorporate this interactive plot into a report / dashboard, so something that works with a HTML document from RMarkdown would be ideal, but a dashboard solution would also be fine.
Consider the following plots; the red line represents the actual data, while the green line plots predictions generated by a model:
The predictions of two different models are displayed; one trained over the first 100 hours, and the other over the first 216 hours. Predictions are then generated for the unseen data-points, then plotted.
What I would like to do, is train n models, eg. one every 12 hours in an expanding window fashion. After having done this, I would like to present the results in an interactive fashion where the user can click/slide something to move the vertical line back and forth, thereby changing which model's predictions are displayed. The point would be to intuitively show the effect of different training lengths.
I'm new to shiny and interactive plots in R; can this be done without too much trouble?
You can with the manipulate package for quick interactive plots. Shiny requires more fine-tuning and it is more time-consuming.
Here is a reproducible example for you to test out:
This creates the slider bar:
library(manipulate)
manipulate(plot(1:x), x = slider(1, 100))
Put your code here for the creation of the interactive plot:
manipulate(
plot(cars, xlim = c(0, x.max), type = type, ann = label),
x.max = slider(10, 25, step=5, initial = 25),
type = picker("Points" = "p", "Line" = "l", "Step" = "s"),
label = checkbox(TRUE, "Draw Labels"))
Check out the CRAN manipulate package for more information:
https://cran.r-project.org/web/packages/manipulate/index.html
I was able to do this with the example at the bottom of this link.
library(shiny)
sliderInput("n", "Training length:", 100, min=24, max= 11*24)
renderPlot({
plotPredictCurve(data= df, trainLength= input$n)
})
I'm having trouble adding some text to an plot of time series data in R using xts. I've produced a simple example of the problem.
My text() command seems to do nothing, whereas I can add a points to the plot. I've tried to keep the code simple by using defaults where possible
require(quantmod)
# fetch the data and plot it using default options
getSymbols('MKS.L')
plot(MKS.L$MKS.L.Close)
# try to add text - doesn't appear
text(as.Date('2012-01-01'),y=500,"wobble", cex=4)
# add a point - this does appear
testPos <- xts(600, as.Date('2012-01-01'))
points( testPos, pch = 3, cex = 4, col = "red" )
Any help appreciated - I'm pretty new to R and I've spent hours on this!
Not a direct answer, but the plot.xts function that comes with the xts package is not fully developed.
You're much better off using plot.zoo or plot.xts from the xtsExtra package (which was written as a Google Summer of Code project with the intention being to roll it into the xts package)
Either of these will work:
plot(as.zoo(MKS.L$MKS.L.Close))
text(as.Date('2012-01-01'),y=500,"wobble", cex=4)
#install.packages("xtsExtra", repos="http://r-forge.r-project.org")
xtsExtra::plot.xts(MKS.L$MKS.L.Close)
text(as.Date('2012-01-01'),y=500,"wobble", cex=4)
So I have a fairly large dataset of GPS locations corresponding to different individuals at different times. It looks like a more complicated version of this...
ID________________Year________________Julian.date________________Distance
1_________________2003______________________15_____________________200
1_________________2004______________________20_____________________500
1_________________2005______________________24_____________________462
1_________________2006______________________28_____________________51
2_________________2002______________________12_____________________248
2_________________2003______________________15_____________________571
2_________________2004______________________16_____________________685
3_________________2003______________________20_____________________521
3_________________2004______________________25_____________________1251
3_________________2005______________________29_____________________225
3_________________2006______________________54_____________________144
What I am trying to do is separate the data out by year and individual. So each individual with have a boxplot of their Distances and the corresponding Julian date. I am able to create a massive pdf of all the plots (12X11) on one sheet using the lattice package (Separator is a column combining the ID and Year columns)..
> barchart(Julian.date~Distance|factor(Separator),data=data)
This isn't particularly helpful as I can't do much with such a massive pdf. So I tried restricting the number of plots per sheet to 1 using...
> barchart(Julian.date~Distance|factor(Separator),data=data,layout=c(1,1))
Which results in all the plots flying past me and none of them exporting to pdf. I have tried searching for a way to accomplish this, but so far no luck. If anyone knows a way of getting these to export as they fly past I would be extremely thankful.
So thanks in advance if anyone out there can help out. And if you need any more information, let me know, I tend not to use the terminology properly.
Ayden
I'm not sure what you are doing, or doing wrong as you don't show code, but using an example modified from ?barchart I see a PDF with multiple pages using this code:
foo <- barchart(yield ~ variety | site, data = barley,
groups = year, layout = c(1,1), stack = TRUE,
auto.key = list(space = "right"),
ylab = "Barley Yield (bushels/acre)",
scales = list(x = list(rot = 45)))
pdf("foo.pdf", onefile = TRUE)
print(foo)
dev.off()
onefile = TRUE should be the default and allows multiple pages in a single PDF. The other thing I do is print the barchart object in the pdf() wrapper; again, I don't think this is required if you are running R interactively but it will be needed if this is a batch or script based job.