Showing posts with label R. Show all posts
Showing posts with label R. Show all posts

Tuesday, November 12, 2024

Day 2: 30-days to learn rgl, plotly, and gganimate - Explore plotly customizations: add labels, hover effects, adjust colors, and practice layout modifications

 


Step 1: Set Up the Basic Plot

We’ll start by creating a simple scatter plot that we’ll customize throughout this tutorial.

1.     Create a Sample Dataset:

# Sample data
x <- c(1, 2, 3, 4, 5)
y <- c(10, 15, 13, 17, 20)
labels <- c("A", "B", "C", "D", "E")

2.     Generate the Basic Scatter Plot:

library(plotly)
 
# Basic scatter plot
plot <- plot_ly(x = ~x, y = ~y, type = 'scatter', mode = 'markers')
plot

Step 2: Add Labels

You can add custom labels for each point to provide additional context.

1.     Add Labels as Text:

plot <- plot_ly(x = ~x, y = ~y, text = ~labels, type = 'scatter', mode = 'markers+text') %>%
  layout(title = "Scatter Plot with Labels",
         xaxis = list(title = "X Axis"),
         yaxis = list(title = "Y Axis"))
plot
    • Explanation:
      • text = ~labels: Adds text labels from the labels variable.
      • mode = 'markers+text': Displays both markers and text.

2.     Adjust Text Position:

plot <- plot %>%
  layout(title = "Scatter Plot with Custom Label Positions") %>%
  style(textposition = 'top right')  # Try 'top left', 'bottom right', etc.
plot

Step 3: Add and Customize Hover Effects

Hover effects can be customized to display more detailed information.

1.     Customize Hover Text:

plot <- plot_ly(x = ~x, y = ~y, text = ~paste("Point:", labels, "<br>X:", x, "<br>Y:", y),
                hoverinfo = 'text', type = 'scatter', mode = 'markers') %>%
  layout(title = "Scatter Plot with Customized Hover Text")
plot
    • Explanation:
      • hoverinfo = 'text': Specifies that hover text should come from the text argument.
      • paste() function with <br>: Adds HTML line breaks to format the hover text neatly.

Step 4: Adjust Colors

Colors can make plots visually appealing and help convey information.

1.     Change Marker Colors:

plot <- plot_ly(x = ~x, y = ~y, text = ~labels, type = 'scatter', mode = 'markers',
                marker = list(color = 'blue', size = 10)) %>%
  layout(title = "Scatter Plot with Custom Marker Color")
plot


2.     Add Conditional Colors:

# Set colors based on y values
colors <- c("red", "green", "blue", "orange", "purple")
 
plot <- plot_ly(x = ~x, y = ~y, text = ~labels, type = 'scatter', mode = 'markers',
                marker = list(color = colors, size = 10)) %>%
  layout(title = "Scatter Plot with Conditional Colors")
plot

3.     Use a Color Gradient:

plot <- plot_ly(x = ~x, y = ~y, text = ~labels, type = 'scatter', mode = 'markers',
                marker = list(color = ~y, colorscale = 'Viridis', size = 10)) %>%
  layout(title = "Scatter Plot with Gradient Colors")
plot

Step 5: Customize Layout

plotly offers extensive layout options to adjust axes, titles, legends, and background.

1.     Modify Title and Axis Titles:

plot <- plot %>%
  layout(title = list(text = "Customized Scatter Plot", font = list(size = 20, color = "darkblue")),
         xaxis = list(title = "Custom X Axis", titlefont = list(size = 15, color = "darkred")),
         yaxis = list(title = "Custom Y Axis", titlefont = list(size = 15, color = "darkgreen")))
plot

2.     Customize Legend:

# Adding a legend and customizing its position and appearance
plot <- plot %>%
  layout(showlegend = TRUE, legend = list(x = 1, y = 1, font = list(size = 10, color = "black")))
plot

3.     Change Background Color:

plot <- plot %>%
  layout(plot_bgcolor = 'lightgrey',  # Background color of the plot area
         paper_bgcolor = 'lavender')  # Background color of the paper
plot



Step 6: Combine All Customizations

Let’s put together everything we learned to create a fully customized scatter plot.

