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)
})
Related
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.
I have 2 csv data files. Each file has a "date_time" column and a "temp_c" column. I want to make the x-axis have the "date_time" from both files and then use 2 y-axes to display each "temp_c" with separate lines. I would like to use plot instead of ggplot2 if possible. I haven't been able to find any code help that works with my data and I'm not sure where to really begin. I know how to do 2 separate plots for these 2 datasets, just not combine them into one graph.
plot(grewl$temp_c ~ grewl$date_time)
and
plot(kbll$temp_c ~ kbll$date_time)
work separately but not together.
As others indicated, it is easy to add new data to a graph using points() or lines(). One thing to be careful about is how you format the axes as they will not be automatically adjusted to fit any new data you input using points() and the like.
I've included a small example below that you can copy, paste, run, and examine. Pay attention to why the first plot fails to produce what you want (axes are bad). Also note how I set this example up generally - by making fake data that showcase the same "problem" you are having. Doing this is often a better strategy than simply pasting in your data since it forces you to think about the core component of the problem you are facing.
#for same result each time
set.seed(1234)
#make data
set1<-data.frame("date1" = seq(1,10),
"temp1" = rnorm(10))
set2<-data.frame("date2" = seq(8,17),
"temp2" = rnorm(10, 1, 1))
#first attempt fails
#plot one
plot(set1$date1, set1$temp1, type = "b")
#add points - oops only three showed up bc the axes are all wrong
lines(set2$date2, set2$temp2, type = "b")
#second attempt
#adjust axes to fit everything (set to min and max of either dataset)
plot(set1$date1, set1$temp1,
xlim = c(min(set1$date1,set2$date2),max(set1$date1,set2$date2)),
ylim = c(min(set1$temp1,set2$temp2),max(set1$temp1,set2$temp2)),
type = "b")
#now add the other points
lines(set2$date2, set2$temp2, type = "b")
# we can even add regression lines
abline(reg = lm(set1$temp1 ~ set1$date1))
abline(reg = lm(set2$temp2 ~ set2$date2))
I am trying to reproduce the plot from the example dataset in the robumeta package in R Studio. While the script works (as expected), I can only see a fraction of the graph. When I increase the size of the window, I get to see more but never the whole thing because apparently, my computer screen is too small and there is no option to scroll up or down. Is there a way to re-scale the forest plot or to print it to pdf (with several pages, if needed)?
Here's the code from package description file:
install.packages("robumeta")
library(robumeta)
data(oswald2013.ex1)
oswald_intercept <- robu(formula = effect.size ~ 1, data = oswald2013.ex1, studynum = Study, var.eff.size = var.eff.size, rho = 0.8, small = TRUE)
forest.robu(oswald_intercept, es.lab = "Crit.Cat", study.lab = "Study",
"Effect Size" = effect.size, # optional column
"Weight" = r.weights) # optional column
png(filename, height, width, pointsize)
#your plot code
dev.off()
in which you play around with the height, width, and pointsize parameters should allow you to save the graph to your desired view size. It won't save as a pdf, but rather a png, which should be ok if your goal is just to view the entire forest plot.
Got a ctree with four labels, but the categories are long text therefore just the first is shown.
category;presence;ratio;tested;located
palindromic_recursion;1;0;0;0
conceptual_comprehension;0;1;0;0
infoxication_syndrome;0;0;1;0
foreign_words_abuse;0;0;0;1
palindromic_recursion;1;0;0;0
conceptual_comprehension;0;1;0;0
infoxication_syndrome;0;0;1;0
foreign_words_abuse;0;0;0;1
concepts.ctree <- ctree(category ~., data)
plot(concepts.ctree)
is there any way or parameter for manipultating (rotate) text, edge label names and this way force them to be all shown in plot?
My real data is much bigger but this sample is ok to test it if you do not use zoom tool.
Regards
There wasn't an option for this up to now. But I just tweaked the development version of partykit on R-Forge to support this feature. Currently, the package is re-building but hopefully you can soon say install.packages("partykit", repos = "http://R-Forge.R-project.org") - or if you don't want to wait that long, simply check out the SVN and re-build yourself.
In the new version, you can pass the rot and just arguments to grid.text() to control rotation and justification of the x-axis labels.
Read the data:
data <- read.csv2(textConnection(
"category;presence;ratio;tested;located
palindromic_recursion;1;0;0;0
conceptual_comprehension;0;1;0;0
infoxication_syndrome;0;0;1;0
foreign_words_abuse;0;0;0;1
palindromic_recursion;1;0;0;0
conceptual_comprehension;0;1;0;0
infoxication_syndrome;0;0;1;0
foreign_words_abuse;0;0;0;1"
))
Fit the tree (using the partykit implementation of ctree()):
library("partykit")
concepts.ctree <- ctree(category ~ ., data = data)
For visualization create a viewport with sufficiently large margins on the x-axis first. Then, add the tree to the existing viewport page and set the rotation/justification arguments for the barplot.
pushViewport(plotViewport(margins = c(6, 0, 0, 0)))
plot(concepts.ctree, tp_args = list(rot = 45, just = c("right", "top")),
newpage = FALSE)
I am trying to plot FEVD (forecast error variance decomposition) for my VAR analysis. As you can see on the image, the legend screws up the graph and information. as this is an automatically created legend, I don’t know how to reposition it. I do not know much yet about plotting in R.
The only code i use to get this is :
library(vars)
var <- VAR(varTable2 , p=4 , type = "both")
plot(fevd(var, n.ahead = 10 ))
Thanks in advance
Legends do not resize well in R. You have to set your plotting window first and then chart your data.
Here's how to do it in Windows. win.graph opens a blank plotting window of the specified width. In Unix/Linux, you should look at X11() and in Mac, at quartz(). You might also consider shorter variable names.
library(vars)
data(Canada)
colnames(Canada) <-c("Long column name1","Long column name2","Long column name3","Long column name4")
var <- VAR(Canada , p=4 , type = "both")
win.graph(width=13,height=8)
plot(fevd(var, n.ahead = 10 ))