# Loading the datasets
data(iris)
terrorism_data <- read.csv("global_terror.csv")

Question 1

# Creating a scatter plot with overlayed density plots
plot_ly(data = iris, x = ~Sepal.Length, y = ~Sepal.Width, color = ~Species, type = "scatter") %>%
  add_trace(x = ~Sepal.Length, type = "histogram", opacity = 0.5, histnorm = "probability density") %>%
  layout(
    title = "Iris Dataset: Sepal Length vs Sepal Width",
    
    xaxis = list(title = "Sepal Length (cm)"),
    yaxis = list(title = "Sepal Width (cm)"),
    hovermode = "closest",
    hoverlabel = list(
      bgcolor = "white",
      font = list(color = "black")
    )
  ) %>%
  style(hoveron = "points", hoverinfo = "text", text = ~paste(
    "Species: ", Species,
    "<br>Sepal Length: ", Sepal.Length,
    "<br>Sepal Width: ", Sepal.Width
  ))
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode

The scatter plot displays each data point as a colored marker, where the color represents the species of iris (setosa, versicolor, or virginica). The x-axis represents the sepal length, while the y-axis represents the sepal width. By looking at the distribution of these markers, you can observe any potential clustering or separation between the species based on these two features. Additionally, the plot includes overlaid density plots, which provide an estimate of the probability density function for the sepal length variable. These density plots help you assess the distribution of sepal length across the entire dataset. One of the most exciting aspects of this plot is its interactive nature. When you hover your cursor over a data point, a tooltip appears, displaying additional information about that particular observation. The tooltip shows the species name, sepal length, and sepal width for the selected data point. This interactive feature enhances your ability to explore the data in more detail. You can easily identify specific data points of interest and quickly access their associated values. While creating this plot, you may encounter some challenges, especially if the dataset contains a large number of data points. In such cases, overlapping markers might make the plot cluttered and difficult to interpret. To address this issue, you could consider using additional techniques like transparency or jittering to improve the visualization’s clarity.

Question 2

# Filtering the data
filtered_data <- terrorism_data %>%
  filter(region_txt %in% c("Sub-Saharan Africa", "Middle East & North Africa", "South Asia")) 
# Creating an animated plotly graph
plot_ly(data = filtered_data, x = ~longitude, y = ~latitude, color = ~region_txt,
        type = "scattergeo", mode = "markers", marker = list(size = 10)) %>%
  layout(
    title = "Global Terrorism Incidents by Region",
    geo = list(
      projection = list(type = "orthographic"),  # Change projection type
      showland = TRUE,
      showcountries = TRUE
    ),
    sliders = list(
      list(
        currentvalue = list(prefix = "Region: "),
        font = list(color = "blue", size = 16),  # Change font color and size
        xanchor = "right",  # Align slider to the right
        yanchor = "top",    # Align slider to the top
        transition = list(duration = 0),  # Transition duration
        visible = TRUE  # Slider visibility
      )
    )
  )

The provided code creates an animated plotly graph that visualizes global terrorism incidents across different regions. The plot focuses on three specific regions: Sub-Saharan Africa, Middle East & North Africa, and South Asia. Each data point represents a terrorism incident, and its location is plotted on a world map using longitude and latitude coordinates. The animation feature of the plot allows you to observe the distribution of terrorism incidents across these regions over time. As the animation progresses, you can see how the incidents are concentrated in certain areas within each region, potentially revealing hotspots or patterns of terrorist activities. One notable aspect of the plot is the use of an orthographic projection, which provides a more immersive and globe-like representation of the Earth. This projection type can help you better visualize the spatial distribution of incidents, especially when dealing with data spanning multiple continents or regions. The plot also includes a slider at the top-right corner, which displays the current region being shown in the animation. The slider’s font color and size have been customized to enhance visibility and readability. While creating this plot, I encountered a issue with the data, as the dataset is too large the map is little bit clumsy. That is the only major issue faced.