I understand using dySeries function from the R dygraphs package can customize the overall legend text. However, I wonder how to create customized legend text in dygraphs (or other tricks to reach the goal). As an example:
library(dygraphs)
data <- data.frame(game = c(1:5),
oppnt = c("aaa", "bbb", "ccc", "ddd", "eee"),
score = c(10, 20, 15, 12, 4))
data %>% select(game, score) %>% dygraph() %>%
dySeries("score", label = "X's score")
This will show X's score: (interactive score) when mouseover each data point. How can I show information like Opponent XXX, X's score: (interactive score). For example: "Opponent ccc, X's score: 15"? Thanks.
Related
I am making a gt table showing the progress of individuals towards a goal. In the table, there is a row showing a horizontal bar graph of progress towards that goal (if goal is 50 and score is 40, the bar is at 80%).
However, when I change the order of the gt rows by using the groupname_col argument, the order of the other cells changes, but not the order of the gtExtras gt_plt_bar_pct column, so it's showing the wrong bars for the name and score in that row, instead, that column seems to always be represented in the order of rows in the input data.
I understand that I can fix this by using arrange on the df before the gt begins, but this doesn't seem like a good solution since I'm going to want to change the order of the rows to view by different groups. Is this a flaw with gtExtras? is there a better fix?
thanks!
reprex:
library(tibble)
library(gt)
library(gtExtras)
library(dplyr)
# make dataframe of individuals and their goals
df <- tribble(
~name, ~group, ~score, ~goal,
"Bob", "C", 20, 40,
"Chris", "A", 50, 40,
"Dale", "B", 30, 50,
"Jay", "A", 0, 40,
"Ben", "B", 10, 20
) %>%
# calculate percent towards goal, and cap at 100%
mutate(percent_to_goal = score/goal *100,
percent_to_goal = case_when(percent_to_goal >= 100 ~ 100,
TRUE ~ percent_to_goal))
df %>%
# this fixes the issue, but doesn't seem like a permanent solution
#arrange(group, name) %>%
# make gt table
gt(rowname_col = "name", groupname_col = "group") %>%
# order groups
row_group_order(groups = c("A","B","C")) %>%
# add bar chart column
gt_plt_bar_pct(column = percent_to_goal) %>%
# highlight blue if person reaches their goal
tab_style(
style = list(
cell_fill(color = "lightcyan"),
cell_text(weight = "bold")),
locations = cells_body(
columns = c(goal,score, percent_to_goal),
rows = score >= goal
)
)
Here is the output from the above code: notice that the length of the bar charts do not always reflect the values of the rows they are appearing in. Instead, they reflect the order of the original dataset.
EDIT: remove row_group_order. If I run the above code again, but comment out the line meant to rearrange the appearance of groups, the grouping shows up in a different order (order of appearance of groups in the original dataset), and the name and first two columns sort into these groups accordingly, but the bar chart column still does not, and remains in the original order of the dataset. Image below:
Per gtExtras v 0.2.4 this bug has been fixed. Thanks for raising and the great reprex!
library(tibble)
library(gt)
library(gtExtras)
library(dplyr)
# make dataframe of individuals and their goals
df <- tribble(
~name, ~group, ~score, ~goal,
"Bob", "C", 20, 40,
"Chris", "A", 50, 40,
"Dale", "B", 30, 50,
"Jay", "A", 0, 40,
"Ben", "B", 10, 20
) %>%
# calculate percent towards goal, and cap at 100%
mutate(percent_to_goal = score/goal *100,
percent_to_goal = case_when(percent_to_goal >= 100 ~ 100,
TRUE ~ percent_to_goal))
df %>%
# make gt table
gt(rowname_col = "name", groupname_col = "group") %>%
# order groups
row_group_order(groups = c("A","B","C")) %>%
# add bar chart column
gt_plt_bar_pct(column = percent_to_goal) %>%
# highlight blue if person reaches their goal
tab_style(
style = list(
cell_fill(color = "lightcyan"),
cell_text(weight = "bold")),
locations = cells_body(
columns = c(goal,score, percent_to_goal),
rows = score >= goal
)
)
I have a Highcharter stacked column chart here and i want to sort the grouping (category) according to the order in the dataset (Z, E, A).
But Highcharter sorts the segments alphabetically (A, E, Z).
Is there any way to sort the segments from Z->A?
Many thanks for any help.
install.packages("highcharter")
library(highcharter)
# data frame
city <- c("New York","New York","New York","Boston","Boston","Boston","Washington","Washington","Washington", "Seattle","Seattle","Seattle", "Houston", "Houston", "Houston")
value <- c(1000,2500,2600,1900,1800,500,4900, 2000,3000,4000,5000,1500,1300,1400,1850)
category <- c("Z", "E", "A","Z", "E", "A","Z", "E", "A","Z", "E", "A","Z", "E", "A")
data <- data_frame(city, value, category)
#View(data)
# higcharter stacked column
hc <- data %>%
hchart('column', hcaes(x= city, y = value, group = category)) %>%
hc_plotOptions(series = list(stacking='normal'))%>%
hc_title(text='Stacked Chart',style = list(fontWeight = "", fontSize = "15px"))
hc
There are no such options from the default, but I think it could be created using the custom logic in formatter function available from the Highcharts API: https://api.highcharts.com/highcharts/xAxis.labels.formatter
Here you can find an article showing how to work with Highcharts JavaScript syntax in R: https://www.highcharts.com/blog/tutorials/working-with-highcharts-javascript-syntax-in-r/?fbclid=IwAR39-dztCuBUrncLQHEo9yNin00lRZfSqKY1JqAdvMU7KkokZGfTfj5DYB4
I'm creating heatmaps in R using heatmap.2 (I think it needs to be heatmap.2 because I'm using 1 dataset to generate the colours of the heatmap and a second dataset to overlay numerical data).
Here is a sample of my code so far. The actual data set is 30 columns and 1000 rows.
heatmap_all_data <-
data.frame(name = c("John", "Mark", "Luke", "Jack", "Will", "Jim", "Clive", "Steve"),
trait_1 = c(1, 2, 5, 8, 5, 3, 7, 8),
trait_2 = c(5, 7, 3, 4, 6, 3, 2, 1)) %>%
column_to_rownames(var="name")
heatmap_colour <- colorRampPalette(brewer.pal(11, "RdYlBu"))(1000)
heatmap.2(as.matrix(heatmap_all_data),
scale = "column",
key = FALSE,
dendrogram = "none",
Rowv = FALSE,
Colv = FALSE,
trace = "none",
col = rev(heatmap_colour),
labRow = row.names(heatmap_all_data))
Which generates the following heatmap: https://i.stack.imgur.com/lK8Sc.png
NOW, the problem is I only want a subsection of this data, e.g I want the following heatmap:
heatmap_part_data <-
data.frame(name = c("John", "Mark", "Luke"),
trait_1 = c(1, 2, 5),
trait_2 = c(5, 7, 3)) %>%
column_to_rownames(var="name")
heatmap_colour <- colorRampPalette(brewer.pal(11, "RdYlBu"))(1000)
heatmap.2(as.matrix(heatmap_part_data),
scale = "column",
key = FALSE,
dendrogram = "none",
Rowv = FALSE,
Colv = FALSE,
trace = "none",
col = rev(heatmap_colour),
labRow = row.names(heatmap_part_data))
https://i.stack.imgur.com/j33Ic.png
BUT, I want each cell to keep the same colours as the original. I.e. I want the colours in my subsetted heatmap to be relative to the total data and not just the subsetted data. (In the real example I want to show 10 out of 1000 entries).
So, I need to either "zoom in" and rescale the top section of the heatmap and then crop the image, extract the top section of the heatmap into a new object while maintaining the same colours, or extract information about the colours in the full heatmap and overwrite the default colours in the subsetted heatmap.
The goal is basically to output an image of the subsetted data heatmap with each colour in each cell the same as in the all_data heatmap.
I hope this is clear - please advise if you need any clarification!
Many thanks for taking the time to read and I hope someone can help.
Best,
Ryan
Found the solution!
So I switched from heatmap.2 to heatmaply - same functionality but with interactivity. With heatmaply you can drag an area over the heatmap and zoom into that area which gives the desired result but I wanted to consistently zoom to a specific area.
From this website (https://plotly.com/r/axes/) I found out about the Layout function of the wider plotly library (that heatmaply is part of).
So to the existing code you can add:
%>% layout(yaxis = list(range = c(10.5, 0.5)))
(Need to add 0.5 to centre the rows properly)
Et voila! The heatmap colours are generated relative to the wider dataset but only a subset is shown.
I am trying to make a pie chart in R using plotly. I have a tibble (df) with 4 columns - (1) an observation (x), (2) value of the observation (y), (3) category of the observation (cat), and (4) color of each observation (colors). Colors are unique for each category (every observation within the same category will share the same color). I need each segment of the pie chart to represent each observation with the size of the segment corresponding to the value of the observation. I also need the segments to be colored by their unique category color. I have been able to build such a pie chart.
I am, however, struggling with how to customize the legend. Using showlegend=TRUE shows each observation with it's color. I need the legend to showcase each unique category with its distinct color. I would really appreciate it if someone could help me out with this.
Here is some dummy code that models this problem -
# Load packages
library(tidyverse)
library(plotly)
# Initialize variables
df = NULL
x = c("apple", "John", "dog", "lion", "strawberry", "Liz",
"cat", "peach", "banana", "elephant", "pear", "tiger")
y = c(1, 1, 3, 5, 4, 3, 6, 4, 1, 2, 1, 6)
cat = c("fruit", "person", "animal", "animal", "fruit", "person",
"animal", "fruit", "fruit", "animal", "fruit", "animal")
colors = NULL
# Define colors
for (i in 1:length(cat)) {
if (cat[i] == "fruit") {
color = "#FFD700"
} else if (cat[i] == "animal") {
color = "#FB8072"
} else {
color = "#D3D3D3"
}
colors = c(colors, color)
}
# Create data frame
df = as_tibble(cbind(x, y, cat, colors))
# Sort data frame
df = df[order(df$cat, df$x), ]
# Define colors for pie chart
pie_colors = as.character(df$colors)
# Define margins for pie chart
margins = list(l=50, r=50, b=125, t=125)
# Build the pie chart
pie_plt = plot_ly(data=df,
values=~y,
labels=~x,
type="pie",
textinfo="label+percent",
sort=FALSE,
marker=list(colors=pie_colors,
line=list(color="black", width=1))) %>%
layout(autosize=FALSE, margin=margins, showlegend=TRUE)
# Show pie chart:
pie_plt
I want to create a flowchart with the R package DiagrammeR. The text of some of the nodes should have a line break.
Consider the following reproducible example in R:
library("DiagrammeR")
# Create a node data frame (ndf)
ndf <- create_node_df(n = 4,
label = c("hi stacko", "aaa", "bbb", "ccc"))
# Create an edge data frame (edf)
edf <- create_edge_df(from = c(1, 2, 3, 3),
to = c(4, 3, 1, 4))
# Create a graph with the ndf and edf
graph <- create_graph(nodes_df = ndf,
edges_df = edf)
# Create a PDF file for the graph (`graph.pdf`)
graph %>%
render_graph()
In this flowchart, I would like to add a line break between "hi" and "stacko" in the lower left node. I found some sources that suggested <br> or \n. Unfortunately, both did not work.
Question: How could I insert a line break in DiagrammeR?
This works for me:
ndf <- create_node_df(n = 4,label = c("hi\nstacko", "aaa", "bbb", "ccc"))
and, when run with the remainder of the code, produces the following diagram: