Implement geom_point() and geom_bin2d() side by side
Now we will Implement geom_point() and geom_bin2d() side by side on weather history dataset to understand the features of both functions.
Dataset Link – Weather History
# Load required libraries
library(ggplot2)
library(cowplot)
# Read the dataset
weather <- read.csv("your/path")
# Plot using geom_point with customization
plot_point <- ggplot(weather, aes(x = Temperature..C., y = Pressure..millibars.)) +
geom_point(alpha = 0.5, color = "hotpink", size = 3, shape = 16) +
labs(x = "Temperature (C)", y = "Pressure (millibars)") +
theme_minimal()
plot_bin2d <- ggplot(weather, aes(x = Temperature..C., y = Pressure..millibars.)) +
geom_bin2d(binwidth = c(2, 100), aes(fill = ..count..), color = "black", alpha = 0.8)+
scale_fill_gradient(name = "Density", low = "yellow", high = "red") +
labs(x = "Temperature (C)", y = "Pressure (millibars)") +
theme_minimal() +
theme(legend.position = "right")
# Display plots side by side
plot_grid(plot_point, plot_bin2d, labels = c("Scatter Plot", "Heatmap"))
Output:
Used geom_point() to create a scatter plot.
- Adjusted point appearance: set transparency (alpha = 0.5), color (color = “hotpink”), size (size = 3), and shape (shape = 16).
- Added labels for the x and y axes using labs().
- Applied a minimal theme using theme_minimal().
- Customized Heatmap (geom_bin2d):
- Used geom_bin2d() to create a heatmap.
- Mapped the fill color to the count of points in each bin using aes(fill = ..count..).
- Adjusted bin appearance: set bin width (binwidth = c(2, 100)), outline color (color = “black”), and transparency (alpha = 0.8).
# Take a sample from the dataset (2000 rows)
sample_data <- weather[sample(nrow(weather), 2000), ]
# Plot using geom_point
plot_point <- ggplot(sample_data, aes(x = Temperature..C., y = Humidity)) +
geom_point(alpha = 0.5, color = "blue") +
labs(x = "Temperature (C)", y = "Humidity") +
ggtitle("Relationship between Temperature and Humidity")
# Plot using geom_bin2d
plot_bin2d <- ggplot(sample_data, aes(x = Temperature..C., y = Humidity)) +
geom_bin2d(binwidth = c(2, 5), color = "black") +
labs(x = "Temperature (C)", y = "Humidity") +
ggtitle("Relationship between Temperature and Humidity")
# Display plots side by side
plot_grid(plot_point, plot_bin2d) #, labels = c("Scatter Plot", "Heatmap")
Output:
Customized fill color gradient using scale_fill_gradient().
- Added labels for the x and y axes using labs().
- Positioned the legend on the right side using theme(legend.position = “right”).
- Applied a minimal theme using theme_minimal().
Display Side by Side by using plot_grid() from the cowplot package to display the scatter plot and heatmap side by side, with appropriate labels.
Plotting Large Datasets with ggplot2’s geom_point() and geom_bin2d()
ggplot2 is a powerful data visualization package in R Programming Language, known for its flexibility and ability to create a wide range of plots with relatively simple syntax. It follows the “Grammar of Graphics” framework, where plots are constructed by combining data, aesthetic mappings, and geometric objects (geoms) representing the visual elements of the plot.
Contact Us