小编ism*_*gal的帖子

通过单击堆叠条形图打开选项卡

我正在使用 R、ggplot 和 plotly 构建一个包含转推的堆叠条形图。如果单击条形图的一部分,我希望打开一个新的浏览器选项卡,并显示该特定日期的推文以及指定的转推量。但是,当我单击下面示例中的其中一个条时,会打开一个不同的链接,表明 url 未与这些条正确连接。我该如何解决这个问题?

我以前从未工作过,甚至从未见过 JavaScript,所以答案很可能就在那里。该图最终将出现在 Shiny 应用程序中。

library(rtweet)
library(ggplot2)
library(plotly)

# Get tweets
tweets <- get_timeline("BBC", n = 10)

# Create dataframe
data <- data.frame("retweet_count" = tweets$retweet_count,
                   "week" =  c(1,1,1,2,2,3,4,5,5,6),
                   "url"  =  tweets$status_url)

# Create ggplot
ggplot(data = data,
           aes(x = week,
           y = retweet_count,
           label = url)) + 
  geom_bar(stat = 'sum', 
           fill = "darkblue")

# Convert to plotly
p <- ggplotly(
  p,
  tooltip = c("y", 'label'))

# Add URL data to plot
p$x$data[[1]]$customdata <- data$url …
Run Code Online (Sandbox Code Playgroud)

javascript r ggplot2 plotly r-plotly

6
推荐指数
1
解决办法
89
查看次数

ivot_longer 具有非常大的 data.frame,内存有效的方法

我有一个data.frame包含 1100 万行的医院数据。

Columns: ID (chr), outcome (1|0), 20x ICD-10 codes (chr).
Rows: 10.6 million
Run Code Online (Sandbox Code Playgroud)

我希望使数据整洁,以便将诊断代码建模为二进制结果。

我通常会使用pivot_longer或 Base R函数,但由于内存(32GB RAM,运行最新 R x64 的 Windows 服务器),aggregate结果很大,而且我的机器很挣扎。data.frame

我将拆分data.frame和 ,pivot_longer并手动添加列以允许data.frame在之后进行绑定,或者单独对每个拆分进行建模data.frame

有没有一种方法可以用来减少数据大小或实现我所缺少的类似目标?

r large-data tidyr tidyverse

6
推荐指数
1
解决办法
644
查看次数

Draggable interactive bar chart Rshiny

I would love to know if building something like this is possible is RShiny. I have experience with interactive plots/charts using plotly, ggplot and ggplotly but I can't see how to do something like this. I love how the graph engages the user to make a guess and then shows the real data.

If anyone could please point me in the direction of any documentation I will be forever grateful! https://www.mathematica-mpr.com/dataviz/race-to-the-top

在此处输入图片说明

r shiny

5
推荐指数
1
解决办法
291
查看次数

更改绘图悬停框 R 的位置

我想更改plotly条形图中悬停框的位置。我希望它在栏上方弹出,而不是在左侧或右侧弹出。这可能吗?我一直在谷歌上搜索这个并盯着 Plotly R 参考页面几个小时,但没有运气。

这是一个示例:

library(dplyr)
library(plotly)
data.frame(x = 1:10, y = 1:10*10) %>% 
  plotly::plot_ly(data = .) %>%
  plotly::add_trace(x = ~x, 
                    y = ~y, 
                    type = 'bar', 
                    hoverinfo = "text", 
                    text = ~y)
Run Code Online (Sandbox Code Playgroud)

r plotly r-plotly

5
推荐指数
0
解决办法
747
查看次数

分别用R中的线连接钢筋

我正在尝试用线条连接堆积的酒吧。

期望值:

在此处输入图片说明

但是,我无法在条之间画线。尝试使用以下脚本,但未添加行。

使用add_trace而不是'add_lines'无效。

df = data.frame(Aria = 20:25, Acqua = 21:26, Fuoco = 22:27, 
                Terra = 23:28, Cielo = 24:29, 
                Labels = c( 'Antonio', 'Maria', 'Giovanni',
                            'Sergio', 'Giorgio', 'Michele' ) )

