R highcharter - Two barchart in same plot with different X-axis - r

I am trying to do the following:
I have a two datasets about my company. The first one has, say, the top 20 growing sellers. The second one has the bottom 20 losing sellers. So, it's something like this:
growing_seller <- c("a","b","c","d","e","f","g","h","i","h")
sales_yoy_growing <- c(100000,90000,75000,50000,37500,21000,15000,12000,10000,8000)
top_growing <- data.frame(growing_seller,sales_yoy_growing)
losing_seller <- c("i","j","k","l","m","n","o","p","q","r")
sales_yoy_losing <- c(-90000,-75000,-50000,-37500,-21000,-15000,-12000,-10000,-8000,-5000)
bottom_losing <- data.frame(losing_seller,sales_yoy_losing)
I am trying to plot both charts in the same plot using DIFFERENT categories, corresponding to the sellers' name. So what I have so far is this:
library(highcharter)
growing_seller <- c("a","b","c","d","e","f","g","h","i","h")
sales_yoy_growing <- c(100000,90000,75000,50000,37500,21000,15000,12000,10000,8000)
top_growing <- data.frame(growing_seller,sales_yoy_growing)
losing_seller <- c("i","j","k","l","m","n","o","p","q","r")
sales_yoy_losing <- c(-90000,-75000,-50000,-37500,-21000,-15000,-12000,-10000,-8000,-5000)
bottom_losing <- data.frame(losing_seller,sales_yoy_losing)
highchart() %>%
hc_add_series(
data = top_growing$sales_yoy_growing,
type = "column",
grouping = FALSE
) %>%
hc_add_series(
data = bottom_losing$sales_yoy_losing,
type = "column"
)
This is what I want to achieve graphically: Chart example
Now,I would like to have a different category array per each independent x-axis: something like the possibility to have "two hc_xAxis" controls, where I could specify per each plotted series its own categories.
My final aim is to, then, have the seller's name as I parse over each of the different columns.
Hope I was clear enough :)
Thanks

Highcharts displays the point's name in the tooltip by default. You just need to point the name value in your data.
You can do it this way:
top_growing <- data.frame(name = growing_seller, y = sales_yoy_growing)
This is the whole code:
library(highcharter)
growing_seller <- c("a","b","c","d","e","f","g","h","i","h")
sales_yoy_growing <- c(100000,90000,75000,50000,37500,21000,15000,12000,10000,8000)
top_growing <- data.frame(name = growing_seller, y = sales_yoy_growing)
losing_seller <- c("i","j","k","l","m","n","o","p","q","r")
sales_yoy_losing <- c(-90000,-75000,-50000,-37500,-21000,-15000,-12000,-10000,-8000,-5000)
bottom_losing <- data.frame(name = losing_seller, y = sales_yoy_losing)
highchart() %>%
hc_add_series(
data = top_growing,
type = "column",
grouping = FALSE
) %>%
hc_add_series(
data = bottom_losing,
type = "column"
)

Related

Is there a way to paginate by group in trelliscopejs R package?

I would like the user to be able to page through the plots in a trelliscopejs object by group. In the following example the group I would like to paginate by is "continent". In the example, there are always 10 plots per page. The first 5 plots are from continent Africa, the next three Americas and the final two Asia. Instead, I would like each page to show one continent only by default (user could then sort and filter as needed). So the 5 Africa plots would show on the first page, then the user would click next, and view the 3 Americas plots, then click next and view 3 Asia plots, and so on.
library(trelliscopejs)
library(tidyverse)
library(gapminder)
library(purrr)
library(plotly)
# reduced gapminder dataset to use for this example
set.seed(123)
data <- gapminder %>%
filter(country %in% sample(levels(country),15)) %>%
nest(data = !one_of(c("country", "continent")))
# add a plot column with map_plot
pops <- data %>% mutate(
panel = map_plot(data, function(x) {
plot_ly(data = x, x = ~year, y = ~pop,
type = "scatter", mode = "markers")
}))
# generate trelliscope object
pops %>%
arrange(continent, country) %>%
trelliscope(name = "populations", nrow = 2, ncol = 5)
Would it be possible to paginate by group in this way?

