简体   繁体   English

如何在 Plotly 中从另一个 Z6A8064B5DF4794555500553C47C55057DZ 中提取的列名中提取 plot a

[英]How to plot a in Plotly from a column name extracted from another dataframe in R

I have two sample dataframes given below我在下面给出了两个示例数据框

library(shiny)
df1 <- data.frame(words = c("bat", "vat","cat","mat"), count = c(3420, 2098, 2003, 1501), words_count = c("bat (3420)","vat (2098)", "cat (2003)", "mat (1501)"), stringsAsFactors = FALSE)

df2 <- data.frame(class = c("A","B","C","D"), bat = c(10,0,30,20), vat = c(0,0,10,30), cat = c(20,30,0,10), mat = c(20,20,0,0), stringsAsFactors = FALSE)

Using the code below I want to extract the column name from drop down menu (made from df1) and use the extracted value to plot the bar graph in plotly using extracted value as column in df2.使用下面的代码,我想从下拉菜单中提取列名(由 df1 制成)并将提取的值用于 plot 中的条形图 plotly 使用提取的值作为 df2 中的列。 I am doing this is Rmd.我正在做的是 Rmd。

inputPanel(
  selectInput("words", label = "Choose the word",
              choices = df1$words_count, 
              selected = df1$words_count[1], width = '100%')
  )

renderPlot({
# Extract the word from drop down
  col_word <- df1 %>% filter(words_count == input$words) %>% pull(words)

  plot_ly(df2, x = ~category, y = ~col_word, type = 'bar') %>%
    layout(yaxis = list(title = 'Count'), xaxis = list(title = ""))
})

I get no output.我没有得到 output。 I have tried a couple of things like get(col_word) however, it does not work.我尝试了一些类似 get(col_word) 的方法,但是它不起作用。

The result col_word is a character value (eg, "bat").结果col_word是一个字符值(例如,“bat”)。

For plot_ly , you need a data frame column, or list or vector, not a character string.对于plot_ly ,您需要一个数据框列、列表或向量,而不是字符串。

You can get around this in ggplot using aes_string .您可以使用aes_stringggplot中解决这个问题。 However, in plotly you need an alternative --- you can try:但是,在plotly中,您需要一个替代方案 --- 您可以尝试:

plot_ly(df2, x = ~class, y = ~get(col_word), type = 'bar') %>%
   layout(yaxis = list(title = 'Count'), xaxis = list(title = ""))

Or:或者:

plot_ly(df2, x = ~class, y = as.formula(paste0('~', col_word)), type = 'bar') %>%
  layout(yaxis = list(title = 'Count'), xaxis = list(title = ""))

Edit : Make sure to also use renderPlotly (note the 'ly' at the end).编辑:确保也使用renderPlotly (注意最后的“ly”)。

Here is a full working example in R Markdown with shiny :这是 R Markdown 和shiny中的完整工作示例:

---
title: "Test509"
author: "Ben"
date: "5/9/2020"
output: html_document
---

```{r setup, include=FALSE}
library(shiny)
library(plotly)
library(tidyverse)
```


```{r}
df1 <- data.frame(words = c("bat", "vat","cat","mat"), count = c(3420, 2098, 2003, 1501), words_count = c("bat (3420)","vat (2098)", "cat (2003)", "mat (1501)"), stringsAsFactors = FALSE)

df2 <- data.frame(class = c("A","B","C","D"), bat = c(10,0,30,20), vat = c(0,0,10,30), cat = c(20,30,0,10), mat = c(20,20,0,0), stringsAsFactors = FALSE)

inputPanel(
  selectInput("words", label = "Choose the word",
              choices = df1$words_count, 
              selected = df1$words_count[1], width = '100%')
)

renderPlotly({
  # Extract the word from drop down
  col_word <- df1 %>% 
    filter(words_count == input$words) %>% 
    pull(words)

  plot_ly(df2, x = ~class, y = ~get(col_word), type = 'bar') %>%
    layout(yaxis = list(title = 'Count'), xaxis = list(title = ""))
})
```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Plotly:如何将另一个数据框中的文本添加到 R 中的矩阵热图? - Plotly: how to add text from another dataframe to a matrix heatmap in R? R 如何从一个 dataframe 到另一个 dataframe 对应的列名进行除法 - R how to do division from one dataframe to another dataframe corresponding column name 如何使用从 R 中的现有列中提取的名称向 data.frame 添加列? - How add a column to a data.frame with name extracted from an existing column in R? 使用从 R 上的另一列中提取的信息创建新列 - Create new columns with information extracted from another column on R 在 R 中将绘图布局从一个绘图复制到另一个绘图 - Copy plotly layout from one plot to another in R R:从另一个数据框中检索数据框名称 - R: retrieve dataframe name from another dataframe 如何使用 dataframe 中另一列的数据命名 R 中的文件下载? - How do I name file downloads in R using data from another column in dataframe? 如何使用R中另一列的键从dataframe的一列中获取值? - How to get the value from a column of a dataframe with the key of another column in R? 排列从 R 中的 docx 中提取的 dataframe 中的内容 - arrange contents in a dataframe extracted from docx in R R:使用另一个数据框中的列名,条件和值在一个数据框中创建一个新列 - R: Create a new column in a dataframe, using column name, condition and value from another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM