Rows As Series Plotly R - r

I have a dataframe:
df<-data.frame(Level=c("Small","Bigger","Biggest"),Threshold1=c(0.9,.8,.7),Threshold2=c(0.4,.5,.6),Threshold3=c(.6,.2,.1))
I want to produce a graphic as below, but using plotly:
How would you read this in to plotly and make this happen?
I have tried rotating using t() and looked at plotly documentation, but didn't see anything about a means to use rows as series and the value of an identifier column as series name.
In my real-life data, the number of values that can exist in the Level column can change, so ideally I would like to find a solution that can scale with whatever number of values Level may consist of.
Any help is much appreciated!

It is easier if you use data.table:
library(data.table)
library(plotly
df<-data.table(Level=c("Small","Bigger","Biggest"),Threshold1=c(0.9,.8,.7),Threshold2=c(0.4,.5,.6),Threshold3=c(.6,.2,.1))
d <-melt.data.table(df, id.vars='Level')
plot_ly(d, x=~variable, y=~value,type = 'scatter', mode = 'lines', color = ~Level)
You should obtain the desired chart.

Solution in base R:
library(plotly)
df<-data.frame(Level=c("Small","Bigger","Biggest"),
Threshold1=c(0.9,.8,.7),Threshold2=c(0.4,.5,.6),Threshold3=c(.6,.2,.1))
df.reshaped <- reshape(df, varying = c("Threshold1", "Threshold2", "Threshold3"),
v.names = "Values", idvar = "Level", direction = "long",
times = c("Threshold1", "Threshold2", "Threshold3"),
new.row.names = 1:(3 * nrow(df)))
plot_ly(df.reshaped, x=~time, y=~Values, type = 'scatter', mode = 'lines', color = ~Level)
Output plot:

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)
)

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")
)

format axis labels in lattice

How can I put my axis labels in a convenient format in lattice?
require(stats)
xyplot(lat*1000000 ~ long, data = quakes)
gives me y-labels like -3.5e+0.7. I would want lattice to write the whole number.
(maybe it is easy, but I can't find a solution.)
Thank you in advance!
Create your own labels and pass them to the scales argument.
y_at <- pretty(quakes$lat*1e6)
y_labels <- formatC(y_at, digits = 0, format = "f")
xyplot(
lat*1000000 ~ long,
data = quakes,
scales = list(
y = list(
at = y_at,
labels = y_labels
)
)
)
For the formatting step, there are lots of alternatives to formatC. Take a look at format, prettyNum and sprintf to get you started.
If you want to do this with dates, then note that scales accepts a format argument for that purpose.
There are a couple of "global options" that might affect how values are printed. In this case scipen is the one you want to move:
old_op <- options(scipen=10)
xyplot(lat*1000000 ~ long, data = quakes)
options(old_op)
# probably better to restore it so the rest of you session is more "normal"

Resources