using dygraph library in r to plot dual axis - r

I am trying to plot two series at different scales on same plot with dygraph lib in r.
dygraph(data.frame(x = 1:10, y = runif(10),y2=runif(10)*100)) %>%
dyAxis("y", valueRange = c(0, 1.5)) %>%
dyAxis(runif(10)*100,name="y2", valueRange = c(0, 100)) %>%
dyEvent(2, label = "test") %>%
dyAnnotation(5, text = "A")
however, The plot does not fit the data with larger scale, I cannot figure out how to align the two axises. I suspect the option independentTicks in dyAxis() function does the trick but I cannot find how to use it in the documentation. Please help out with this. Best

One way could be:
We pass the named vector of the column with higher values to dySeries function:
See here https://rstudio.github.io/dygraphs/gallery-axis-options.html
library(dygraphs)
library(dplyr)
df = data.frame(x = 1:10, y = runif(10),y2=runif(10)*100)
y2 <- df %>%
pull(y2)
names(y2) <- df$x
dygraph(df) %>%
dySeries("y2", axis = 'y2')

Related

Is there a way to set axis labels in r-plotly conditionally?

I would like to declutter a plot created with plotly and remove the repetitions of the year on the x-axis. The year should be shown only for the first month. For the rest of the labels the month (without the year) is enough.
I have tried to achieve this result with the ifelse function - without success (see reproducible example below). Is there any way to use ifelse or if_else to set the axis labels in plotly? I think it works that way in ggplot.
library(tidyverse)
library(plotly)
set.seed(42)
df <-
data.frame(date = seq(ymd('2021-01-01'), ymd('2021-12-12'), by = 'weeks'),
value = cumsum(sample(-10:20, length(seq(ymd('2021-01-01'), ymd('2021-12-12'), by = 'weeks')), replace = TRUE)))
df %>%
plot_ly(x = ~date, y = ~value) %>%
add_lines() %>%
layout(xaxis = list(dtick = "M1", tickformat = ~ifelse(date <= "2021-01-31", "%b\n%Y", "%b")))

Converting basic r barplot to ggplot

I've currently got a barplot that has a few basic parameters. However, I'm looking to try and convert this into ggplot. The extra parameters don't matter too much; the main problem that I'm having is that I'm trying to plot the sum of various columns, but I'm unable to transpose it correctly as t(data) doesn't seem to work. Here's what I've got so far:
## Subset of indicators
indicators <- clean_data[c(8, 12, 14:23)]
## Get sum of columns
indicator_sums <- colSums(indicators, na.rm = TRUE)
### Transpose for ggplot
(empty)
## Make bar plot
barplot(indicator_sums, ylim=range(pretty(c(0, indicator_sums))), cex.axis=0.75,cex.lab=0.8, cex.names=0.7, col='magenta', las=2, ylab = 'Offences Recorded Using Indicator')
You may try
library(dplyr)
library(reshape2)
dummy <- data.frame(
A = c(1:20),
B = rnorm(20, 10, 4),
C = runif(20, 19,30),
D = sample(c(10:40),20, replace = T)
)
barplot(colSums(dummy))
dummy %>%
colSums %>%
melt %>%
rownames_to_column %>%
ggplot(aes(x = rowname, y = value)) +
geom_col()

Density Plot in R Highcharter

I am trying to plot similar density plot in highcharter R. I am pretty new to highcharter, any guidance will be really appreciated.
dt <- data.frame(x=rnorm(1000),y=sample(c(0,1),size = 1000,replace = T))
library(ggplot2)
ggplot(data = dt) +
aes(x = x,fill=factor(y)) +
geom_density(adjust = 1,alpha=0.5)
My Attempts:
library(highcharter)
library(dplyr)
hcdensity(density(dt$x), area = TRUE) %>%
hc_add_series(density(dt$y), area = TRUE)
It looks like you want two curves (y = 0 and y = 1). Would just call hcdensity of dt$x, first for y=0. Then for y=1 use 'density' for hc_add_series. Use type='area' to fill.
hcdensity(dt$x[dt$y==0]) %>%
hc_add_series(density(dt$x[dt$y==1]), type='area')
If you want to include multiple curves or more generalizable solution, try this:
library(purrr)
tapply(dt$x, dt$y, density) %>%
reduce(.f = hc_add_series, type='area', .init=highchart())
This was helpful:
Create highchart density with more than 2 groups

ggvis - custom non-numeric y-axis labels showing as 'NaN'