evo_bar_plot_variant = function(plot_data, var_x, x_name = 'X axis', 
                                y_name = 'Y axis', ... ){
  df = data.frame(plot_data) 
  df = na.omit(df)
  var = quos(...) 
  names_vars = names( var )
  y_vars = names_vars[ startsWith( names_vars, 'var_y' ) ]
  y_var_names = sapply(1:length(y_vars), function(j){ 
                          quo_name(var[[y_vars[j]]] )})
  row_sum = df %>% …
Run Code Online (Sandbox Code Playgroud)

r plotly r-plotly

5
推荐指数
1
解决办法
115
查看次数

将鼠标悬停在闪亮的App的R图表中

这里有人在将鼠标悬停在地块或任何可以这样做的包装上时显示图像的示例吗?我已经尝试了一些东西,但它只会显示url而不会显示图像。我知道这段代码只是封装了URL。如何建立一个div以显示图像。

library(shiny)
library(shinydashboard)
library(DT)
library(dplyr)
library(plotly)

# Data ------------------------------------------------------------------
dt <- data.frame(fruits = c("apple","banana","oranges"),
  rank = c(11, 22, 33), 
  image_url = c(
    'https://images.unsplash.com/photo-1521671413015-ce2b0103c8c7?ixlib=rb-0.3.5&s=45547f67f01ffdcad0e33c8417b840a9&auto=format&fit=crop&w=667&q=80',             
    "https://images.unsplash.com/photo-1520699697851-3dc68aa3a474?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=ef15aee8bcb3f5928e5b31347adb6173&auto=format&fit=crop&w=400&q=80",                        
    "https://images.unsplash.com/photo-1501925873391-c3cd73416c5b?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=379e4a0fffc6d11cd5794806681d0211&auto=format&fit=crop&w=750&q=80"
))

# img_dt <- dt %>%
#   mutate(img = paste0("<a target='_blank' href='", image_url, "'><img src=\'", image_url, "' height='40'></img></a>")) %>%
#   mutate(link = paste0("<a href='", image_url,"' target='_blank'>","View photo","</a>")) 

# Dashboard ----------------------------------------------------------------
ui <- dashboardPage(
  dashboardHeader(title = "Test"),

  dashboardSidebar(),

  dashboardBody(
    tags$head(
      tags$style(
        HTML(
          "img.small-img {
          max-width: 75px;
          }")
      )
    ),

    plotlyOutput("hoverplot")
  )
)

server <- function(input, output) …
Run Code Online (Sandbox Code Playgroud)

r shiny plotly shinyapps r-plotly

5
推荐指数
1
解决办法
149
查看次数

删除shinydashboardPlus中右侧栏宽度改变时出现的多余空间

我正在使用shinydashboardPlus并希望更改右侧边栏的宽度,我知道可以通过width在函数调用中指定参数来完成rightSidebar,但是当我这样做时(按照从这里获取的下面的示例),冗余空间出现在右侧菜单(请参阅下面屏幕截图中右侧菜单旁边的深灰色列/空间)。

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyWidgets)

data(iris)
mychoices <- c("pick me A", 
               "pick me - a very long name here", 
               "no pick me - B", 
               "another one that is long")

