R Highcharter, hw_grid inside of hw_grid - r

I am using the highcharter package in R to create subplots, a function that highcharter calls hw_grid. It's fairly straightforward to create a couple plots stacked on top of one another using the following as a code example to get a 3 x 1 grid of charts.
library(dplyr)
library(highcharter)
MyData <- tibble(xvals=c(1,2,3),yvals=c(4,5,6))
h1 <- highchart() %>%
hc_add_series(MyData,'line',hcaes(x=xvals,y=yvals))
h2 <-highchart() %>%
hc_add_series(MyData,'line',hcaes(x=xvals,y=yvals))
h3 <- highchart() %>%
hc_add_series(MyData,'line',hcaes(x=xvals,y=yvals))
subplot <- hw_grid(h1,h2,h3,ncol = 1)
What I'd like is for chart number 2 to be a subplot in and of itself, but the hw_grid function doesn't seem to like being nested. In other words, instead of having a 1/1/1 layout of charts vertically, I'd have a 1/2/1 layout of charts, where my second row contains two charts side by side. Here would be an example of how I would expect that to work.
library(dplyr)
library(highcharter)
MyData <- tibble(xvals=c(1,2,3),yvals=c(4,5,6))
h1 <- highchart() %>%
hc_add_series(MyData,'line',hcaes(x=xvals,y=yvals))
h2 <-highchart() %>%
hc_add_series(MyData,'line',hcaes(x=xvals,y=yvals))
h3 <- highchart() %>%
hc_add_series(MyData,'line',hcaes(x=xvals,y=yvals))
h4 <- highchart() %>%
hc_add_series(MyData,'line',hcaes(x=xvals,y=yvals))
hSubplot <- hw_grid(h2,h3,ncol=2)
subplot <- hw_grid(h1,hSubplot,h4,ncol = 1)
Thanks in advance for your assistance

Related

Remove value display from eCharts4R gauge chart

Given this example
library(echarts4r)
library(magrittr)
gauge_out <- e_charts() %>%
e_gauge(41,"Percent")
print(gauge_out)
You get this gauge chart
I'd like to NOT have the number "41" displayed at the bottom. From looking through the eCharts docs if there's a quick way to do that I seem to be missing it.
Got it...
library(echarts4r)
library(magrittr)
gauge_out <- e_charts() %>%
e_gauge(41,"Percent",
detail = list(
show = FALSE
))
print(gauge_out)

Making barplot with offset baselines using ggplot2

