[英]R Plotly - use alias for x-axis variable
Trying to figure out how to use an alias but not replace the variable in the x-axis of a R Plotly bar graph.试图弄清楚如何使用别名但不替换 R Plotly 条形图 x 轴中的变量。 Take the following example:
以下面的例子为例:
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)
}
When run, it outputs a graph like this:运行时,它输出如下图:
When you click the radio button to change it to Name
, the graph changes and aggregates two washer values like this:当您单击单选按钮将其更改为
Name
,图表会更改并聚合两个垫圈值,如下所示:
I'm not wanting the values to aggregate, but instead simply replace the Part Numbers
with the Part Names
so the graph would like something more like this:我不希望这些值聚合,而是简单地用
Part Names
替换Part Numbers
,这样图形就会更像这样:
You'll need to use ticktext
to change the tick labels, x needs to be constant.您需要使用
ticktext
来更改刻度标签,x 需要保持不变。
For further information please see schema()
: object ► layout ► layoutAttributes ► xaxis ► ticktext有关更多信息,请参阅
schema()
: object ► layout ► layoutAttributes ► xaxis ► ticktext
Please check the following:请检查以下内容:
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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.