plotly

Embedding plotly graphs in a Rmarkdown document using source(“filename.R”)

家住魔仙堡 提交于 2019-12-10 22:58:48
问题 I am creating a RMarkdown HTML document where chunks are sourcing R files: ```{r } source("test.R") ``` Where test.R is: library(ggplot2) library(plotly) data <- as.data.frame(datasets::mtcars) create_plot <- function(data, var_x, var_y, var_color, var_size) { data %>% ggplot(aes_string( x = var_x, y = var_y, color = var_color, size = var_size)) + geom_point() } p <- create_plot(data, "disp", "qsec", "vs", "hp") p <- ggplotly(p) print(p) This works inline the Rmarkdown document (RStudio) but

R remove marked rectangle after plotly selection reset

走远了吗. 提交于 2019-12-10 22:43:28
问题 I use @shosaco solution from here to reset selection in plotly: library(shiny) library(plotly) library(shinyjs) library(V8) ui <- shinyUI( fluidPage( useShinyjs(), extendShinyjs(text = "shinyjs.resetClick = function() { Shiny.onInputChange('.clientValue-plotly_selected-A', 'null'); }"), actionButton("reset", "Reset plotly click value"), plotlyOutput("plot"), verbatimTextOutput("clickevent") ) ) server <- shinyServer(function(input, output) { output$plot <- renderPlotly({ plot_ly(mtcars, x=

Use conditional coloring on a plotly surface

烈酒焚心 提交于 2019-12-10 21:53:26
问题 I am using plotly via R for the first time and trying to create a surface from a grid and color it based on a calculation. For example, I would like to use the surface from data(volcano) , as in library(plotly) plot_ly(z = ~volcano) %>% add_surface() But instead of color based on the z-value (altitude), let's just say I wanted to color based on distance from my house on the little mesa at (20,60) . house_loc <- c(20,60,150) # (x,y,z) of my house dist_to_house <- Vectorize(function(x,y,z){sqrt

Color a specific bar in histogram using python

不想你离开。 提交于 2019-12-10 21:44:33
问题 I have a histogram (matplotlib or plotly) and want to color a specific bar, witch has value N in the bar range (for example if N=131 the colored bar must be 130-132). How can i do that? 回答1: When calling plt.hist() , it will return three things. Firstly an array holding the value in each bin. Secondly the values for each of the bins, and lastly an array of patches . These let you modify each bar individually. So all you need to do is determine which bin is for the range 130-132 and then

Plotly subplot with two ggplots and common legend?

喜欢而已 提交于 2019-12-10 19:59:51
问题 I'm trying to arrange two ggplot object converted to a plotly object and use one common legend. But the legend is somehow doubled: df1 <- read.table(text = "group x y group1 -0.212201 0.358867 group2 -0.279756 -0.126194 group3 0.186860 -0.203273 group4 0.417117 -0.002592 group1 -0.212201 0.358867 group2 -0.279756 -0.126194 group3 0.186860 -0.203273 group4 0.186860 -0.203273", header = TRUE) df2 <- read.table(text = "group x y group1 0.211826 -0.306214 group2 -0.072626 0.104988 group3 -0

Is there any way to retrieve a local variable from a running function?

旧巷老猫 提交于 2019-12-10 18:31:15
问题 Desperate. Say we have the following: def main(): ALotOFCode list1 = [] list2 = [] while condition: # a lot of times where raw_input is used in this loop # e.g. x = raw_input('lots of steps to compute x') y = raw_input('lots of steps to compute y') list1 = list1.append(x) list2 = list2.append(y) stream.write({'x':list1,'y':list2}) #send new data point to plot.ly via raspberry pi I don't know what happened. But my plot in plot.ly is gone. Deleted completely. I was messing with what I had on

Control which tick marks / labels appear on x-axis in plotly?

北城以北 提交于 2019-12-10 18:25:12
问题 I want to have control over which tick marks appear on the x-axis. The following code places tick marks in a sequence of 5 (at 5, 10, 15 ... 30) library(plotly) df <- data.frame(x = 1:30, y = sample(100:300, size = 30, replace = T)) p <- plot_ly(data = df, x = x, y = y, type = 'line') %>% layout(title = 'Example plot') p I need to place them in a sequence of 6 at 6, 12, 18, 24, 30. I've been browsing the documentation but I cannot seem to find what I need. In ggplot2 this can be done via

Update content on server only after I click action button in Shiny

无人久伴 提交于 2019-12-10 17:55:07
问题 I'm having problems with reactive output in shiny server in R. What I want to do is create a plotly plot that uses values calculated based on input. The output has to be updated only after I click submit/done button and the output has to be calculated as many times as needed. What I managed to do was to update the content independently of the submit button (the submit button simply has no function) and the plot changed immediately after I changed 1 value. What I would like to do is to change

Stacked bar graphs in plotly: how to control the order of bars in each stack

蓝咒 提交于 2019-12-10 17:35:50
问题 I'm trying to order a stacked bar chart in plotly, but it is not respecting the order I pass it in the data frame. It is best shown using some mock data: library(dplyr) library(plotly) cars <- sapply(strsplit(rownames(mtcars), split = " "), "[", i = 1) dat <- mtcars dat <- cbind(dat, cars, stringsAsFactors = FALSE) dat <- dat %>% mutate(carb = factor(carb)) %>% distinct(cars, carb) %>% select(cars, carb, mpg) %>% arrange(carb, desc(mpg)) plot_ly(dat) %>% add_trace(data = dat, type = "bar", x

Plotly: Bar and pie charts side by side

谁说胖子不能爱 提交于 2019-12-10 17:25:47
问题 I would like to plot a bar and a pie chart side by side using the subplot function in the plotly package in R. However, a big pie chart is plotted in the center of the graph, overlaying the bar plot. Here is a sample code: Animals <- c("giraffes", "orangutans", "monkeys") SF_Zoo <- c(20, 14, 23) LA_Zoo <- c(12, 18, 29) data <- data.frame(Animals, SF_Zoo, LA_Zoo) bar <- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar') %>% layout(yaxis = list(title = 'Count'), barmode = 'stack') pie <-