Thursday, November 14, 2024

Day 4: 30-days to learn rgl, plotly, and gganimate - Installing rgl and Creating Basic 3D Plots




Step 1: Install and Load rgl

1.     Install the rgl package (if you haven’t already):

install.packages("rgl")

2.     Load the rgl library:

library(rgl)

Step 2: Create a Basic 3D Scatter Plot using plot3d

1.     Generate sample data for plotting:

set.seed(123)
x <- rnorm(100)
y <- rnorm(100)
z <- rnorm(100)

2.     Create a 3D scatter plot using plot3d:

plot3d(x, y, z, col = "blue", size = 5, type = "s")
    • col: Set the color of the points.
    • size: Control the point size.
    • type: "s" represents spheres. You can also try other types, like "p" for points.

The plot should now open in an interactive window, allowing you to view your data in 3D.

Step 3: Create a 3D Scatter Plot using scatter3d

1.     Install and load the car package to access the scatter3d function:

install.packages("car")
library(car)

2.     Use scatter3d for a quick 3D scatter plot:

scatter3d(x, y, z, surface = FALSE, fit = "linear")
    • surface: Set to FALSE if you don't want a regression surface (3D plane).
    • fit: Set to "linear" for a linear fit (you can also use other fits if you like).

Step 4: Exploring Basic Camera Rotations and Zoom Functions

rgl provides several functions to interactively control the camera's view, allowing you to rotate, zoom, and pan.

1.     Rotate the Camera:

    • The rgl.viewpoint() function allows you to set the camera angle. Try the following command:
rgl.viewpoint(theta = 45, phi = 30)
      • theta: Controls rotation around the z-axis.
      • phi: Controls the vertical rotation.

2.     Zoom In and Out:

    • Adjust the zoom level using the zoom parameter in rgl.viewpoint:
rgl.viewpoint(theta = 45, phi = 30, zoom = 0.7)
      • A zoom value less than 1 will zoom out, while a value greater than 1 will zoom in.

3.     Play Around with Different Viewpoints:

    • Experiment with theta, phi, and zoom to see how they change your perspective. Here are a few examples:
# Top-down view
rgl.viewpoint(theta = 90, phi = 90, zoom = 0.5)
 
# Side view
rgl.viewpoint(theta = 0, phi = 0, zoom = 0.8)
 
# Rotate incrementally
for (angle in seq(0, 360, by = 10)) {
  rgl.viewpoint(theta = angle, phi = 30)
  Sys.sleep(0.1)
}

Step 5: Practice and Experiment

Explore rgl further by experimenting with different colors, sizes, and types of points in plot3d, or by adjusting the viewpoint using theta and phi in rgl.viewpoint.

Wednesday, November 13, 2024

Day 3: 30-days to learn rgl, plotly, and gganimate - Dive into 3D plotting with plotly, exploring 3D scatter and surface plots, and customizing axes and color scales




Step 1: Set Up for 3D Plotting

1.     Load plotly: Make sure plotly is loaded in your R environment.

library(plotly)

2.     Create Sample Data: Let’s create sample data for our 3D scatter plot and surface plot.

# Data for 3D Scatter Plot
x_scatter <- rnorm(50)
y_scatter <- rnorm(50)
z_scatter <- rnorm(50)
 
# Data for 3D Surface Plot
x_surface <- seq(-10, 10, length.out = 50)
y_surface <- seq(-10, 10, length.out = 50)
z_surface <- outer(x_surface, y_surface, function(x, y) sin(sqrt(x^2 + y^2)))

Step 2: Create a 3D Scatter Plot

1.     Basic 3D Scatter Plot: Use plot_ly() with type = 'scatter3d' to create a basic 3D scatter plot.

scatter3d_plot <- plot_ly(x = ~x_scatter, y = ~y_scatter, z = ~z_scatter, 
                          type = 'scatter3d', mode = 'markers')
scatter3d_plot
    • Explanation:
      • x = ~x_scatter, y = ~y_scatter, and z = ~z_scatter: Set the x, y, and z coordinates.
      • type = 'scatter3d': Specifies a 3D scatter plot.
      • mode = 'markers': Displays only markers (points) in 3D space.

2.     Customize Marker Colors and Sizes:

scatter3d_plot <- plot_ly(x = ~x_scatter, y = ~y_scatter, z = ~z_scatter, 
                          type = 'scatter3d', mode = 'markers',
                          marker = list(size = 5, color = ~z_scatter, colorscale = 'Viridis'))
scatter3d_plot
    • Explanation:
      • size = 5: Adjusts the size of the markers.
      • color = ~z_scatter: Colors markers based on their z values.
      • colorscale = 'Viridis': Applies a color scale to the z values.

Step 3: Create a 3D Surface Plot

1.     Basic 3D Surface Plot: Use plot_ly() with type = 'surface' to create a basic 3D surface plot.

surface_plot <- plot_ly(z = ~z_surface, x = ~x_surface, y = ~y_surface, type = 'surface')
surface_plot
    • Explanation:
      • z = ~z_surface: Sets the height values of the surface.
      • x = ~x_surface and y = ~y_surface: Sets the x and y coordinates for the surface plot.
      • type = 'surface': Specifies a 3D surface plot.

2.     Customize the Surface Plot with a Color Scale:

surface_plot <- plot_ly(z = ~z_surface, x = ~x_surface, y = ~y_surface, 
                        type = 'surface', colorscale = 'Viridis')
surface_plot
    • Explanation:
      • colorscale = 'Viridis': Adds a Viridis color scale, enhancing the visualization of depth.