If I'm using the default R graphics package, then I can make plots with staggered baselines.
Here is an example using the CPTtools package (which is available on github)
library(devtools)
install_github("ralmond/CPTtools")
library(CPTtools)
margins <- data.frame (
Trouble=c(Novice=.19,Semester1=.24,Semester2=.28,Semseter3=.20,Semester4=.09),
NDK=c(Novice=.01,Semester1=.09,Semester2=.35,Semseter3=.41,Semester4=.14),
Model=c(Novice=.19,Semester1=.28,Semester2=.31,Semseter3=.18,Semester4=.04)
)
margins <- as.matrix(margins)
baseline <- apply(margins[1:2,],2,sum)
stackedBarplot(margins,offset=-baseline,
main="Marginal Distributions for NetPASS skills",
sub="Baseline at 2nd Semester level.",
col=hsv(223/360,.2,0.10*(5:1)+.5))
This produces the output:
The key is the offset argument which is passed to the barplot function.
For various reasons, I'm trying to rewrite the graphics of CPTtools using ggplot. I can't figure out how to adjust the position of the stacked bar. The closest I've come is
library(tidyverse)
margins <- data.frame (
Trouble=c(Novice=.19,Semester1=.24,Semester2=.28,Semseter3=.20,Semester4=.09),
NDK=c(Novice=.01,Semester1=.09,Semester2=.35,Semseter3=.41,Semester4=.14),
Model=c(Novice=.19,Semester1=.28,Semester2=.31,Semseter3=.18,Semester4=.04)
)
tibble::rownames_to_column(margins,var="Level") %>%
tidyr::pivot_longer(-Level,
names_to="Attribute", values_to="probability") ->
marg
marg %>% filter(Level=="Novice" | Level=="Semester1") %>%
group_by(Attribute) %>% summarize(baseline=sum(probability)) ->
bases
ggplot(marg,aes(Attribute,probability,fill=Level)) +
geom_col(position=position_stack() +
scale_fill_brewer(palette="Blues")

Narrow down DT using plotly_click in Rmarkdown

Plot plotly heatmap on Rmarkdown. I want to display the DT of the clicked data by clicking the heatmap. It was possible with Shiny. Is it possible to reproduce this function with Rmarkdown? Thank you
rmarkdown.Rmd
```{r}
library(plotly); library(DT); library(shiny)
p <- plot_ly(data=iris, x=~Sepal.Length, y=~Sepal.Width, z=~Petal.Length, type="heatmap", source = "heat")
p
observeEvent(event_data("plotly_click", source = "heat"),{
x <- event_data("plotly_click", source = "heat")$x
iris_ <- filter(iris, Sepal.Length == x)
dt <- datatable(iris_)
})
dt
``` 
This is the only way I could get it to work, but hoping you can convert this into a heatmap. For me, the heatmap wasn't rendering properly. This example uses the crosstalk function and utilizes brushing and will auto render the DT table of the selected table.
```{r}
library(ggplot2)
library(plotly)
library(DT)
m<-highlight_key(iris)
p<-ggplot(m,aes(Sepal.Length,Sepal.Width))+geom_point(aes(color = Species))
gg<-highlight(ggplotly(p),"plotly_selected")
crosstalk::bscols(gg,DT::datatable(m))
```

Plot Pretty and aligned Colored Dendrograms in R

I am using the Sparcl package (https://cran.r-project.org/web/packages/sparcl/sparcl.pdf) to plot dendrograms in R. In my specific problem, I am clustering the groups according to one criterion, and I want to visualize by coloring based on another criterion (the point of this is to show that the cluster coincides (or does not), with another characteristic. I have been able to do this with the Sparcl package, to highlight the nodes that I want to emphasize:
df <- read.delim("the_data_matrix.txt");
d <- dist(as.matrix(df))
hc = hclust(d)
y[]='black'
y[list_of_nodes$V1]='red' # This will allow me to color only certain branches red, leaving the others black
If I plot with the standard plotting function, I can control various parameters, such as labels and text size with hang and cex (but cannot color any branches) (In the picture this is "Dendrogram 1"):
plot(hc,hang=-10,cex=.1)
On the other hand, if I plot using the ColorDendrogram function within Sparcl, I can get a colored dendrogram, but lose formatting options (In the picture this is "Dendrogram 2"):
ColorDendrogram(hc, y = y, branchlength = 4)
ColorDendrogram gave me errors when I used hang and cex to control text size and placement.
My Question
Does anyone know how to fix this, either within the Sparcl package or another one? I would like to have flexibility of color that ColorDendrogram has, but not lose formatting capabilities.
Try the package dendextend (vignette), which should give you all flexibility:
library(dendextend)
d1 <- mtcars %>% dist %>% hclust %>% as.dendrogram
d2 <- mtcars %>% dist(method="minkowski") %>% hclust(method="single") %>% as.dendrogram
vals <- grep("Merc", rownames(mtcars), val=T) # highlight branches leading to "Merc..."
par(mfrow=c(2, 1))
d1 %>% set("by_labels_branches_col", value = vals) %>% set("hang_leaves", -10) %>% set("labels_cex", .1) %>% plot
d2 %>% set("by_labels_branches_col", value = vals) %>% plot

ggvis slider for bar plots

Am using R version 3.2.2. I am trying to create a bar plot with date on the x axis with a slider to control the number of date values displayed. I cannot figure what option helps me do this. I tried:
library(dplyr)
library(ggvis)
library(shiny)
shinyServer(function(input,output){
final_data %>%
ggvis(~Date, ~work_left) %>%
layer_bars(width = input_slider(min = 1, max = 10)) %>%
along with a host of other options including size which I found on other help sites. But none of them work.
My ui.r is a simple display of the chart in the main panel.
Any help is appreciated. Thanks

Resources