I am relatively new to R and am attempting to use gganimate to plot the occurrences of a given phenomenon on a map of the United States. I have a column frameid which is calculated by multiplying a column week by 100 and adding the nth occurrence of said phenomenon. So the 7th occurrence of a phenomenon in New York in the first week of the dataset would have a frameid of 107, and the 7th occurrence of a phenomenon in Los Angeles would also have a frameid of 107. I am passing frameid into transition_states, but the problem is that I only want to display the week (i.e. everything but the last two characters, as the occurrences could never conceivably exceed 99), so I need to manipulate frameid somehow. The attempted solution:
labs(title = "My Title",
subtitle = paste("2019, Week",
substring("{closest_state}", 1, nchar("{closest_state}")-2)))
Returns 50 warnings, the first of which is "1: Cannot get dimensions of plot table. Plot region might not be fixed" and the last 49 of which are "Expecting '}'".
I have also tried:
gsub('.{3}$', '', "{closest_state}")
Which returns a similar error. Is there a solution or a workaround to this problem or can "{closest_state}" not be manipulated?
Here is the reproducible code:
library(ggplot2)
library(gganimate)
library(fiftystater)
sample <- data.frame(
frameid = c(101, 101, 101, 102, 102, 102, 201, 201, 201),
latitude = c(38.02262, 38.99691, 41.31194, 27.00071,
28.539, 30.2836, 38.02262, 38.03112,
40.21603),
longitude = c(-84.50521, -104.84369, -105.56906, -108.4121,
-81.4028, -97.73234, -84.50521, -78.51371,
-85.4177)
)
fortynine_states <- fifty_states %>%
filter(id != "alaska")
my_plot <- sample %>%
ggplot(aes(x = longitude, y = latitude)) +
geom_polygon(data = fortynine_states,
mapping = aes(long, lat, group = group),
fill = "white", color = "black") +
geom_point(aes(alpha = 0.2, color = "red")) +
coord_map() +
labs(title = "My Title",
subtitle = paste("2019, Week",
substring("{closest_state}", 1, nchar("{closest_state}")-2))) +
transition_states(frameid, transition_length = 2) +
exit_fade()
animate(my_plot, duration = 5, fps = 20, width = 400, height = 300, renderer = gifski_renderer())
Related
I have made a pbiviz custom visual using developer tools of Normal distribution curve over a Histogram plot with R - ggplot2 and plotly libraries in a pbiviz.package
The visual works fine. Now I want to add interactivity of the Histogram with other Power BI visuals.
i.e. If user clicks on a bar of the Histogram, it should filter out a Table on my PBI report with rows relevant to the histogram bar data.
Considering the limitations of using R script with Power BI, I do not know if it is possible with my current visual as I am new to scripting.
Is there a better way (Typescript, JS, Python, etc.) other than what I have done to make an Interactive Histogram & Distribution Curve in Power BI?
This is the R script along with sample data and Visual Image
Histogram represents the projects falling in different durations
There are two bell curves - One for closed projects and Other for Active Projects
source('./r_files/flatten_HTML.r')
############### Library Declarations ###############
libraryRequireInstall("ggplot2");
libraryRequireInstall("plotly");
libraryRequireInstall("tidyverse");
libraryRequireInstall("scales");
libraryRequireInstall("htmlwidgets");
library(ggplot2)
library(tidyverse)
library(scales)
library(plotly)
theme_set(theme_bw())
##### Making DataSet for All Selected Projects #####
Duration <- dataset$Duration
Status <- (rep(dataset$ProjectStatus))
da <- data.frame(Duration,Status)
lenx <- length(Duration)
meanall <- mean(da$Duration)
sdx <- sd(da$Duration)
binwidth <- 30
font_label <- list(family = "Segoe UI", size = 21, colour = "black")
hovlabel <- list(bordercolor = "black", font = font_label)
#Filtering Out Closed Projects from Dataset
#Creating Data Frame for Closed Projects
closedproj <- dataset %>%
select(Duration,ProjectStatus) %>%
filter(ProjectStatus == "Closed")
closed <- closedproj$Duration
df <- data.frame(closed)
xclosed <- closedproj$
df2 <- data.frame(xclosed)
lenc <- length(xclosed)
mean_closed <- mean(df2$xclosed)
sdc <- sd(df2$xclosed)
a <-
(ggplot(da,aes(x=Duration, fill = Status, text = paste("Duration: ",x,"-", x + binwidth,"<br />Project Count", ..count..)))+
#Histogram
geom_histogram(aes(y=..count..),alpha=0.5, position='identity',binwidth = binwidth)+
# #Distribution Curve
annotate(
geom = "line",
x = da$Duration,
y = dnorm(da$Duration, mean = meanall, sd = sdx) * lenx * binwidth,
width = 3,
color = "red"
) +
annotate(
geom = "line",
x = df2$xclosed,
y = dnorm(df2$xclosed, mean = mean_closed, sd = sdc)* lenc * binwidth,
width = 3,
color = "blue"
) +
labs(
x = "Project Duration (Days)",
y = "Project_Count",
fill = "Project Status")+
#Mean
geom_vline(aes(xintercept=meanall),color="red",linetype="dashed",size = 0.8,label=paste("Mean :",round(meanall,0)))+
geom_vline(aes(xintercept=mean_closed),color="blue",linetype="dashed",size = 0.8,label=paste("Mean (Closed):",round(mean_closed,0)))+
# 1 Sigma
geom_vline(aes(xintercept = (meanall + sdx)), color = "red", size = 1, linetype = "dashed") +
geom_vline(aes(xintercept = (meanall - sdx)), color = "red", size = 1, linetype = "dashed")+
geom_vline(aes(xintercept = (mean_closed + sdc)), color = "blue", size = 1, linetype = "dashed") +
geom_vline(aes(xintercept = (mean_closed - sdc)), color = "blue", size = 1, linetype = "dashed")+
# Theme
theme(
plot.background = element_rect(fill = "transparent"),
legend.background = element_rect(fill = "lightgray"),
axis.title.x = element_text(colour = "Black",size = 18,face = "bold"),
axis.title.y = element_text(colour = "Black",size = 18,face = "bold"),
axis.text.x = element_text(colour = "Black",size = 15),
axis.text.y = element_text(colour = "Black",size = 15),
panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
scale_x_continuous(labels = comma,
breaks = seq(0, max(Duration),50)) +
scale_y_continuous(labels = comma,
breaks = seq(0,max(Duration),10)))
############# Create and save widget ###############
p = ggplotly(a, tooltip = c("text")) %>%
style(hoverlabel = hovlabel) %>%
layout(legend = list(
orientation = "h",
x = 0,
y = 1.13,
title = list(text = "Project Status",font = list(family = "Segoe UI", size = 23)),
font = font_label
),
yaxis = list(title = list(standoff = 25L)),
xaxis = list(title = list(standoff = 25L)),
annotations = list(showarrow=FALSE,align = "left",valign = "top",x = 0.95, xref = "paper",yref = "paper",y = 0.955,
font = list(family = "Segoe UI", size = 22, color = "#cc0000"),
text = paste("Max Duration: ", comma(round(max(da$Duration),0)),
"<br>Mean (Closed): ", comma(round(mean_closed,0)),
"<br>Mean (All) : ", comma(round(meanall,0))))
) %>%
config(modeBarButtonsToRemove = c("select2d","hoverClosestCartesian", "lasso2d","hoverCompareCartesian","toggleSpikelines"), displaylogo = FALSE);
internalSaveWidget(p, 'out.html');
}
####################################################
################ Reduce paddings ###################
ReadFullFileReplaceString('out.html', 'out.html', ',"padding":[0-5]*,', ',"padding":0,')
What I expect is -- If user clicks on a bar of the Histogram, it should reflect on a Table visual on my PBI report with rows relevant to the histogram bar data.
Any help will be highly appreciated !
Regards
I am trying to create a table using rhandsontable with several rows of merged cells (different cells in each row).
I am trying to achieve merging of the indicated cells in the screenshot below ...
I am able to successfully merge the first set of cells (row 11) but subsequent merges using a "list of lists" to specify the cells doesn't work. I have tried every permutation of the "list of lists" syntax that I can think of;
a reprex of the example is here ...
library(shiny)
library(rhandsontable)
## Create the data set
DF = data.frame((Cycle = 1:13),
`A` = as.numeric(""),
`B` = as.numeric(""),
`C` = as.numeric(""),
`D` = as.numeric(""))
DF[11,1] = "Total"
DF[12,1] = ""
DF[13,1] = "Loss"
server <- shinyServer(function(input, output, session) {
output$hotTable <- renderRHandsontable({rhandsontable(DF,
width = 1500, height = 350, rowHeaders = FALSE) %>%
hot_cols(colWidths = c(100, 150, 150, 150, 150)) %>%
hot_col(c(1,3:5), readOnly = TRUE) %>%
hot_col(col = c(1:5), halign = "htCenter") %>%
hot_col(col = c(2:4), format = "0.0000") %>%
hot_col(col = 5, format = 0000) %>%
hot_table(mergeCells = list(list(row = 10, col = 2, rowspan = 1, colspan = 3),
list(row = 11, col = 1, rowspan = 1, colspan = 5),
list(row = 12, col = 3, rowspan = 1, colspan = 3)))})
})
ui <- basicPage(mainPanel(rHandsontableOutput("hotTable")))
shinyApp(ui, server)
I think you should change some parameters as follow:
mergeCells = list(list(row = 10, col = 2, rowspan = 1, colspan = 3),
list(row = 11, col = 0, rowspan = 1, colspan = 5),
list(row = 12, col = 2, rowspan = 1, colspan = 3))
so your desired rhandsontable would be produced as you want:
I have another question in the word2vec universe.
I am using the 'sparklyr'-package. Within this package I call the ft_word2vec() function. I have some trouble understanding the output:
For each number of sentences/paragraphs I am providing to the ft_word2vec() function, I always get the same amount of vectors. Even, if I have more sentences/paragraphs than words. For me, that looks like I get the paragraph-vectors. Maybe a Code-example helps to understand my problem?
# add your spark_connection here as 'spark_connection = '
# create example data frame
FK_data = data.frame(sentences = c("This is my first sentence",
"It is followed by the second sentence",
"At the end there is the last sentence"))
# move the data to spark
sc_FK_data <- copy_to(spark_connection, FK_data, name = "FK_data", overwrite = TRUE)
# prepare data for ft_word2vec (sentences have to be tokenized [=list of words instead of one string in each row])
sc_FK_data <- ft_tokenizer(sc_FK_data, input_col = "icd_long", output_col = "tokens")
# split data into test and trainings sets
partitions <- sc_FK_data %>%
sdf_random_split(training = 0.7, test = 0.3, seed = 123456)
FK_train <- partitions$training
FK_test <- partitions$test
# given a trainings data set (FK_train) with a column "tokens" (for each row = a list of strings)
mymodel = ft_word2vec(
FK_train,
input_col = "tokens",
output_col = "word2vec",
vector_size = 15,
min_count = 1,
max_sentence_length = 4444,
num_partitions = 1,
step_size = 0.1,
max_iter = 10,
seed = 123456,
uid = random_string("word2vec_"))
# I tried to get the data from spark with:
myemb = mymodel %>% sparklyr::collect()
Has somebody had similar experiences? Can someone explain what exactly the ft_word2vec() function returns? Do you have an example on how to get the word embedding vectors with this function? Or does the returned column indeed contain the paragraph vectors?
my colleague found a solution! If you know how to do it, the instructions really begin to make sense!
# add your spark_connection here as 'spark_connection = '
# create example data frame
FK_data = data.frame(sentences = c("This is my first sentence",
"It is followed by the second sentence",
"At the end there is the last sentence"))
# move the data to spark
sc_FK_data <- copy_to(spark_connection, FK_data, name = "FK_data", overwrite = TRUE)
# prepare data for ft_word2vec (sentences have to be tokenized [=list of words instead of one string in each row])
sc_FK_data <- ft_tokenizer(sc_FK_data, input_col = "icd_long", output_col = "tokens")
# split data into test and trainings sets
partitions <- sc_FK_data %>%
sdf_random_split(training = 0.7, test = 0.3, seed = 123456)
FK_train <- partitions$training
FK_test <- partitions$test
# CHANGES FOLLOW HERE:
# We have to use the spark connection instead of the data. For me this was the confusing part, since i thought no data -> no model.
# maybe we can think of this step as an initialization
mymodel = ft_word2vec(
spark_connection,
input_col = "tokens",
output_col = "word2vec",
vector_size = 15,
min_count = 1,
max_sentence_length = 4444,
num_partitions = 1,
step_size = 0.1,
max_iter = 10,
seed = 123456,
uid = random_string("word2vec_"))
# now that we have our model initialized, we add the word-embeddings to the model
w2v_model = ml_fit(w2v_model, sc_FK_EMB)
# now we can collect the embedding vectors
emb = word2vecmodel$vectors %>% collect()
I am creating a Shiny app where I would like to have the default of a numericInput be dependent on another default to a previously defined numericInput.
e.g.,
Here I would like the numericInput elements of (2) to be the reciprocal of (1), without having to specify values for value,min,max, and step beforehand:
(1) numericInput("obs1", "Label1", value = 10, min = 10, max = 20, step = 1)
(2) numericInput("obs2", "Label2", value = 1/10, min = 1/10, max = 1/20, step = 1)
Above (1) is the previously-defined numericInput.
Is there a simple way to do this?
If you want an input object to have dynamic parameters you need to use uiOutput, so that you can generate them in runtime (in server.R).
Example: In the first column you can set min, max and value. Modifying any of them renders obs1 and obs2 with new parameter values.
library(shiny)
ui <- fluidPage(
column(6,
tags$h2("Set parameters"),
numericInput("value", "Value", value = 20, min = 10, max = 60, step = 10),
numericInput("min", "Min", value = 10, min = 0, max = 30, step = 10),
numericInput("max", "Max", value = 40, min = 40, max = 60, step = 10)
),
column(6,
uiOutput("ui")
)
)
server <- function(input, output, session) {
output$ui <- renderUI( {
tagList(
tags$h2("Numeric inputs that depend on reactive data"),
numericInput("obs1", "Label1", value = input$value, min = input$min, max = input$max, step = 1),
numericInput("obs2", "Label2", value = input$value + 5, min = input$min - 5, max = input$max + 5, step = 1)
)
})
}
shinyApp(ui, server)
Please note that you need to wrap elements in tagList, when you want to pass more than one input element to renderUI.
In the Shiny dashboard tutorial of the Wikimedia foundation a screenshot is shown with a kind of horizontal stacked bar (the one with red, green, and blue "Full-text...OpenSearch..Prefix):
I have been searching everywhere, but I cannot find out how to create a bar like this. Can anyone point me in the right direction?
This is not a great answer, but it works. Requires learning some ggplot2 if you want to tweak it, and I tried to get rid of the border around the edges but it isn't gone completely. Still, the basic idea is here.
library(ggplot2)
mydf <- data.frame(labels = c('This', 'that', 'the other'),
percents = c(0.31, 0.15, 0.54))
mydf$pos <- pmax(0, cumsum(mydf$percents) - (0.5 * mydf$percents))
p <- ggplot(mydf, aes(x = NA, y = percents)) +
geom_bar(stat = 'identity', aes(fill = percents)) +
geom_text(color = 'white', aes(label = labels, y = pos)) +
coord_flip() +
guides(fill = FALSE) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0)) +
theme_void()
png('this_plot.png', width = 800, height = 30)
p
dev.off()