rchart nvd3 - set tickmark interval - r

I'd like to specify the interval on the xAxis of a lineChart in rchart nvd3 to be 1.
Currently I have a dynamic chart that has the xAxis being either 0-6, 0-5, or 0-3 - when xAxis only ranges from 0-3, the tickmark interval automatically becomes 0.5, but in this case doesn't make sense, and I would like it to still go by integer intervals - how do I specify that?
Currently my code looks like:
p <- nPlot( yvalue ~ xvalue, data = dat, group = "scenario_id", type = "lineChart" )
p$yAxis(tickFormat = "#!
function(d) {return d3.format('%')(d)}
!#" )

You could try to manually set the tick values by doing:
p$xAxis(tickValues=min(dat$xvalue):max(dat$xvalue))

Related

Categorical axes in plotly heatmapgl

Hi I've some problems with heatmapgl plot (but with heatmap I get the same) of plotly package.
I've a distance matrix between a series of elements. I would like to plot a heatmap with names of elements on axes and possibly also on hover text labels.
library(plotly)
values=c(22.63409, 323.93780, 366.31498, 467.94524, 461.93251, 643.61306, 665.03471, 482.49047, 500.06208, 662.71600, 673.29995, 567.99427, 558.41419, 632.86704, 615.55000, 508.01659, 462.50232, 411.39160, 399.00598, 286.55681, 268.69871)
z_matrix=matrix(NA,7,7)
z_matrix[lower.tri(z_matrix, diag=FALSE)] <- values
elements_names=c("ATHOTPlun04", "ATHOTPlun10", "ATHOTPlun16", "ATHOTPlun22", "ATHOTPmar04", "ATHOTPmar10", "ATHOTPmar16")
q <- plot_ly(
x=elements_names,
y=elements_names,
z = z_matrix, type = "heatmapgl") %>%
layout(title = 'Distances for ATHOTP route')
q
Although this is what I get when I run the code:
I tried also to set axes type to "category" in layout options but nothing seems to change. What am I missing? Thank you!
heatmap-gl is used for images where you have continuous x- and y-axes. Therefore you need to change type to a non-continuous type, e.g. type = "heatmap".
q <- plot_ly( x=elements_names,
y=elements_names,
z = z_matrix, type = "heatmap") %>%
layout(title = 'Distances for ATHOTP route')
q

Is there a way to specify the ordering of traces in R plotly?

I am dynamically generating data based on multiple user input values, and then graphing the resulting traces on a single graph. Because there are many traces, I set the alpha of the fillcolor to 1, so the colors stay crisp. I am also stacking the traces, and would like to have the trace value at the end (with the largest value) in the back of the graph, and the smallest value (the first trace) in the front. However, it seems that plotly adds each successive trace to the front. Is there any way to tell plotly to add a trace to the back, e.g.
plot <- plotly_empty()
color.first <- ## Some color with alpha of 1
color.second <- ## Some color with alpha of 1
x.data.first <- 1:10
y.data.first <- 1:10
plot <- plot %>% add_trace(x = x.data.first, y = y.data.first, fill = 'tozeroy', color = color.first, fillcolor = color.first)
x.data.second <- 1:10
y.data.second <- (1:10) + y.data.first
plot <- plot %>% add_trace(x = x.data.second, y = y.data.second, fill = 'tozeroy', color = color.second, fillcolor = color.second)
plot
Will create a plot where the second set of data (x.data.second, y.data.second) is visible, but because the fillcolor's alpha level is 1, the first set of data is not visible. I am looking for an option like
add_trace(..., toback = TRUE)
Where toback would specify that the trace being added should be added behind the existing traces.
It would be possible for me to calculate all the data and the total in one section, and then graph the resulting list of traces backwards in a different section. That feels inefficient, when I have all the data I need in one place.

Creating Hexbins with Dates in R hexbin()

I am trying to create hexbins where the x-axis is a date using the hexbin function in the hexbin package in R. When I feed in my data, it seems to convert the dates into a numeric, which gets displayed on the x-axis. I want it force the x-axis to be a date.
#Create Hex Bins
hbin <- hexbin(xData$Date, xData$YAxis, xbins = 80)
#Plot using rBokeh
figure() %>%
ly_hexbin(hbin)
This gives me:
Here's a brute force approach using the underlying grid plotting package. The axes are ugly; maybe someone with better grid skills than I could pretty them up.
# make some data
x = seq.Date(as.Date("2015-01-01"),as.Date("2015-12-31"),by='days')
y = sample(x)
# make the plot and capture the plot
p <- plot(hexbin(x,y),yaxt='n',xaxt='n')
# calculate the ticks
x_ticks_date <-
x_ticks <- axTicks(1, log = FALSE, usr = as.numeric(range(x)),
axp=c(as.numeric(range(x)) ,5))
class(x_ticks_date) <- 'Date'
y_ticks_date <-
y_ticks <- axTicks(1, log = FALSE, usr = as.numeric(range(y)),
axp=c(as.numeric(range(y)) ,5))
class(y_ticks_date) <- 'Date'
# push the ticks to the view port.
pushViewport(p$plot.vp#hexVp.off)
grid.xaxis(at=x_ticks, label = format(y_ticks_date))
grid.yaxis(at=y_ticks, label = format(y_ticks_date))

How to customize x axis in rCharts r

I have a monthly time series data set from 2013-07 to 2014-07 and I'm trying to plot a bar chart based on that data set in nPlot from rCharts. The plot looks all right however the ticks of x axis only shows 2013-09, 2013-12, 2014-03, 2014-06. So what can I do to change the ticks and show all the month (e.g. 2013-07, 2013-08, 2013-09, ..., 2014-07).
Suppose my rChart plot is called n1. Do I need to change something in n1$xAxis or n1$chart? If so how do I make this modification.
Thanks if there is any suggestions
To accomplish this you will need a little Javascript. I tried to comment in the code what you might want to change. You could also supply an array of values in the tickValues.
library(rCharts)
data(economics, package = 'ggplot2')
p6 <- nPlot(uempmed ~ date, data = economics, type = 'lineChart')
p6$xAxis(
tickFormat = "#!function(d){return d3.time.format('%b %Y')(new Date(d*60*60*24*1000))}!#"
#for everything
#,tickValues = "#!data[0].values.map(function(v){return v[opts.x]})!#"
#for something potentially more sensible
,tickValues = "#!data[0].values
.map(function(v){
return v[opts.x]
})
.filter(function(v){
return d3.time.format('%m')(new Date(v*60*60*24*1000)) == '12' && +d3.time.format('%Y')(new Date(v*60*60*24*1000)) % 10 == 0
})!#"
)
p6

Draw vertical ending of error bar line in dotplot

I am drawing dotplot() using lattice or Dotplot() using Hmisc. When I use default parameters, I can plot error bars without small vertical endings
--o--
but I would like to get
|--o--|
I know I can get
|--o--|
when I use centipede.plot() from plotrix or segplot() from latticeExtra, but those solutions don't give me such nice conditioning options as Dotplot(). I was trying to play with par.settings of plot.line, which works well for changing error bar line color, width, etc., but so far I've been unsuccessful in adding the vertical endings:
require(Hmisc)
mean = c(1:5)
lo = mean-0.2
up = mean+0.2
d = data.frame (name = c("a","b","c","d","e"), mean, lo, up)
Dotplot(name ~ Cbind(mean,lo,up),data=d,ylab="",xlab="",col=1,cex=1,
par.settings = list(plot.line=list(col=1),
layout.heights=list(bottom.padding=20,top.padding=20)))
Please, don't give me solutions that use ggplot2...
I've had this same need in the past, with barchart() instead of with Dotplot().
My solution then was to create a customized panel function that: (1) first executes the original panel function ; and (2) then uses panel.arrows() to add the error bar (using a two-headed arrow, in which the edges of the head form a 90 degree angle with the shaft).
Here's what that might look like with Dotplot():
# Create the customized panel function
mypanel.Dotplot <- function(x, y, ...) {
panel.Dotplot(x,y,...)
tips <- attr(x, "other")
panel.arrows(x0 = tips[,1], y0 = y,
x1 = tips[,2], y1 = y,
length = 0.15, unit = "native",
angle = 90, code = 3)
}
# Use almost the same call as before, replacing the default panel function
# with your customized function.
Dotplot(name ~ Cbind(mean,lo,up),data=d,ylab="",xlab="",col=1,cex=1,
panel = mypanel.Dotplot,
par.settings = list(plot.line=list(col=1),
layout.heights=list(bottom.padding=20,top.padding=20)))

Resources