Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为plotly graph-R选择数据框中的列_R_Ggplot2_Shiny_Plotly_R Plotly - Fatal编程技术网

为plotly graph-R选择数据框中的列

为plotly graph-R选择数据框中的列,r,ggplot2,shiny,plotly,r-plotly,R,Ggplot2,Shiny,Plotly,R Plotly,通过下面的编码,我一直在尝试改变y轴来绘制图表,但我不太确定我在做什么。在这个问题上,他们似乎想要得到相似的东西,但对于一个数据表,区别在于他们有一个全局数据帧,我需要它是反应式的,因为我希望当我改变输入时整个可视化都会改变 # GLOBAL # # UI # ui <- fluidPage( # Set theme theme = shinytheme("lumen"), navbarPage("Analysis", tabPanel("

通过下面的编码,我一直在尝试改变y轴来绘制图表,但我不太确定我在做什么。在这个问题上,他们似乎想要得到相似的东西,但对于一个数据表,区别在于他们有一个全局数据帧,我需要它是反应式的,因为我希望当我改变输入时整个可视化都会改变

# GLOBAL #


# UI #
ui <- fluidPage( 
  # Set theme 
  theme = shinytheme("lumen"),

  navbarPage("Analysis",

             tabPanel("Impact",

                      titlePanel(
                        div(
                          h1(HTML(paste0("<b>","Graph against cluster count","</b>"))),
                          align = "left"
                        )
                      ),

                      tags$br(),

                      fluidRow(
                        sidebarPanel(
                          hr(style="border-color: #606060;"),
                          h3(HTML(paste0("<b>","Clusters impact.","</b>"))),
                          h5("Key areas of patent concentration can be found around the clusters that reach higher levels."),
                          br(),
                          # Y axis selection 
                          radioButtons("y_axis", 
                                       h4("What do you want to analyze IP collection against?"), 
                                       choices = list("Claims"                  = 3, 
                                                      "Forward citations"       = 4,
                                                      "Backward citations"      = 5,
                                                      "Patent Strenght mean"    = 6),
                                       selected = 3), # radioButtons - y_axis
                          hr(style="border-color: #606060;"),
                          width = 3
                        ),
                        mainPanel(
                          br(),
                          plotlyOutput("impact"),
                          br(),
                          width = 9
                        )
                      )
             )
  )
)

# SERVER #
server <- function(input, output, session) {

  # Set maximun input size as 100Mb
  options(shiny.maxRequestSize=100*1024^2)

  # Plot

  ## Data setting
  dtd5 <- reactive({

    dtd5 <- structure(list(Topic = c("Topic 1", "Topic 3", "Topic 5", "Topic 9"), 
                           Count = c(45L, 51L, 40L, 32L), 
                           Claims = c(630, 346, 571, 599), 
                           Forward = c(64, 32, 27, 141), 
                           Backward = c(266,  177, 101, 397), 
                           `Strength mean` = c(31, 25.22, 30.85, 39.59)), 
                      row.names = c(NA, -4L), class = "data.frame")
    dtd5 <- as.data.frame(dtd5)

  })

  ## Visualization
  output$impact <- renderPlotly({

    # Color setting
    ramp4 <- colorRamp(c("darkred", "snow3"))
    ramp.list4 <- rgb( ramp4(seq(0, 1, length = 15)), max = 255)

    # Scatterplot
    y <- dtd5()[,input$y_axis]

    p <- ggplot(dtd5(), aes(x=Count, y=y) ) +
      geom_point(aes(col=Topic)) +
      labs(y=colnames(dtd5())[input$y_axis],
           x="Cluster count",
           title="Cluster Impact")  +
      theme_minimal() +
      scale_colour_manual(values=ramp.list4)

    ggplotly(p) %>%
      config(displayModeBar = FALSE) 

  })
}

shinyApp(ui,server)
#全局#
#用户界面#

ui问题似乎出在您的
单选按钮上
-即使
选项设置为返回数值3到6,它也会返回字符串

如果您选中帮助
?单选按钮
,您将在
选项
下看到这一点:

值应该是字符串;其他类型(如逻辑和 数字)将强制为字符串


如果将
指定为.numeric(输入$y_轴)
renderPlotly
中的两个位置,它应该可以工作。

是的!非常感谢你!
dtd5 <- structure(list(Topic = c("Topic 1", "Topic 3", "Topic 5", "Topic 9"
), Count = c(45L, 51L, 40L, 32L), Claims = c(630, 346, 571, 599
), Forward = c(64, 32, 27, 141), Backward = c(266, 177, 101, 
397), `Stregth mean` = c(31, 25.22, 30.85, 39.59)), row.names = c(NA, 
-4L), class = "data.frame")

# Scatterplot
y <- dtd5[,4]
p <- ggplot(dtd5, aes(x=Count, y=y) ) +
  geom_point(aes(col=Topic)) +
  labs(y=colnames(dtd5)[4],
       x="Number of patents",
       title="Cluster Impact")  +
  theme_minimal() 

ggplotly(p) %>%
  config(displayModeBar = FALSE)