Dynamic title and sliders using manipulate package in Rstudio - r

Using the package manipulate in Rstudio, I'm trying to create a scatterplot in which I can select among several data frames using a picker and then using sliders I control the columns I'd like to plot for each axis. For example, using these two datasets: mtcars and iris.
library(manipulate)
manipulate(
plot(dataset[, xaxis] ~ dataset[, yaxis],
dataset,
xlab = colnames(dataset)[xaxis],
ylab = colnames(dataset)[yaxis],
main = title),
xaxis = slider(1, 10),
yaxis = slider(1, 10),
dataset = picker("mtcars" = mtcars, "iris" = iris),
title = picker("mtcars", "iris")
)
It works ok, however, I'm struggling with two questions:
How to change dynamically the title of the plot based on the selected dataset (mtcars or iris) instead of manually using another picker as I do in the example above. I'm unable to get the name of the selected data frame and pass it as a character title.
How can I determine dynamically the max argument of the sliders, instead of hardcoding in the sliders from 1 to 10. For example mtcars has 11 columns and iris 5. Or better still, select the columns for each axis by name. I've tried many different ways but I think the problem is that I can't pass variables used in a control (dataset) to others (sliders). For example, this generates an error:
xaxis = slider(1,as.numeric(dim(dataset)[2]))

I took a different approach, after reading an example included in Learning RStudio for R Statistical Computing by Mark P. J. Van der Loo. I wrote a function that solves my problems:
library(manipulate)
scatterplot <- function(dataset){
vars <- as.list(names(dataset))
name <- sys.call()[[2]]
manipulate(
plot(dataset[, xaxis] ~ dataset[, yaxis],
xlab = colnames(dataset)[xaxis],
ylab = colnames(dataset)[yaxis],
main = as.character(name)),
xaxis = slider(1, as.numeric(dim(dataset)[2]), initial = 1),
yaxis = slider(1, as.numeric(dim(dataset)[2]), initial = 2)
)
}
scatterplot(mtcars)

Related

R: 1 Dimensional "scatterplot"

I am using the R programing language. Recently, I came across this previous stackoverflow post where it describes how to make a 1 dimensional scatter plot in R: How can I plot a 1-D plot in R?:
x <- rnorm(100,10,10)
x <- data.frame(x,1) ## 1 is your "height"
plot(x, type = 'o', pch = '|', ylab = '')
My question: is it possible to transform the above plot into a "plotly" plot?
Suppose I have the following data:
library(plotly)
x <- rnorm(100,10,10)
color <- rnorm(100, 2,1)
frame = data.frame(x,color)
Would it be possible to do something like this?
fig <- plot_ly(data = frame, x = ~frame$x, color ~ frame$color )
fig
I get the following error when running this code:
No trace type specified:
Based on info supplied, a 'histogram' trace seems appropriate.
Can someone please show me how to do this?
Thanks
Source: https://plotly.com/r/line-and-scatter/
In plotly language, a trace is the type of visualization that you would like to use to display your data. So the error basically lets you know that you have not specified any trace and that the program is picking one for you: "a histogram". For scatterplots, you need type = "scatter" and mode = "markers'.
Also, inside the plot_ly() function, once you specify the data argument, you can simply access the columns with the column name preceded by a tilde ~.
Finally, since you want a one dimensional scatterplot along the x-axis, you need to add y = " " to the plot_ly() function.
This is how you can achieve your desired result:
library(plotly)
x <- rnorm(100,10,10)
color <- rnorm(100, 2,1)
frame = data.frame(x,color)
plot_ly(type = "scatter", mode = "markers", data = frame, x = ~x, y = " ", color = ~color )
Note that plotly is a very rich framework and you can read the appropriate documentation to learn how to customize your plot to your liking.

Overlay a normal distribution in Highcharts R

I have vector that contains the stock market returns. I would like to add a normal distribution to the plot in highcharts. I have tried using dnorm() but cant seem to figure out how to plot it on the graph. I have made a work around by creating a vector using rnorm(). That said, I have to use 20k points in order for it to remove the bumps. This is time consuming.
Here is my current workaround
library(quantmod)
library(highcharter)
library(tidyverse)
getSymbols("SPY")
returns = na.omit(coredata(ROC(Ad(SPY))))
r = rnorm(20000, mean = 0, sd = sd(returns))
hchart(
density(returns), type = "area",
name = "SPY"
) %>%
hc_add_series(
density(r), type = "area",
name = "Normal"
)

R multiple lines plotly chart with customized line types

