Rcharts: plot only shows first character of label - r

I recently switched from ggplot to Rcharts and have a fairly simple question about the labels.
Sample data
data_1 <- data.table(Filter = c('Filter 1', 'Filter 2'),
Amount = c(100, 50))
data_2 <- data.table(Filter = c('Filter 1'),
Amount = c(100))
Plots
hPlot(Amount ~ Filter, data = data_1, type = 'bar', group.na = 'NA\'s')
hPlot(Amount ~ Filter, data = data_2, type = 'bar', group.na = 'NA\'s')
Question:
Why do we see the correct label in the first plot, but only the first letter of the label in the second plot? This issue always occurs when the number of rows = 1 (as it is in data_2).
Does anyone has a quick fix / workaround?

Related

Plotly gauge graph and crosstalk filtering for flexdashboard

I am trying the create a plotly gauge graph for a flexdashboard which should change value depending on the chosen filter in crosstalk::filter_select().
I have tried and tried but cannot get the filter to work. This is an example with mtcars of what I am trying to do. I noticed that if the SharedData object has only one value, then it works, but otherwise plotly does not show any data.
mtcars_data <- tibble::rownames_to_column(mtcars, "Car")
shared_mtcars <- SharedData$new(mtcars_data)
row1 <- bscols(filter_select("Car", "Car", shared_mtcars, ~Car, multiple = F)
)
fig <- plot_ly(shared_mtcars,
domain = list(x = c(0, 1), y = c(0, 1)),
value = ~mpg,
title = list(text = "MPG"),
type = "indicator",
mode = "gauge+number")
bscols(row1, fig, widths = 12)
This code results in a graph with no data. If I subset mtcars_data to take the first row or the first two rows (which happen to have the same value for mpg) then it works. If I subset rows 1 and 3, it doesn't.
I might be missing something - in that case would really appreciate any feedback.

How to plot multiple lines in radar chart using split in plotly

I have tried using split trace with scatterpolar and it seems to partly work but can't get it to plot the values for all 10 variables. So I want each row (identified by "ean") be plotted as its own line using the values from X1 to X10.
library(tidyverse)
library(vroom)
library(plotly)
types <- rep(times = 10, list(
col_integer(f = stats::runif,
min = 1,
max = 5)))
products = bind_cols(
tibble(ean = sample.int(1e9, 25)),
tibble(kategori = sample(c("kat1", "kat2", "kat3"), 25, replace = TRUE)),
gen_tbl(25, 10, col_types = types)
)
plot_ly(
products,
type = 'scatterpolar',
mode = "lines+markers",
r = ~X1,
theta = ~"X1",
split = ~ean
)
How can I get plotly to plot all variables in the radarchart (X1-X10)? Usually I would select the columns with X1:X10 but I can't do that here (I think it has to do with that ~ is used to select variable here).
So I want the result to look something like this (but I only show lines and not filled polygons and I would have more products). So in the end 25 products is a lot but I am connecting it so that the user can select the diagrams it wants to show.
In plotly it's convenient to use data in long format - see ?gather.
Please check the following:
library(dplyr)
library(tidyr)
library(vroom)
library(plotly)
types <- rep(times = 10, list(
col_integer(f = stats::runif,
min = 1,
max = 5)))
products = bind_cols(
tibble(ean = sample.int(1e9, 25)),
tibble(kategori = sample(c("kat1", "kat2", "kat3"), 25, replace = TRUE)),
gen_tbl(25, 10, col_types = types)
)
products_long <- gather(products, "key", "value", -ean, -kategori)
plot_ly(
products_long,
type = 'scatterpolar',
mode = "lines+markers",
r = ~value,
theta = ~key,
split = ~ean
)

Change the 'Frame' Label in Plotly Animation

