plotly_POST: the plot appears in R Viewer, but not online - r

I'm trying to publish the plot online via these two lines of code in R:
library('plotly')
plot_ly(data = sampledata, x = date, y = id, type = 'bar')
plotly_POST(sampledata, filename = "Test", world_readable=TRUE)
It doesn't give me any errors or warnings and says "Success! Created a new plotly here ->"
The plot appears in R Viewer, but if I go to my plotly account (or follow the link), I see a blank graph like this:
Blank plotly bar chart
I previously was able to publish one graph online - but I don't recall changing anything in my code.
Any advice is much appreciated!

You'll have to send the whole figure:
p <- plot_ly(data = sampledata, x = date, y = id, type = 'bar')
plotly_POST(p, filename = "Test", world_readable=TRUE)
For a little more information on plotly_POST you can check out this reference: https://plot.ly/r/file-options/

Related

How to scatterplot in RStudio

I am trying to create a scatterplot in rstudio with my data. I am new to rstudio and having a lot of time understanding. The code I have found says plot().
This is what I used: plot(pa2_wti2$ï..Approving, pa2_wti2$ï..Price)
Even when I tried a single it didn't give me a scatter plot. I have tried ggplot but it says it does not find what I have put as the x.
I have tried:
ggplot(pa2_wti2) +
geom_point(aes(x = Price, y= Approving))
ggplot(pa2_wti2) +
geom_point(aes(x = ï..Price, y= ï..Approving))
Any help is welcomed. Thanks!
Here is some additional information:
wti2 <- read.csv("C:/Users/thomp/OneDrive/Desktop/FQM/Data Project 2/WTI Price Only Take 2.csv")
summary(wti2)
table(wti2)
sd(wti2$ï..Price)
wti2_prices2 = rnorm(wti2$ï..Price, mean=43.92, sd=28.1762)
pa2 <- read.csv("C:/Users/thomp/OneDrive/Desktop/FQM/Data Project 2/PA Approval Only Take 2.csv")
summary(pa2)
table(pa2)
sd(pa2$ï..Approving)
pa2_approve = rnorm(pa2$ï..Approving, mean=50.09, sd=11.46188 )
plot(pa2_wti2$ï..Approving)
plot(plot(pa2_wti2$ï..Approving, pa2_wti2$ï..Price)

R plotly - get bin sizes / breaks

given
library(plotly)
plot_ly(data = wind, x = ~r, type = "histogram")
how do I obtain the sizes of bins/breaks from the default algorithm? I checked the plotly object, I couldn't find the info. It is probably rendered on the JS side?
The output should be in sense of
hist(wind)$breaks

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

R Programming: How to show data labels in rCharts?

I have a bar chart where I want to show the value of the bar above each bar. However, using showValues results in the plot not working. Any ideas?
data2plot <-data.frame(Status=c("Open","Closed","Blocked"),Count=c(200,300,400))
a <- nPlot(Count ~ Status, data = data2plot, type = "multiBarChart")
a$chart(showValues=TRUE)
a
I'm using nplot, the nvd3 version. I'm open to changing to something else if I need to.
You can change type to discreteBarChart. To format the values displayed, you can use valueFormat, which takes a Javascript function and applies it to the values. Here is some info on d3.format function.
library(rCharts)
a <- nPlot(Count ~ Status, data = data2plot, type = "discreteBarChart")
a$chart(showValues=TRUE)
a$chart(valueFormat="#!d3.format('d')!#")
a

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.

Resources