![]() |
R Programming Language is a powerful tool for data analysis and visualization. Interactive plots with R can be particularly useful for exploring and presenting data, but creating them can be challenging. The Shiny package provides a framework for creating web-based applications with R, including interactive plots. However, Shiny can be complex and time-consuming to set up, and it may not be suitable for all situations. Interactive plots with RAn alternative to using Shiny for creating interactive plots is to use the Plotly and Leaflet packages in combination with HTML widgets. These packages allow you to create interactive plots with R visualizations directly from R code, without the need for a separate web application framework. In this way, you can quickly create interactive plots with r that can be easily shared and embedded in web pages.
# Install packages Interactive scatter plot using PlotlyThe plotly package provides an easy way to create interactive plots with R. We will use the iris dataset and data frame for our example.
Output: In this code, we create a scatter plot using the plot_ly() function, specifying the iris dataset, the x-axis (Sepal.Length) and y-axis (Petal.Length) variables, and the type of plot (scatter) with mode = “markers”. We also add color and size to the markers using the add_markers() function. Finally, we add labels and a title to the plot using the layout() function and display the plot using the plot object. Interactive line plots with R
Output: This code creates an interactive plots with r using the plotly package in R. It first creates a dataset with x and y values, then creates the plot using plot_ly() function and specifies the data and plot type. The resulting plot can be interacted with by zooming, hovering over the points to see their values, and selecting specific points to display only those points. Finally, the plot is saved as an HTML widget using the saveWidget() function, which can be embedded into a website or viewed in a web browser. Interactive 3D Scatter Plot
Output: Interactive Surface Plot
Output: Interactive map using LeafletThe leaflet package provides an easy way to create interactive maps in R. We will use the mtcars dataset for our example.
Output: In this code, we create a map using the leaflet() function, specifying the mtcars dataset and adding tiles using the addTiles() function. We also set the initial view of the map using the setView() function.
ConclusionIn conclusion, we have demonstrated how to generate interactive plots with R using HTMLwidgets. We used built-in datasets in R, specifically the iris and mtcars datasets, to create an interactive scatter plot using Plotly and an interactive map using leaflet. |
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |