What am I doing wrong with dygraph's showZeroValues option? - r

I'm trying to plot several data series onto the same plot in R, but even with the showZeroValues=TRUE argument in dyLegend(), the legend stops showing values on mouseover when at least one of the series has a y=0 at the current x. I am not sure what I am doing wrong.
Below is a simplified example:
library(dygraphs)
library(xts)
x=data.frame(a=c(1, 2, 3, 1, 0, 0, 2), b=c(2, 3, 1, 0, 1, 4, 5))
x$Date=seq(as.Date("2017-06-01"), (as.Date("2017-06-01")+dim(x)[1]-1), by="days")
d=xts(x, order.by=x$Date)[,1:2]
dygraph(d) %>%
dyOptions(drawGrid=FALSE, fillGraph=TRUE) %>%
dyLegend(labelsSeparateLines=TRUE, showZeroValues=TRUE)
On my computer the dynamic legend skips all x values at which one of the two series has y=0, as can be seen with the cursor being close to zeros but the legend still stuck on the right end of the graph: example.

I had the same issue and found out that it was caused by the xts object containing character strings. The original data frame had a Date column, which I used to create the xts object, but I did not subset the numerical data. This resulted in the xts object being created but with character values (see issue here). Surprisingly enough, the resulting plots were not much impacted, and the output was correct, which made troubleshooting less straightforward.
In your example, the following should solve the issue:
x=data.frame(a=c(1, 2, 3, 1, 0, 0, 2), b=c(2, 3, 1, 0, 1, 4, 5))
x$Date=seq(as.Date("2017-06-01"), (as.Date("2017-06-01")+dim(x)[1]-1), by="days")
d=xts(x[, 1:2], order.by=x$Date) # This is the only change in your code
dygraph(d) %>%
dyOptions(drawGrid=FALSE, fillGraph=TRUE) %>%
dyLegend(labelsSeparateLines=TRUE, showZeroValues=TRUE)

Related

plotly: adjusting the x axis so it increases at a regular interval

I'm very novice at this so this might be a dumb question but -
I have an csv data of a regular x and y values. The x values however - are not always increasing constantly. The graph that plotly made for me had x values and increased based on the data. the x value are dates so this causes some misinterpretation base on the graph. Is there a way to have the dates increase at regular interval in the graph?
Here's what the graph looks like(a snippet)
Beginning and end
You would need to convert your x-values to datetime objects first. Plotly will then recognize the x-values as date values and plot them accordingly.
from datetime import datetime
import plotly
plotly.offline.init_notebook_mode()
x = ['1961/04/12',
'1961/04/13',
'1961/05/04',
'1961/06/06',
'1961/07/20',
'1961/07/22',
'1961/08/05',
'1961/08/07',
'1962/02/19']
y = [1, 0, 0, 0, 0, 1, 2, 1, 0]
# convert your x-values to date
d = []
for t in x:
t = [int(t) for t in t.split('/')]
d.append(datetime(*t))
data = [plotly.graph_objs.Scatter(x=d, y=y,line=dict(shape='hv'))]
fig = plotly.graph_objs.Figure(data=data)
plotly.offline.iplot(fig)

ggplot heatmap failing to fill tiles

This (minimal, self-contained) example is broken:
require(ggplot2)
min_input = c(1, 1, 1, 2, 2, 2, 4, 4, 4)
input_range = c(4, 470, 1003, 4, 470, 1003, 4, 470, 1003)
density = c(
1.875000e-01,
5.598958e-04,
0.000000e+00,
1.250000e-02,
3.841146e-04,
0.000000e+00,
1.250000e-02,
1.855469e-04,
0.000000e+00)
df = data.frame(min_input, input_range, density)
pdf(file='problemspace.pdf')
ggplot(df, aes(x=min_input, y=input_range, fill=density)) +
geom_tile()
dev.off()
Producing:
Why are there big gaps?
There are gaps because you don't have data for all of the tiles. If you want to try to fill them in, your only option is to interpolate (assuming you don't have access to additional data). In theory, geom_raster() (a close relative of geom_tile()) supports interpolation. However, according to this github issue, that feature is not currently functional.
As a workaround, however, you can use qplot, which is just a wrapper around ggplot:
qplot(min_input, input_range, data=df, geom="raster", fill=density, interpolate=TRUE)
If there is too much space between the points that you have data for, you will still end up with blank spaces in your graph, but this will extend the range that you can estimate values for.
EDIT:
Based on the example that you posted, this will be the output
As you can see, there is a vertical band of white running through the middle, due to the lack of data points between 2 and 4.

