R Plotly - 对 x 轴变量使用别名
R Plotly - use alias for x-axis variable
试图弄清楚如何使用别名而不是替换 R Plotly 条形图的 x 轴中的变量。举个例子:
if (interactive()) {
library(plotly)
PartNum <- c("123", "456", "789", "321", "654")
PartName <- c("washer", "nut", "bolt", "washer", "screw")
PartCount <- c(10, 15, 6, 8, 2)
data <- data.frame(PartNum, PartName, PartCount)
ui <- fluidPage(
radioButtons("radio_PartNumName", "Show Part:",
c("Number" = "PartNum", "Name" = "PartName"),
inline = TRUE
),
plotlyOutput("partPlot")
)
server <- function(input, output, session) {
output$partPlot <- renderPlotly({
plot_ly(data,
x = ~get(input$radio_PartNumName),
y = ~PartCount,
type = "bar",
text = ~PartCount)
})
}
shinyApp(ui, server)
}
当运行时,它输出这样的图形:
当您单击单选按钮将其更改为 Name
时,图表会更改并聚合两个 washer 值,如下所示:
我不希望聚合这些值,而是简单地将 Part Numbers
替换为 Part Names
,这样图表会更像这样:
您需要使用 ticktext
来更改刻度标签,x 需要保持不变。
更多信息请参阅schema()
:
对象 ► 布局 ► layoutAttributes ► xaxis ► ticktext
请检查以下内容:
library(shiny)
library(plotly)
if (interactive()) {
PartNum <- c("123", "456", "789", "321", "654")
PartName <- c("washer", "nut", "bolt", "washer", "screw")
PartCount <- c(10, 15, 6, 8, 2)
data <- data.frame(PartNum, PartName, PartCount)
ui <- fluidPage(
radioButtons("radio_PartNumName", "Show Part:",
c("Number" = "PartNum", "Name" = "PartName"),
inline = TRUE
),
plotlyOutput("partPlot")
)
server <- function(input, output, session) {
output$partPlot <- renderPlotly({
plot_ly(data,
x = ~PartNum,
y = ~PartCount,
type = "bar",
text = ~PartCount) %>%
layout(xaxis = list(
tickmode = "array",
tickvals = ~PartNum,
ticktext = ~get(input$radio_PartNumName))
)
})
}
shinyApp(ui, server)
}
试图弄清楚如何使用别名而不是替换 R Plotly 条形图的 x 轴中的变量。举个例子:
if (interactive()) {
library(plotly)
PartNum <- c("123", "456", "789", "321", "654")
PartName <- c("washer", "nut", "bolt", "washer", "screw")
PartCount <- c(10, 15, 6, 8, 2)
data <- data.frame(PartNum, PartName, PartCount)
ui <- fluidPage(
radioButtons("radio_PartNumName", "Show Part:",
c("Number" = "PartNum", "Name" = "PartName"),
inline = TRUE
),
plotlyOutput("partPlot")
)
server <- function(input, output, session) {
output$partPlot <- renderPlotly({
plot_ly(data,
x = ~get(input$radio_PartNumName),
y = ~PartCount,
type = "bar",
text = ~PartCount)
})
}
shinyApp(ui, server)
}
当运行时,它输出这样的图形:
当您单击单选按钮将其更改为 Name
时,图表会更改并聚合两个 washer 值,如下所示:
我不希望聚合这些值,而是简单地将 Part Numbers
替换为 Part Names
,这样图表会更像这样:
您需要使用 ticktext
来更改刻度标签,x 需要保持不变。
更多信息请参阅schema()
:
对象 ► 布局 ► layoutAttributes ► xaxis ► ticktext
请检查以下内容:
library(shiny)
library(plotly)
if (interactive()) {
PartNum <- c("123", "456", "789", "321", "654")
PartName <- c("washer", "nut", "bolt", "washer", "screw")
PartCount <- c(10, 15, 6, 8, 2)
data <- data.frame(PartNum, PartName, PartCount)
ui <- fluidPage(
radioButtons("radio_PartNumName", "Show Part:",
c("Number" = "PartNum", "Name" = "PartName"),
inline = TRUE
),
plotlyOutput("partPlot")
)
server <- function(input, output, session) {
output$partPlot <- renderPlotly({
plot_ly(data,
x = ~PartNum,
y = ~PartCount,
type = "bar",
text = ~PartCount) %>%
layout(xaxis = list(
tickmode = "array",
tickvals = ~PartNum,
ticktext = ~get(input$radio_PartNumName))
)
})
}
shinyApp(ui, server)
}