Welcome Back

Google icon Sign in with Google
OR
I agree to abide by Pharmadaily Terms of Service and its Privacy Policy

Create Account

Google icon Sign up with Google
OR
By signing up, you agree to our Terms of Service and Privacy Policy
Instagram
youtube
Facebook

Creating Basic Plots (Bar, Line, Scatter)

Data visualization helps in understanding patterns, trends, and relationships in data. In R, the ggplot2 package is commonly used to create different types of plots. Among the most basic and widely used plots are bar plots, line plots, and scatter plots. Each plot type is used for a specific purpose depending on the type of data being analyzed.

To create plots using ggplot2, the package must first be loaded into the R session.

library(ggplot2)

A scatter plot is used to show the relationship between two numerical variables. Each point on the plot represents an observation in the dataset.

ggplot(data = mtcars, aes(x = wt, y = mpg)) +
  geom_point()

In this example, the weight of the car is displayed on the x-axis, and miles per gallon is displayed on the y-axis. The geom_point() function creates the scatter plot.

A line plot is commonly used to show trends over time or ordered data. It connects data points with lines to display changes across a sequence.

ggplot(data = economics, aes(x = date, y = unemploy)) +
  geom_line()

In this example, the line plot shows the change in unemployment numbers over time. The geom_line() function is used to create the line chart.

A bar plot is used to compare values across different categories. It is commonly used for categorical data.

ggplot(data = mtcars, aes(x = factor(cyl))) +
  geom_bar()

In this example, the bar plot shows the count of cars for each cylinder category. The factor(cyl) function converts the cylinder variable into a categorical variable, and geom_bar() creates the bars.

These basic plots form the foundation of data visualization. Scatter plots help identify relationships between variables, line plots show trends over time, and bar plots compare values across categories. By choosing the appropriate plot type, analysts can communicate insights more clearly and effectively.