plot <- plot_ly(x = ~x, y = ~y, text = ~paste("Label:", labels, "<br>X:", x, "<br>Y:", y),
                hoverinfo = 'text', type = 'scatter', mode = 'markers+text',
                marker = list(color = ~y, colorscale = 'Viridis', size = 15)) %>%
  layout(title = list(text = "Fully Customized Scatter Plot", font = list(size = 22, color = "darkblue")),
         xaxis = list(title = "Custom X Axis", titlefont = list(size = 15, color = "darkred")),
         yaxis = list(title = "Custom Y Axis", titlefont = list(size = 15, color = "darkgreen")),
         plot_bgcolor = 'lightgrey',
         paper_bgcolor = 'lavender',
         legend = list(x = 1, y = 1, font = list(size = 10, color = "black")))
 
plot

Summary

Today, you:

  • Added text labels and customized their position.
  • Created hover text with detailed information and used HTML for formatting.
  • Customized colors, including conditional colors and gradients.
  • Modified layout elements such as titles, axis labels, background color, and legend position.

Next, you’ll practice 3D plotting and explore deeper interactions to make your plots even more dynamic.


Monday, November 11, 2024

Day 1 - 30 days R learning plan for gganimate, plotly, and rgl, and their combination

 


Here’s a step-by-step tutorial for Day 1 to get you started with plotly in R. This guide will walk you through the installation process and teach you how to create basic interactive plots like scatter, bar, and line charts.


Step 1: Install and Load plotly

1.     Open RStudio (or any R environment): Make sure you have an R environment ready to work with.

2.     Install plotly: If you haven’t installed plotly yet, use the following code:

install.packages("plotly")

3.     Load plotly: Load the library so that you can use it in your session.

library(plotly)

Step 2: Create Basic Interactive Plots

A. Scatter Plot

1.     Create Sample Data: For the scatter plot, let’s create a simple dataset to plot.

# Sample data
x <- c(1, 2, 3, 4, 5)
y <- c(10, 15, 13, 17, 20)

2.     Create a Scatter Plot with plot_ly(): Use plot_ly() to create an interactive scatter plot.

scatter_plot <- plot_ly(x = ~x, y = ~y, type = 'scatter', mode = 'markers')
scatter_plot
    • Explanation:
      • x = ~x and y = ~y: Define the x and y coordinates for the points.
      • type = 'scatter': Specifies a scatter plot.
      • mode = 'markers': Displays only markers (you can also try 'lines+markers').

B. Bar Chart

1.     Create Sample Data for a Bar Chart:

categories <- c("A", "B", "C", "D")
values <- c(20, 14, 23, 17)

2.     Create a Bar Chart:

bar_chart <- plot_ly(x = ~categories, y = ~values, type = 'bar')
bar_chart
    • Explanation:
      • x = ~categories: Defines the x-axis labels.
      • y = ~values: Defines the values for each bar.
      • type = 'bar': Specifies a bar chart.

C. Line Chart

1.     Create Data for a Line Chart:

time <- c(1, 2, 3, 4, 5)
values <- c(3, 7, 9, 6, 10)

2.     Create a Line Chart:

line_chart <- plot_ly(x = ~time, y = ~values, type = 'scatter', mode = 'lines')
line_chart
    • Explanation:
      • type = 'scatter' with mode = 'lines': This combination creates a line plot.
      • x = ~time and y = ~values: Define the x and y coordinates for the line.

Step 3: Customize Your Plots

You can customize colors, add titles, and adjust axis labels for each plot.

1.     Add Titles and Labels:

# Scatter Plot with Labels
scatter_plot <- scatter_plot %>%
  layout(title = "Interactive Scatter Plot",
         xaxis = list(title = "X Axis"),
         yaxis = list(title = "Y Axis"))
 
# Bar Chart with Labels
bar_chart <- bar_chart %>%
  layout(title = "Interactive Bar Chart",
         xaxis = list(title = "Categories"),
         yaxis = list(title = "Values"))
 
# Line Chart with Labels
line_chart <- line_chart %>%
  layout(title = "Interactive Line Chart",
         xaxis = list(title = "Time"),
         yaxis = list(title = "Values"))

2.     View Customized Plots: Display each plot with the customized labels.

scatter_plot
bar_chart
line_chart

Step 4: Explore Interactivity

Hover over the data points on each plot to see interactive information, and try zooming and panning. plotly provides built-in interactivity, including tooltips and customizable actions.