I have probably a simple R plotly question but I spent about one hour reading questions in stackoverflow and I really can't find what I need. I have a dataframe (I will share a screenshot) with different columns used to create a multiple lines plotly chart.
This is the code I use to create the plot:
plot_ly(data = df_final, x=~TENOR, y=~RATE) %>% add_trace(type='scatter',mode='lines', color=~LINE_NAME, colors = ~LINE_COL) %>%
layout(title=paste0("Market data"),
xaxis=list(title='Term (years)'),
yaxis=list(title='Yield'))
it works amazing but I would like to have the option to choose if some lines will have to be dashed, dots, or solid lines as well as their width.
I would need / want to specify this information inside the dataframe and choose the dataframe column that has such information (i.e. see the column "LINE_STYLE_FACTOR" in my attached dataframe).
I checked Multiple line chart using plotly r and Plotly r, line style by variable but I can't find how to do what I need.
The solution has to use plotly and not other charting solutions.
Thanks
At least for the line types (dash vs line), you can you 'linetype':
library(dplyr)
library(plotly)
df = data.frame(xVals = rep(1:10,2),
yVals = c(1:10, 2:11),
myColor = c(rep('Red', 10), rep('Blue', 10)),
myType = c(rep('solid', 10), rep('dot', 10)),
myName = c(rep('FirstName', 10), rep('SecondName', 10)))
plot_ly(df,
x = ~xVals,
y = ~yVals,
color = ~I(myColor),
name = ~myName,
type = 'scatter',
mode = 'lines',
linetype = ~I(myType)
)

ggplot2 equivalent of 'factorization or categorization' in googleVis in R

Due to static graph prepared by ggplot, we are shifting our graphs to googleVis with interactive charts. But when it comes to categorization we are facing many problems. Let me give example which will help you understand:
#dataframe
df = data.frame( x = sample(1:100), y = sample(1:100), cat = sample(c('a','b','c'), 100, replace=TRUE) )
ggplot2 provides parameter like alpha, colour, linetype, size which we can use with categories like shown below:
ggplot(df) + geom_line(aes(x = x, y = y, colour = cat))
Not just line chart, but majority of ggplot2 graphs provide categorization based on column values. Now I would like to do the same in googleVis, based on value df$cat I would like parameters to get changed or grouping of line or charts.
Note:
I have already tried dcast to make multiple columns based on category column and use those multiple columns as Y input, but that it not what I would like to do.
Can anyone help me regarding this?
Let me know if you need more information.
vrajs5 you are not alone! We struggled with this issue. In our case we wanted to fill bar charts like in ggplot. This is the solution. You need to add specifically named columns, linked to your variables, to your data table for googleVis to pick up.
In my fill example, these are called roles, but once you see my syntax you can abstract it to annotations and other cool features. Google has them all documented here (check out superheroes example!) but it was not obvious how it applied to r.
#mages has this documented on this webpage, which shows features not in demo(googleVis):
http://cran.r-project.org/web/packages/googleVis/vignettes/Using_Roles_via_googleVis.html
EXAMPLE ADDING NEW DIMENSIONS TO GOOGLEVIS CHARTS
# in this case
# How do we fill a bar chart showing bars depend on another variable?
# We wanted to show C in a different fill to other assets
suppressPackageStartupMessages(library(googleVis))
library(data.table) # You can use data frames if you don't like DT
test.dt = data.table(px = c("A","B","C"), py = c(1,4,9),
"py.style" = c('silver', 'silver', 'gold'))
# Add your modifier to your chart as a new variable e.g. py1.style
test <-gvisBarChart(test.dt,
xvar = "px",
yvar = c("py", "py.style"),
options = list(legend = 'none'))
plot(test)
We have shown py.style deterministically here, but you could code it to be dependent on your categories.
The secret is myvar.googleVis_thing_youneed linking the variable myvar to the googleVis feature.
RESULT BEFORE FILL (yvar = "py")
RESULT AFTER FILL (yvar = c("py", "py.style"))
Take a look at mages examples (code also on Github) and you will have cracked the "categorization based on column values" issue.

Automatically scale x-axis by date range within a factor using xyplot()

I've been trying to write out an R script that will plot the date-temp series for a set of locations that are identified by a Deployment_ID.
Ideally, each page of the output pdf would have the name of the Deployment_ID (check), a graph with proper axes (check) and correct scaling of the x-axis to best show the date-temp series for that specific Deployment_ID (not check).
At the moment, the script makes a pdf that shows each ID over the full range of the dates in the date column (i.e. 1988-2010), instead of just the relevant dates (i.e. just 2005), which squishes the scatterplot down into uselessness.
I'm pretty sure it's something to do with how you define xlim, but I can't figure out how to have R access the date min and the date max for each factor as it draws the plots.
Script I have so far:
#Get CSV to read data from, change the file path and name
data <- read.csv(file.path("C:\Users\Person\Desktop\", "SampleData.csv"))
#Make Date real date - must be in yyyy/mm/dd format from the csv to do so
data$Date <- as.Date(data$Date)
#Call lattice to library, note to install.packages(lattice) if you don't have it
library(lattice)
#Make the plots with lattice, this takes a while.
dataplot <- xyplot(data$Temp~data$Date|factor(data$Deployment_ID),
data=data,
stack = TRUE,
auto.key = list(space = "right"),
layout = c(1,1),
ylim = c(-10,40)
)
#make the pdf
pdf("Dataplots_SampleData.pdf", onefile = TRUE)
#print to the pdf? Not really sure how this works. Takes a while.
print(dataplot)
dev.off()
Use the scales argument. give this a try
dataplot <- xyplot(data$Temp~data$Date|factor(data$Deployment_ID),
data=data,
stack = TRUE,
auto.key = list(space = "right"),
layout = c(1,1),
scales= list( relation ="free")
)

Resources