Creating an Enhanced Scatter Plot with ggplot2
Now we can Creating an Enhanced Scatter Plot with ggplot2 so we will make our plot more attrective.
R
# Assuming you have a data frame named data_frame with columns col1, col2, and col3 # Load the ggplot2 library library (ggplot2) # Create a more attractive plot ggplot (data_frame, aes (x = col1, y = col3, color = col2)) + geom_point (size = 2, alpha = 0.7) + theme_minimal () + labs (title = "Scatter Plot with Grouping" , x = "X-axis Label" , y = "Y-axis Label" , color = "Group" ) + theme (axis.title.x = element_text (angle = 0, vjust = -0.5, size = 14, color = "blue" ), axis.title.y = element_text (angle = 90, vjust = 0.5, size = 14, color = "green" ), legend.title = element_text (size = 14, color = "purple" ), legend.text = element_text (size = 12), plot.title = element_text (hjust = 0.5, size = 16, color = "brown" ), panel.grid.major = element_blank (), panel.grid.minor = element_blank (), panel.background = element_rect (fill = "lightgray" )) |
Output:
Here we add extra parameters to adjusted point size and transparency for better visibility. used a minimal theme for a clean and simple appearance. added informative labels for the title, X-axis, Y-axis, and color legend.
- Adjusted the angle, size, and color of axis titles for better readability. removed major and minor gridlines for a cleaner look.
- Set a light gray background for the plot panel.
These are the some of the main functions and parameters that we used in Working with Axes in R using ggplot2.
Working with Axes in R using ggplot2
The ggplot2 package is a powerful and widely used package for graphic visualization. It can be used to provide a lot of aesthetic mappings to the plotted graphs. This package is widely available in the R Programming Language. The package can be downloaded and installed into the working space using the following command.
install.packages("ggplot2")
The ggplot method can be used to create a ggplot object. The graphical object is used to create plots by providing the data and its respective points. The data can be plotted using both points as well as lines.
Syntax : ggplot(data, aes = )
Arguments :
data – The data to be plotted
aes – The aesthetic mappings
The geom_point method can be used to plot the data points on the existing visualization that has been created. The color has been assigned based on the grouping values assigned in the col2 of the data plane in the following code snippet.
That is since there are three groups a, b, and c three different colors are used to denote the values contained in these three groups. These parameters are specified in the aesthetic mappings, as discussed above in the ggplot method.
R
#installing the required libraries library ( "ggplot2" ) #creating a data frame data_frame = data.frame (col1 = c (1,4,2,5,6,9,5,3,6,3), col2 = c ( "a" , "b" , "a" , "c" , "b" , "b" , "b" , "a" , "c" , "a" ), col3 = c (3,2,4,2,1,4,8,6,4,2)) #creating the plot #assigning groups based on col2 values ggplot (data_frame, aes (col1, col3, colour=col2)) + geom_point () |
Output