how to Showing counts in echarts4r `e_pie` pie ,charts bar charts in R

I have a column that contains 4 variables which are( Bad , Good , Very Good , Excellent )
I need to count how much they repeats in that column and compare each of them and presint to me in pie chart and bar chart in echarts4r
For example : df <- data.frame( var = c("low","low","low","high") ) i want the same result as ggplot(df)+geom_bar(aes(var)).
First you need to create a dataframe which shows the count per var and after that you can use this in e_chart with e_bar like this:
df <- data.frame( var = c("low","low","low","hight") )
library(dplyr)
library(echarts4r)
df_result <- df %>%
count(var) %>%
arrange(n)
plot <- df_result %>%
e_charts(x = var) %>%
e_bar(n, legend = FALSE, name = "var")
Output:
Which is the same result using ggplot:
ggplot(df)+geom_bar(aes(var))

Looping through variables to make many boxplots

I am using from the package OlinkAnalyze and I am trying to make box plots.
install.packages("OlinkAnalyze")
library(OlinkAnalyze)
df = npx_data1
the code for the boxplot is:
plot <- df %>%
na.omit() %>% # removing missing values which exists for Site
olink_boxplot(variable = "Site",
olinkid_list = c("OID01216", "OID01217"),
number_of_proteins_per_plot = 2)
plot[[1]]
It takes values from the olinkID column. What I would like, is to loop through the column, choosing the next two olinkID at a time, to make boxplots, renaming the plot each time (e.g.plot 1 with OID01216 and OID01217 and plot 2 with OID01218 OID01219
I used a while loop.
install.packages("OlinkAnalyze")
library(OlinkAnalyze)
df = npx_data1
i <- 1
ids <- as.data.frame(unique(df$OlinkID))
while(i <= nrow(ids)){
print(i)
x <- i+1
temp <- ids[i:x,]
plotx <- df %>%
na.omit() %>% #
olink_boxplot(variable = "Site",
olinkid_list = c(paste(c(ids[i,],ids[x,]))),
number_of_proteins_per_plot = 2)
plottemp <- assign(paste0("plot_",ids[i,],"_",ids[i,]),plotx)
i <- i+2
}
If you want the loop, you could write like this:
for i in seq(from = 1, to = length(data$OlinkID), by = 2){
the plot code
}
This way you can access the two observations you want by data$OlinkID[i] or data$OlinkID[i+1].
So the boxplot code should be
plot <- data %>%
na.omit() %>%
olink_boxplot(variable = "Oxwatchtime",
olinkid_list = c(data$OlinkID[i],data$OlinkID[i+1]),
number_of_proteins_per_plot = 2)
If you want to save the plots, add a ggsave() or a png()/pdf() in the loop to save them externally or create a list with them using ggarrange() function from ggpubr package. Let me know if it works as you intended.
OlinkAnalyze::olink_boxplot() plots several plots until all proteins specified under the olinkid_list argument are plotted. The number_of_proteins_per_plot argument determines the number of IDs plotted on one plot.
Try this:
library(OlinkAnalyze)
data("npx_data1")
ids <- unique(npx_data1$OlinkID)
olink_boxplot(npx_data1,
variable = "Site",
olinkid_list = ids,
verbose = TRUE,
number_of_proteins_per_plot = 2)
The code runs for a while as each plot takes time to be generated. When it completes you can use the arrow buttons in RStudio to look at all the plots.

Venn Diagrams with `venneuler` in `R`: delete the name of the set from the plot and add elements name

For the past few days I have been trying to figure out how to draw a Venn diagram from an arbitrary number of sets and came across the R package venneuler. This code:
genes <- paste("gene",1:20,sep="")
df = data.frame(term=character(), class=character(), stringsAsFactors = FALSE)
for (k in 1:15) {
df2=data.frame(term=sample(genes,10), class = rep(paste("class ",k,sep=""),10), stringsAsFactors =
FALSE)
df<-rbind(df,df2)
}
library(rJava)
library(UpSetR)
library(tidyverse)
library(venneuler)
library(grid)
v <- venneuler(df)
par(cex = 0.5)
plot(v)
produces a figure like this:
This figure was just what I was looking for. Anyway, I would like to remove the name of the set (class 1, class 2 etc.) from the plot, and instead add the elements (e.g. gene1, gene2) contained within each set.
You could directly modify v$labels:
library(venneuler)
library(dplyr)
library(stringr)
v <- venneuler(df)
par(cex = 0.5)
# Create the list of genes per class
classgenes <- df %>% group_by(class) %>%
summarize(labels = paste(stringr::str_sort(term,numeric=T),collapse = '\n')) %>%
ungroup
# Order the class new label according to v$labels order
newlabels <- left_join(data.frame(class = v$labels), classgenes)
#> Joining, by = "class"
# Modify the labels
v$labels <- newlabels$labels
plot(v)

Add layer underneath interactive layer with ggvis

I want to use the tooltip function within ggvis to create hover text for specific points along a curve. I can get the plot to form, but the text in the hover field won't show up. This occurs when I try to add a background layer that should not be considered part of the interactive part of the visualization. Below is some code illustrating:
library(ggvis)
# one-compartment oral concentration curve
comp1.oral <- function(ka,ke,v,f,dose,time){
(ka * dose * f)/ (v*(ka-ke)) * (exp(-ke * time) - exp(-ka*time))
}
time <- 0:200 # time points to create curve
tp <- 6 # number of times to sample
tmax <- max(time)
#generically choosing tp points to sample at
tnew <- exp(seq(0,log(tmax),length=(tp)))
#computing the concentration (y value)
y <- comp1.oral(.1,.03,4,1,100,tnew)
#creating dataframe with values
# PK and ECG should be in the hover text
d1 <- data.frame(
Conc= y,
Time=tnew,
PK = 1:tp,
ECG= "No"
)
# creating a column with the text to appear in the hover box
d1$long <- paste0("PK: ",d1$PK,"<br>","ECG: ",d1$ECG,"<br>")
#creating another data frame to input the time-conc curve as a background layer
d2 <- data.frame(
x=time,
y=comp1.oral(.1,.03,4,1,100,time)
)
The code below will form the plot I want but without the hover text.
d1 %>%
ggvis(x = ~Time, y=~Conc) %>%
layer_points(size.hover:=200) %>%
layer_paths(~x,~y,data=d2) %>%
add_tooltip(function(d1){
if (!is.null(d1$Time)) paste0("PK:", "<br>ECG:", "<br>Time: ", as.character(round(d1$Time)), " minutes post-dose")
}, "hover")
I would like to get the otehr values from d1$long into the hover text box. I tried adding it similar to what is seen in the rotten tomatoes shiny example, but it wouldn't work.
I tried the following, but it can't seem to find the additional text in the variable d1$long
d1 %>%
ggvis(x = ~Time, y=~Conc, key := ~long) %>%
layer_points(size.hover:=200) %>%
layer_paths(~x,~y,data=d2) %>%
add_tooltip(function(d1){
if (!is.null(d1$Time)) paste0(as.character(d1$long),"Time: ", as.character(round(d1$Time)), " minutes post-dose")
}, "hover")
The error is that the variable should be passed in the layer_points
d1 %>%
ggvis(x = ~Time, y=~Conc) %>%
layer_points(size.hover:=200, key := ~long) %>%
layer_paths(~x,~y,data=d2) %>%
add_tooltip(function(d1){
if (!is.null(d1$Time)) paste0(as.character(d1$long),"Time: ", as.character(round(d1$Time)), " minutes post-dose")
}, "hover")

Resources