## my css
CSS <- function(colors){
  template <- "
.checkboxGroupButtons div.btn-group:nth-child(%s) button {
  background: %s !important;
  color: black !important;
  padding: 5px;
  margin-bottom: 8px
}"
  paste0(
    apply(cbind(seq_along(colors), colors), 1, function(vc){
      sprintf(template, vc[1], vc[2])
    }),
    collapse = "\n"
  )
}
cols <- c("red", "blue", "yellow", …
Run Code Online (Sandbox Code Playgroud)

css r shiny shinydashboard

5
推荐指数
1
解决办法
445
查看次数

R:当 tidyverse 动词不起作用时,串扰 :: SharedData 链接数据具有不同的格式(宽/长)

编辑TL;DR 使用crosstalk包,我正在寻找一种方法来链接利用长格式数据(线图)的图形与宽格式数据的交互式表格,以便表格中的每一行对应于图中的一条线。

我正在尝试将 DT 表与绘图图链接起来。我的麻烦在于图形需要长格式的数据,而表格需要宽格式。我可能专注于 tidyverse 的做事方式。我将尝试提供一个最小的示例,说明我正在尝试做什么以及我想获得什么。

设置:

library(tidyverse)
library(crosstalk)
library(plotly)
library(DT)

# Wide format
df_test1 <- data.frame(
  id = c("id1", "id2"),
  item1 = c(0, 4),
  item2 = c(3, 2),
  item3 = c(1, 4),
  item4 = c(3, 4),
  item5 = c(1, NA)
)

# Reshaped to long format
df_test2 <- 
  df_test1 %>%
  tidyr::pivot_longer(cols = item1:item5, names_to = "item", values_to = "value") %>%
  dplyr::mutate(item = as.factor(item)) %>%
  dplyr::mutate(value = factor(as.character(value), levels = c("0", "1", "2", "3", "4")))
Run Code Online (Sandbox Code Playgroud)

我试过的: …

r plotly dt tidyverse crosstalk

5
推荐指数
1
解决办法
249
查看次数

通过绘图下拉菜单切换显示的轨迹

我正在使用 R 编程语言。我正在尝试在此处为我自己的数据复制本教程:https : //plotly.com/r/dropdowns/

我创建了一些假数据并绘制了 4 个图:

#load libraries 

library(plotly)
library(MASS)
library(dplyr)


# create data

x <- sample( LETTERS[1:4], 731, replace=TRUE, prob=c(0.25, 0.25, 0.25, 0.25) )
y <- rnorm(731,10,10)
z <- rnorm(731,5,5)
date= seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")
    
    df <- data.frame(x,y, z, date)
df$x = as.factor(df$x)



# plot 1 : time series

 aggregate = df %>%
        mutate(date = as.Date(date)) %>%
        group_by(month = format(date, "%Y-%m")) %>%
        summarise( mean = mean(y))

ts_1 <- ggplot(aggregate) + geom_line(aes(x = month, y = mean, group = …
Run Code Online (Sandbox Code Playgroud)

r onclick dplyr plotly r-plotly

5
推荐指数
1
解决办法
863
查看次数

如何更改 R 中 ggplotly 中的图例位置

下面的代码使用ggplot和生成两个图ggplotly。尽管使用了layout()ggplotly,图例仍然位于右侧。图例必须位于底部。任何人都可以帮助将图例移动到 ggplotly 的底部吗?我已经尝试了 R +shiny+plotly 的解决方案:ggplotly 将图例移至右侧,但在这里不起作用。如果我错过了显而易见的事情,有人可以帮忙吗?

measure<-c("MSAT","MSAT","GPA","MSAT","MSAT","GPA","GPA","GPA")
score<-c(500, 490, 2.9, 759, 550, 1.2, 3.1, 3.2)
data<-data.frame(measure,score)


ui <- fluidPage(
  mainPanel(
    plotOutput("myplot" ),
    plotlyOutput("myplot2" )
  )
)

server <- function(input, output) {
  myplot <- reactive({
    gpl1 <- ggplot(data,aes(y=reorder(measure, score),x=score,fill=score)) +
      geom_bar(stat="identity")+
      theme(legend.position="bottom")+
      xlab("x")+
      ylab("y")+
      labs(title = NULL)
    gpl1
  })
  
  myplot2 <- reactive({
    gpl2 <- ggplot(data,aes(y=reorder(measure, score),x=score,fill=score)) +
      geom_bar(stat="identity") +
      theme(legend.position="bottom")+
      xlab("x")+
      ylab("y")+
      labs(title = NULL)
    ggplotly(gpl2) %>% 
      layout(legend = list(orientation = 'h', …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 ggplotly r-plotly

5
推荐指数
1
解决办法
3637
查看次数