需要帮助..。我制作了一个饼图,比较美国工业和奥通林的汽车产量(内部用途)。当我点击自动计数(图中的橙色区域)时,我想得到一个自动装配的汽车列表,点击美国计数(图中的蓝色区域),.The自动制造列表包含51辆汽车制造,美国制造列表包含79辆汽车制造。我用巧妙的软件包创建了这个图形,数据库连接在一起,.I想要得到autongin和美国工业的汽车制造列表。现在点击情节时没有变化。
The sample data is attached here
AutonginMake USMakename
1 Acura Acura
2 Aston Martin Aston Martin
3 Audi Audi
4 Bentley Bentley
5 BMW BMW
6 Buick Buick
7 Cadillac Cadillac
8 Chevrolet Chevrolet
9 Chrysler Chrysler
10 Dodge Dodge
11 Ford Ford
12 GMC GMC
13 Honda Honda
14 HUMMER Hummer
I took the count of above autonginmake and US make and polotted..My requirement is to list this makes when clicking on corresponding regions of pie chart
#packages needed
library(plotly)
library(shiny)
library(DBI)
library(RMySQL)
#connecting db
dealerinventory1<-dbConnect(RMySQL::MySQL(), user='ghhjjl',
password='dfgfdgdg!',
host='hfghfh',
dbname='hhthhq23u')
uscount1=dbGetQuery(dealerinventory1,
'SELECT count(distinct makename) as USmakes FROM dealer_inventory.CarQuery;')
autongincount1=dbGetQuery(dealerinventory1,
'SELECT count(distinct makename) as autonginmakes FROM dealer_inventory.car_inventory_json_lookup;')
usandautongintable <- c(autongincount1,uscount1)
usandautongintable
label <- c(paste("Autongin Count: ", autongincount1),paste("US Industry Count: ", uscount1))
label
unlist <- as.numeric(unlist(usandautongintable))
typeof(unlist)
#table used for plotting
table<- as.data.frame(usandautongintable)
table
#for plotting pie chart
plotpie<- plot_ly(table, labels = label,values = unlist, type = "pie") %>%
layout(title = 'Comparison of Makes',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
plotpie
library(shiny)
library(plotly)
ui= fluidPage(
plotlyOutput("plot")
)
server1<- function(input,output){
output$plot=renderPlotly({
plot_ly(table, labels = label,values = unlist, type = "pie") %>%
layout(title = 'Comparison of Makes',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
})
}
shinyApp(ui,server1)输出图链接在这里,http://autonginreports.iinerds.com:3838/sample-apps/plot/
发布于 2018-09-14 16:33:45
首先,将make名称作为变量。然后,巧妙地使用event_data来捕捉已经单击的饼饼的哪一部分。“制作”列表打印在示例中的文本输出上,但您可以任意使用它。
这里有一个很好的参考,https://plotly-book.cpsievert.me/linking-views-with-shiny.html。
library(shiny)
library(plotly)
ui <- fluidPage(tagList(
plotlyOutput("pie"),
verbatimTextOutput("makes")
))
server <- function(input, output) {
# dummy makes data
usmakes <- c("ford", "acura", "bmw")
autonginmakes <- c("cadillac", "hummer")
usmakes_count <- length(usmakes)
autonginmakes_count <- length(autonginmakes)
output$pie <- renderPlotly({
plot_ly(labels=c("US", "Autongin"),
values=c(usmakes_count, autonginmakes_count),
key=c("us", "autongin"),
type="pie")
})
output$makes <- renderPrint({
clicked <- event_data("plotly_click")
if (is.null(clicked)) {
"no selection"
} else if (clicked$key == "us") {
usmakes
} else {
autonginmakes
}
})
}
shinyApp(ui, server)https://stackoverflow.com/questions/52307320
复制相似问题