make a figure in R with two panels in the top row and three in the bottom row [duplicate]

This question already has answers here:
Arrange plots in a layout which cannot be achieved by 'par(mfrow ='
(2 answers)
Closed 7 years ago.
I would really like to make a figure in R that has two panels in the top row and three in the bottom row. I would like to pull this off using the base graphics package. I do not want to use ggplot. Thoughts?
plot.mat = matrix(c(1, 1, 1, 2, 2, 2,
3, 3, 4, 4, 5, 5),
nrow = 2, byrow = T)
layout(plot.mat)
layout.show(n = 5)
# looks good
for (i in 1:5) plot(rnorm(10), rnorm(10))
The ?layout help is well-written and has plenty of examples.

How do I Order Boxes on One boxplot graph? (r)

I've read the answers to several questions of this type, so sorry for the repeat, but I had trouble understanding how they applied to how my data is laid out.
What I would like is to order the boxes on my boxplot (which has, say, four boxes) in the order I wish as opposed to alphabetically.
This is a simplified version of my current code:
TotalPer = c(1, 4, 6, 17, 4, 12)
IntPer = c(3, 8, 10, 1, 4, 8)
DomPer = c(4, 5, 10, 20, 13, 12)
IntDomBox <- data.frame(y=c(TotalPer,IntPer,DomPer),
x=c(rep("Total",length(TotalPer)),rep("International",length(IntPer)),
rep("Domestic",length(DomPer))))
with(IntDomBox, boxplot(y~x, main = "Prediction Residuals", ylab="%",
par(cex.axis=0.7)))
This produces a boxplot in alphabetical order. If I prefer to have the boxplot in the order listed in the dataframe (Total, Int, Dom) how would I do this? I know about making it reverse alphabetical instead, but in some cases that is not what I want, so I'd prefer to be able to manually assign the order.
Thanks!
One possibility out of many: Reorder the factor levels before plotting by executing IntDomBox$x <- factor(IntDomBox$x, levels=unique(IntDomBox$x))

Spidergraph in R

The following is some code that produces various spider graphs:
# Data must be given as the data frame, where the first cases show maximum.
maxmin <- data.frame(
total=c(5, 1),
phys=c(15, 3),
psycho=c(3, 0),
social=c(5, 1),
env=c(5, 1))
# data for radarchart function version 1 series, minimum value must be omitted from above.
RNGkind("Mersenne-Twister")
set.seed(123)
dat <- data.frame(
total=runif(3, 1, 5),
phys=rnorm(3, 10, 2),
psycho=c(0.5, NA, 3),
social=runif(3, 1, 5),
env=c(5, 2.5, 4))
dat <- rbind(maxmin,dat)
op <- par(mar=c(1, 2, 2, 1),mfrow=c(2, 2))
radarchart(dat, axistype=1, seg=5, plty=1, vlabels=c("Total\nQOL", "Physical\naspects",
"Phychological\naspects", "Social\naspects", "Environmental\naspects"),
title="(axis=1, 5 segments, with specified vlabels)")
radarchart(dat, axistype=2, pcol=topo.colors(3), plty=1, pdensity=30, pfcol=topo.colors(3),
title="(topo.colors, fill, axis=2)")
radarchart(dat, axistype=3, pty=32, plty=1, axislabcol="grey", na.itp=FALSE,
title="(no points, axis=3, na.itp=FALSE)")
radarchart(dat, axistype=1, plwd=1:5, pcol=1, centerzero=TRUE,
seg=4, caxislabels=c("worst", "", "", "", "best"),
title="(use lty and lwd but b/w, axis=1,\n centerzero=TRUE, with centerlabels)")
par(op)
The output of the graphs consists of two sets of line segments with different colors. Where did the second set of line segments come from? Also what is a good way to graph multiple items on the same spider graph?
You should mention that you are using the fmsb library to create the graph. The code you show is the example in the documentation. The puzzling thing at first glance is why three sets of lines are shown (not two as you imply with "second set") while there are 5 records in dat.
It is all in that same documentation you took the code from:
row 1 = the maximum values (defined in `maxmin` in the example code)
row 2 = minimum values (defined in `maxmin` in the example code)
row 3 to 5 are example data points, each row leading to one of the
three line segments that you see in the example graphs.
Just read the documentation for radarchart {fmsb} again and play with the numbers in the example as you do so. It should be pretty clear what is happening and what options you have for your own data. You can add as many data-rows and create corresponding lines as you wish. But these do tend to become unreadable if you overdo it.

Resources