Step 4: Customize Axes

You can adjust the axis properties to improve the plot’s readability and style.

1.     Customize 3D Scatter Plot Axes:

scatter3d_plot <- scatter3d_plot %>%
  layout(scene = list(xaxis = list(title = "X Axis", backgroundcolor = "lightgrey", gridcolor = "white"),
                      yaxis = list(title = "Y Axis", backgroundcolor = "lightgrey", gridcolor = "white"),
                      zaxis = list(title = "Z Axis", backgroundcolor = "lightgrey", gridcolor = "white")))
scatter3d_plot
    • Explanation:
      • scene: Defines 3D plot layout options, including xaxis, yaxis, and zaxis.
      • title: Sets custom titles for each axis.
      • backgroundcolor and gridcolor: Adjust axis background and grid colors.

2.     Customize 3D Surface Plot Axes:

surface_plot <- surface_plot %>%
  layout(scene = list(xaxis = list(title = "X Axis", color = "blue"),
                      yaxis = list(title = "Y Axis", color = "green"),
                      zaxis = list(title = "Z Axis", color = "red")))
surface_plot

Step 5: Experiment with Different Color Scales

plotly provides several color scales. Try using different ones to see their effects on your plot.

1.     Apply Different Color Scales to the 3D Scatter Plot:

scatter3d_plot <- plot_ly(x = ~x_scatter, y = ~y_scatter, z = ~z_scatter, 
                          type = 'scatter3d', mode = 'markers',
                          marker = list(size = 5, color = ~z_scatter, colorscale = 'Cividis'))
scatter3d_plot

2.     Apply Different Color Scales to the 3D Surface Plot:

surface_plot <- plot_ly(z = ~z_surface, x = ~x_surface, y = ~y_surface, 
                        type = 'surface', colorscale = 'Electric')
surface_plot
    • Other Color Scales:
      • Try color scales like Jet, Bluered, RdBu, YlGnBu, Greens, and Portland to see different effects.

Step 6: Combine All Customizations

Let’s bring everything together to create a polished 3D scatter and 3D surface plot.

Final 3D Scatter Plot with Customizations

final_scatter3d <- plot_ly(x = ~x_scatter, y = ~y_scatter, z = ~z_scatter, 
                           type = 'scatter3d', mode = 'markers',
                           marker = list(size = 5, color = ~z_scatter, colorscale = 'Viridis')) %>%
  layout(scene = list(xaxis = list(title = "X Axis", backgroundcolor = "lightgrey", gridcolor = "white"),
                      yaxis = list(title = "Y Axis", backgroundcolor = "lightgrey", gridcolor = "white"),
                      zaxis = list(title = "Z Axis", backgroundcolor = "lightgrey", gridcolor = "white")))
final_scatter3d

Final 3D Surface Plot with Customizations

final_surface_plot <- plot_ly(z = ~z_surface, x = ~x_surface, y = ~y_surface, 
                              type = 'surface', colorscale = 'Electric') %>%
  layout(scene = list(xaxis = list(title = "X Axis", color = "blue"),
                      yaxis = list(title = "Y Axis", color = "green"),
                      zaxis = list(title = "Z Axis", color = "red")))
final_surface_plot

Summary

Today, you:

  • Learned to create 3D scatter and surface plots using plotly.
  • Customized marker colors and sizes for 3D scatter plots.
  • Applied color scales to enhance visual appeal.
  • Customized axes in 3D space, including titles, colors, and backgrounds.

Next, you’ll build on this knowledge by diving into more complex plot customizations and interactive features to make the plots even more informative.

Tuesday, November 12, 2024

Several Problematic as well as Beneficial Housing societies in and near Islamabad, Pakistan

 

(Source: Pixabay)

Several housing societies came to market during the time from 2016 and after. Considering the history, during 2003 to 2005, a boom came in the context of housing societies. However, after October 2005 Earthquake, real state sector went down. After that several societies, including Jeddah Town and Green City, came and go. After that from 2006-2007 to 2016-2017, no significant housing society came. However, only old societies keep working. After that due to remittance, some housing societies started working.

In this case, some of the societies are still present that are present on-ground but there is no development in those societies. Among these societies, some are Kingdom Valley, where development is very slow; GFS Society, where development is almost negligible; Nova City, where development started but then stopped; Mivida city, is also not working; Avalon city, which is almost silent; Hawks Melbourne, which is also almost silent; Forest Town, Islamabad, is also almost silent; Saffron City is also almost silent; Mega City, Gujar Khan, is also almost silent; and Abdullah City, Al-Makkah City, and Capital Valley city are also almost silent.

Aside from these societies, there are also other, where some development is taking place, though sometimes development is more and other times less. Among these societies are, Taj Residencia, where previously development speed was slow, but now it is better; Park View City, is also a good housing society and has taken NOCs from government; Blue World City, is also showing development on a large scale; Capital Smart City is also working good; Faisal Town Phase II is also showing good development; Silver City has also taken NOCs from all departments of government and is working on about 5000-7000 kanals of land; New City Phase II and Kohistan Enclave are also developing; and New Metro City, Gujar Khan, is also developing.

Nevertheless, in the case of plots, it is essential to bring plots on-ground or take possession-able plots.

Source:

Gondal Group of Marketing Islamabad - 22 Fraud Housing Projects in Islamabad Rawalpindi | Beware Property Frauds | Scam Housing Projects - https://www.youtube.com/watch?v=5UnoAxUthLk


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.