TLDR: I want to label the frame slider with the three letter abbreviation instead of the number for each month.
I created a bar chart showing average snow depth each month over a 40 year period. I'm pulling my data from NOAA and then grouping by year and month using lubridate. Here is the code:
snow_depth <- govy_data$snwd %>%
replace_na(list(snwd = 0)) %>%
mutate(month_char = month(date, label = TRUE, abbr = TRUE)) %>%
group_by(year = year(date), month = month(date), month_char) %>%
summarise(avg_depth = mean(snwd))
The mutate function creates a column (month_char) in the data frame holding the three letter abbreviation for each month. The class for this column is an ordered factor.
The code below shows how I'm creating the chart/animation:
snow_plot <- snow_depth %>% plot_ly(
x = ~year,
y = ~avg_depth,
color = ~avg_temp,
frame = ~month,
text = ~paste('<i>Month</i>: ', month_char,
'<br><b>Avg. Depth</b>: ', avg_depth,
'<br><b>Avg. Temp</b>: ', avg_temp),
hoverinfo = 'text',
type = 'bar'
)
snow_plot
This code generates a plot that animates well and looks like this:
What I'd like to do is change the labels on the slider so instead of numbers, it shows the three letter month abbreviation. I've tried switching the frame to ~month_char which is the ordered factor of three letter month abbreviations. What I end up with, isn't right at all:
The data frame looks like:
I fear, with the current implementation of animation sliders in R's plotly API the desired behaviour can't be realized. This is due to the fact, that no custom animation steps are allowed (this includes the labels). Please see (and support) my GitHub FR for further information.
This is the best I was currently able to come up with:
library(plotly)
DF <- data.frame(
year = rep(seq(1980L, 2020L), each = 12),
month = rep(1:12, 41),
month_char = rep(factor(month.abb), 41),
avg_depth = runif(492)
)
fig <- DF %>%
plot_ly(
x = ~year,
y = ~avg_depth,
frame = ~paste0(sprintf("%02d", month), " - ", month_char),
type = 'bar'
) %>%
animation_slider(
currentvalue = list(prefix = "Month: ")
)
fig
(Edit from OP) Here's the resulting graph using the above code:

R Plotly Returns Blank Sunburst Chart

I am trying to create a sunburst chart using plotly, but it returns a blank chart. My dataset contains currency information which is similar to below (except more rows):
df <- data.frame(
MainGroup = c("A", "A","A"),
Currency = c("USD","EUR","GBP"),
Percent = c(90,5,5),
Category = c("USD","Other","Other"),
stringsAsFactors = FALSE)
The Category is based on the Percent column: anything less than 10 will be categorized as "Other". I tried plot_ly(df, labels = ~Currency, parents = ~Category, values = ~Percent, type = 'sunburst'), but it returned a blank chart. I'd like to create a chart like this:
df <- data.frame(
MainGroup = c("A", "A","A"),
Currency = c("USDollars","EUR","GBP"),
Percent = c(90,5,5),
Category = c("USD","Other","Other"),
stringsAsFactors = FALSE)
plot_ly(labels = c(" ", "USD","Other",df$Currency), parents = c(""," "," ",df$Category), values = c(100,90,10,df$Percent), type = 'sunburst',
branchvalues='total')
Multiple problems here (I have to say these sunburst plots are tricky):
You have to provide for each element the parents, in your example Otherhas no parent
You have two different items called USD, this is a problem to specify parents, I changed one to USDollars
To get whole circles, you have to put branchvalues='total'

Scatter/Bubble plot not correctly plotting using rCharts and dimple.js

I am trying to create the simplest of scatter charts using dimple and rCharts. I am curious if there is something I mis-understand about the 'scatter' type. When I run this code, the y-axis values are off by factors of ten or larger--almost as if the scatter chart is acting as a stacked bar chart instead of a simple scatter plot. The sample data below mimics my data exactly.
testdat1 <- data.frame(Recommend = sample(60:90, 200, replace = T), Quiet = sample(20:60, 200, replace = T),
Owner = as.factor(rep(c(1,2), 100)))
summary(testdat1) # no values exceed 90
dtest <- dPlot(Recommend ~ Quiet, groups = 'Owner', data = testdat1, type = 'scatter')
dtest # plotted y-values reach upwards of 450
Any thoughts?
See comment but answer might be accomplished through this block of code:
require(rCharts)
testdat1 <- data.frame(Recommend = sample(60:90, 200, replace = T), Quiet = sample(20:60, 200, replace = T),
Owner = as.factor(rep(c(1,2), 100)))
summary(testdat1) # no values exceed 90
dtest <- dPlot(Recommend ~ Quiet, groups = 'Owner', data = testdat1, type = 'bubble')
#will aggregate as avg by default
dtest$xAxis(type="addMeasureAxis")
dtest
#add x,y, and grouping so now only will aggregate where x,y,and group is exact same
#if still a problem, could a unique id and group on that
dtest$params$groups <- c('Recommend','Quiet','Owner')
dtest # plotted y-values reach upwards of 450

Resources