Adding multiple frequency data to highchart barplot in R - r

I came across this website which show how to generate an interactive barplot using highchart. The frequency is called 'len' and I would like to know if there are len1, len2, len3 and len4, how do we include all len1–4 in highchart barplot?
I'm copying the R code they provide below:
# Load required R packages
library(highcharter)
# Set highcharter options
options(highcharter.theme = hc_theme_smpl(tooltip = list(valueDecimals = 2)))
df <- data.frame(dose=c("D0.5", "D1", "D2"),
len=c(4.2, 10, 29.5))
df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("D0.5", "D1", "D2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))
hc <- df2 %>%
hchart('column', hcaes(x = 'dose', y = 'len', group = 'supp')) %>%
hc_colors(c("#0073C2FF", "#EFC000FF"))
hc
Can someone gives some guidance? I hand drawn the plot that I mean. Thanks.

Related

How to remove empty factors from xaxis

Trying to make interactive plotly barchart with filter_select() and no-shiny work. I am working with data for a lot of airports (> 100). A barchart is typically too crowded to support the user to compare the performance observed (value VAL) at one airport (APT_x) to a subset of peers. The idea is to use a filter to have the user select the subset of airports.
# create a dummy table with data for year, airport, and oberved value
yr <- c(2017, 2018, 2019)
ap <- c("APT_1", "APT_2", "APT_3", "APT_N")
df <- expand.grid(YEAR = yr, APT = ap)
df$VAL <- c(10, 11, 12, 14, 9, 8, 7, 6, 2, 10, 12, 13)
library(plotly)
# shared data
df_sh <- highlight_key(df, key=~APT)
# filters
ap_filter <- filter_select(id="airport",label="filter airport", sharedData=df_sh, group=~APT)
# stacked bar chart
bc <- df_sh %>% plot_ly(x=~APT, y=~VAL, color=~factor(YEAR)) %>%
group_by(APT) %>%
add_bars() %>%
layout(barmode = "stack")
# arrange plot
bscols(widths = c(3, 9)
, ap_filter
, bc
)
Whenever more than one airport APT is selected, the x-axis shows all the entity-ticks between the bars.
How can this be removed/surpressed? Obviously, in the following example, APT_2 should not be shown. Thanks for any pointers.
I got an answer to the same issue here.
All that is needed is to set categoryorder = "trace" in the layout of the axis you are interested in.
In your example, it is (only difference is in the layout call of the bc definition):
library(crosstalk)
library(plotly)
# create a dummy table with data for year, airport, and oberved value
yr <- c(2017, 2018, 2019)
ap <- c("APT_1", "APT_2", "APT_3", "APT_N")
df <- expand.grid(YEAR = yr, APT = ap)
df$VAL <- c(10, 11, 12, 14, 9, 8, 7, 6, 2, 10, 12, 13)
# shared data
df_sh <- highlight_key(df, key = ~APT)
# filters
ap_filter <- filter_select(id = "airport", label = "filter airport", sharedData = df_sh, group = ~APT)
# stacked bar chart
bc <- df_sh %>% plot_ly(x = ~APT, y = ~VAL, color = ~factor(YEAR)) %>%
group_by(APT) %>%
add_bars() %>%
layout(barmode = "stack",
xaxis = list(categoryorder = "trace"))
# arrange plot
bscols(widths = c(3, 9), ap_filter, bc)

Soil profiles with coloured volume fractions with "aqp" in R

I am trying to plot a soil profile in R using the package aqp: algorithms for quantitative pedology. The profile should represent matrix colour, plus mottling colour and percentage. For that purpose, I am using the function addVolumeFraction, which works well to some extent: it plots points on the profile that correspond to the right mottling percentage for each horizon, but it doesn't assign the corresponding colours. Here an example:
#Variables for the soil profile
id <- rep(1, 4)
hor <- c("H1", "H2", "H3", "H4")
tops <- c(0,15,35,60)
bottoms <- c(15, 35, 60, 95)
mx_Hex <- c("#695F59FF", "#A59181FF", "#9E9388FF", "#A59181FF")
mot_Hex <- c("#EEB422","#EEB422", "#CD4F39", "#CD4F39")
mot_perc <- c(5, 10, 40, 8)
#Soil profile df
soildf <- data.frame(id,hor,tops,bottoms, mx_Hex, mot_Hex, mot_perc)
soildf$mx_Hex <- as.character(mx_Hex) #the class "SoilProfileCollection" needs colors as characters
soildf$mot_Hex <- as.character(mot_Hex)
# Transform df to "SoilProfileCollection"
depths(soildf) <- id ~ tops + bottoms
#Plot
plot(soildf, name = "hor", color = "mx_Hex", divide.hz = TRUE)
addVolumeFraction(soildf, "mot_perc",pch = 19, cex.min = 0.4, cex.max = 0.5, col = soildf$mot_Hex)
Soil profile plot
As you can see on the plot, the mottles' colours are mixed along the profile. I would like to have mottles of a given colour for their corresponding horizon instead. Can anybody help me to solve this problem?
Thanks!!
This works as expected in the current version of aqp available on CRAN (v1.19 released in January 2020).
I modified your example below to use alternating black and white mottles in each horizon.
library(aqp)
#Variables for the soil profile
id <- rep(1, 4)
hor <- c("H1", "H2", "H3", "H4")
tops <- c(0,15,35,60)
bottoms <- c(15, 35, 60, 95)
mx_Hex <- c("#695F59FF", "#A59181FF", "#9E9388FF", "#A59181FF")
# change mottle colors to something obviously different in each horizon
mot_Hex <- c("#FFFFFF", "#000000", "#FFFFFF","#000000")
mot_perc <- c(5, 10, 40, 8)
#Soil profile df
soildf <- data.frame(id, hor, tops, bottoms, mx_Hex, mot_Hex, mot_perc)
#the class "SoilProfileCollection" needs colors as characters
soildf$mx_Hex <- as.character(mx_Hex)
soildf$mot_Hex <- as.character(mot_Hex)
# Transform df to "SoilProfileCollection"
depths(soildf) <- id ~ tops + bottoms
#Plot
plot(soildf,
name = "hor",
color = "mx_Hex",
divide.hz = TRUE)
addVolumeFraction(
soildf,
"mot_perc",
pch = 19,
cex.min = 0.4,
cex.max = 0.5,
col = soildf$mot_Hex
)
alternating mottles

Interactive chart with r2d3 and shiny app

I am trying to add an interactive bar chart to my Shiny app, using r2d3 package. I have a dataset like this dummy sample containing date, id and motion column just for reference:
df <- data.frame(stringsAsFactors=FALSE,
date = c("2019-08-06", "2019-08-07", "2019-08-08", "2019-08-09",
"2019-08-10", "2019-08-06", "2019-08-07", "2019-08-08",
"2019-08-09", "2019-08-10"),
id = c("18100410-1", "18100410-1", "18100410-1", "18100410-1",
"18100410-1", "18100496-1", "18100496-1", "18100496-1",
"18100496-1", "18100496-1"),
useage = c(16.43, 15.78, 14.43, 15.68, 15.5, 17.08, 0, 0, 14.78, 14.57)
) %>%
mutate(date = readr::parse_date(date, format = "%Y-%m-%d"))
My aim is to have an app that user can select each id from the right menu and then we have a bar chart shows usage hours per day as a bar chart ( here is dummy example).
I have tried this for my bar plot chart section, but obviously, I am missing something here. Any help would be greatly appreciated
bar_graphD3=reactive({
grouped <- ifelse(input$id != "ALL", expr(date), expr(id), expr(usage))
data <- sel_data() %>%
group_by(!!grouped) %>%
collect(usage) %>%
mutate(
y = n,
x = !!grouped
) %>%
select(x, y)
data <- data %>%
mutate(label = x)
r2d3(data, "bar_plot_sample.js")
})

Loop functions with multiple variables for ggplot2

I want to build several plots from one large database, so that I have one plot for each Text (factor) and for each Measure (the many resulting measures of an eye tracking study). The following is a much simpler example of what I am trying to to:
Let's say this is my dataset
Text <- c(1,1,1,1,2,2,2,2,1,1,1,1,2,2,2,2)
Position <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)
Modified <- c(1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0)
Line_on_page <- c(1, 1, 1, 1, 2,2,2,2 ,1 ,1,1,1,2,2,2,2)
IA_FIXATION_DURATION <- c(250.3, 70.82, 400, 120.12, 270, 120.5, 100.54, 212.43, 250.3, 70.82, 320.29, 123.12, 260, 121.5, 100.54, 272.43)
IA_FIXATION_COUNT <- c(1,0,1,1,3,2,0, 1, 1,0,1,2,3,2,0, 2)
IA_LABEL <- c("she", "did", "not", "know", "what", "to", "say", "to", "she", "did", "not", "know", "what", "to", "do", "to")
testDF <- data.frame(Text , Position , Line_on_page, Modified, IA_FIXATION_DURATION, IA_FIXATION_COUNT, IA_LABEL)
so I want a heatmap (or another graph) for each Text (1/2/3), and for each measure (IA_FIXATION_DURATION/IA_FIXATION_COUNT)
# so first i create my vectors
library(stringr)
library(reshape2)
library(ggplot2)
library(ggthemes)
library(tidyverse)
Text_list <- unique(testDF$Text)
Measure_list <- testDF %>% dplyr::select_if(is.numeric) %>% colnames() %>% as.vector()
# create graphing function
Heatmap_FN <- function(testDF, na.rm = TRUE, ...){
# create for loop to produce ggplot2 graphs
for (i in seq_along(Text_list)) {
for (j in seq_along(Measure_list)) {
# create plot for each text in dataset
plots <- ggplot(subset(testDF, testDF$Text==Text_list[i])) +
geom_tile(aes(x=Position,
y=Line_on_page,
fill = Measure_list[j])) +
geom_text(aes(x=Position,
y=Line_on_page,
label=IA_LABEL),
color = "white", size = 2, family = "sans") +
scale_fill_viridis_c(option = "C", na.value = "black") +
scale_y_reverse() +
facet_grid(Page ~ Modified)+
theme(legend.position = "bottom") +
ggtitle(paste(Text_list[i],j, 'Text \n'))
ggsave(plots, file=paste(Measure_list[j], "_T", Text_list[i], ".pdf", sep = ""), height = 8.27, width = 11.69, units = c("in"))
}
}
}
Heatmap_FN(testDF)
now, I am pretty sure that the problem lies in the geom_tile "fill" part, where I would like to indicate to the function that I want to use the results variables one by one to produce the plot.
Any ideas on how to fix that?
Thanks

rCharts HighCharts dataLabels

I am using the following code to produce a scatterplot using rCharts & HighCharts. I each point to have a their corresponding Ticker right next to the point at all times. I would also like for the color of the dot to be determined by "Type", and all points to be circles.
library(rCharts)
x <- as.data.frame(c(1:6))
x$Tickers <- c("DBC", "IWV", "TIP", "TLT", "SPY", "MODEL")
x$Return <- c(0, 15, 4.3, 7.3, 15, 7)
x$StdDev <- c(16, 16, 6, 15, 16, 6)
x$Type <- c('Asset', 'Asset', 'Asset', 'Asset', 'Benchmark', 'Model')
x
b <- hPlot(x="StdDev", y="Return", data = x, group="Type", type = "scatter")
b
Thank you!
As is, the nodes are colored by "Type", you can force the nodes to be circles with this:
b$plotOptions(scatter=list(marker=list(symbol='circle')))

Resources