अपने प्लॉट को इंटरैक्टिव बनाइए
plotly Shiny में इंटरैक्टिव प्लॉट बनाने के लिए एक लोकप्रिय पैकेज है. इंटरैक्टिव विज़ुअलाइज़ेशन के लिए और भी कई पैकेज हैं, लेकिन हम plotly का उपयोग मुख्यतः इसके ggplotly() फंक्शन की वजह से करेंगे, जो किसी ggplot2 प्लॉट को इंटरैक्टिव प्लॉट में बदल देता है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
केस स्टडीज़: R में Shiny के साथ वेब एप्लिकेशन बनाना
अभ्यास निर्देश
पिछले अभ्यास की Shiny ऐप का कोड दिया गया है. आपका काम ggplot2 प्लॉट को plotly प्लॉट से बदलना है. विशेष रूप से:
plotlyपैकेज लोड करें.- प्लॉट आउटपुट फंक्शन को
plotlyOutputसे बदलें (लाइन 20). - प्लॉट रेंडर फंक्शन को
renderPlotlyसे बदलें (लाइन 29). - मौजूदा
ggplot2प्लॉट कोplotlyप्लॉट में बदलें (लाइन 31).
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Load the plotly package
___
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textInput("title", "Title", "GDP vs life exp"),
numericInput("size", "Point size", 1, 1),
checkboxInput("fit", "Add line of best fit", FALSE),
colourInput("color", "Point color", value = "blue"),
selectInput("continents", "Continents",
choices = levels(gapminder$continent),
multiple = TRUE,
selected = "Europe"),
sliderInput("years", "Years",
min(gapminder$year), max(gapminder$year),
value = c(1977, 2002))
),
mainPanel(
# Replace the `plotOutput()` with the plotly version
plotOutput("plot")
)
)
)
# Define the server logic
server <- function(input, output) {
# Replace the `renderPlot()` with the plotly version
output$plot <- renderPlot({
# Convert the existing ggplot2 to a plotly plot
___({
data <- subset(gapminder,
continent %in% input$continents &
year >= input$years[1] & year <= input$years[2])
p <- ggplot(data, aes(gdpPercap, lifeExp)) +
geom_point(size = input$size, col = input$color) +
scale_x_log10() +
ggtitle(input$title)
if (input$fit) {
p <- p + geom_smooth(method = "lm")
}
p
})
})
}
shinyApp(ui = ui, server = server)