EDIT: Sample code updated with more complex example
I have a data frame with (x, y) values and a name for each pair, and I'm plotting it in a ggvis scatterplot. SO users already pointed me to this post (How do I label plot tick marks using ggvis) and I updated my code to reflect what I saw there by using factor() to create the list of custom y-axis labels ylabels_ which is fed to values = under the add_axis() statement. Here is the new code:
library(stringi)
require(ggvis)
set.seed(13)
df <- data.frame(x = 1:15,
y = c(rep.int(1, 15),
rep.int(4, 15),
rep.int(7, 15)))
ylabels_ <- factor(c(1,4,7), labels = stri_rand_strings(3, 7))
df %>%
ggvis(~x, ~y) %>%
add_axis("y", values = ylabels_)
My axis labels are still coming back with NaN instead of the factors. Am I missing something?
SOLUTION EDIT: Here is my code that I finally got to work. Note I changed the x values to be staggered so I could distinguish between the three lines.
library(stringi)
require(ggvis)
set.seed(13)
df <- data.frame(x = c(seq(1, length.out = 15, by = 3),
seq(2, length.out = 15, by = 3),
seq(3, length.out = 15, by = 3)),
y = factor(c(rep.int(1, 15),
rep.int(4, 15),
rep.int(7, 15)), labels = c("one", "four", "seven"),
ordered = T))
df %>%
ggvis(~x, ~y) %>%
layer_points() %>%
scale_ordinal("y", domain = c("seven", "four", "one")
I don't understand why in the domain = portion I had to reverse the order of the factors for them to show in the correct order. If anyone has any insight on that, it would be very helpful!
Notice that in the linked answer, the new factor variable is used as the y variable. You can use it for your values, as well, but it's not necessarily needed.
Making the new factor based on y but using name as the labels is to get the name variable in the correct order for plotting.
df$ylabels_ <- factor(df$y, labels = stri_rand_strings(3, 7))
Because you are now working with a factor variable on the y axis, layer_points needs to be used explicitly.
df %>%
ggvis(~x, ~ylabels_) %>%
layer_points()
Note that this whole approach will only work if the original y data are integers.
Call me a fool, but I'm calling this a bug (which, BTW, is not answering your question):
## set up data
lab <- factor(1:15, labels=stri_rand_strings(15, 7))
val <- 1:15
## all works according to expectations on the "x" axis
data.frame(xx=lab, yy=val) %>%
ggvis(~xx, ~yy) %>%
layer_points() %>%
add_axis("x", values=lab,
properties=axis_props(labels=list(angle=90, fontSize = 10)))
## nothing works according to expectations on the "y" axis
## attempt 1
data.frame(xx=val, yy=lab) %>%
ggvis(~xx, ~yy) %>%
layer_points() %>%
add_axis("y", values=lab,
properties=axis_props(labels=list(angle= 0, fontSize = 10)))
## attempt 2
data.frame(xx=val, yy=as.numeric(val)) %>%
ggvis(~xx, ~yy) %>%
layer_points() %>%
add_axis("y", values=lab,
properties=axis_props(labels=list(angle= 0, fontSize = 10)))

Make dual X-axs based on different variables using ggvis

df <- data.frame(X1 = rep(1:5,1), X2 = rep(4:8,1), var1 = sample(1:10,5), row.names = c(1:5))
library("ggvis")
graph <- df %>%
ggvis(~X1) %>%
layer_lines(y = ~ var1) %>%
add_axis("y", orient = "left", title = "var1") %>%
add_axis("x", orient = "bottom", title = "X1") %>%
add_axis("x", orient = "top", title = "X2" )
graph
Obviously, the top x-axis (X2) is not correct here since it refers to the same variable as X1. I know how to create a scaled dual-y axis in ggvis. But how can I create a similar dual axis on different X? Those two X-axis should refer to different variables (X1 and X2 in this example).
I know this could be a really BAD idea to make dual X-axis. But one of my working dataset may need me to do so. Any comments and suggestions are appreciated!
The second axis needs to have a 'name' in order for the axis to know which variable to reflect. See below:
df <- data.frame(X1 = rep(1:5,1),
X2 = rep(4:8,1),
var1 = sample(1:10,5),
row.names = c(1:5))
library("ggvis")
df %>%
ggvis(~X1) %>%
#this is the line plotted
layer_lines(y = ~ var1) %>%
#and this is the bottom axis as plotted normally
add_axis("x", orient = "bottom", title = "X1") %>%
#now we add a second axis and we name it 'x2'. The name is given
#at the scale argument
add_axis("x", scale = 'x2', orient = "top", title = "X2" ) %>%
#and now we plot the second x-axis using the name created above
#i.e. scale='x2'
layer_lines(prop('x' , ~X2, scale='x2'))
And as you can see the top x-axis reflects your X2 variable and ranges between 4 and 8.
Also, as a side note: You don't need rep(4:8,1) to create a vector from 4 to 8. Just use 4:8 which returns the same vector.

Resources