R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Creating one-dimensional scatterplots (also known as strip plots, dot plots, or jitter plots) is a great way to visualize the distribution of a single continuous variable or to compare a continuous variable across different levels of a categorical variable. Here's how you can create such plots in R:
For a simple strip plot in base R, you can use the stripchart()
function:
# Sample data data <- rnorm(100) # Create a basic strip plot stripchart(data, method="jitter", vertical=TRUE, pch=20, main="One-dimensional Scatterplot", ylab="Value")
Here:
method="jitter"
adds a little random noise to separate overlapping data points.vertical=TRUE
makes the plot vertical. Set this to FALSE
for a horizontal plot.pch=20
specifies the plotting symbol (filled circle).ggplot2
:The ggplot2
package offers a geom_jitter()
function, which provides more flexibility:
First, you'll need to install and load the ggplot2
package if you haven't already:
install.packages("ggplot2") library(ggplot2)
Now, let's create a one-dimensional scatterplot:
# Sample data data <- data.frame(Value=rnorm(100)) # Create a jitter plot using ggplot2 ggplot(data, aes(x=1, y=Value)) + geom_jitter(width=0.2) + theme_minimal() + labs(title="One-dimensional Scatterplot", x="", y="Value")
In this code:
aes(x=1, y=Value)
places all points along x=1
.width=0.2
controls the amount of jitter or spread along the x-axis.If you have a categorical variable and want to compare the distribution of the continuous variable across categories, you can do so easily with ggplot2
:
# Sample data set.seed(123) # for reproducibility data <- data.frame(Category=rep(c("A", "B", "C"), each=100), Value=c(rnorm(100, mean=0), rnorm(100, mean=2), rnorm(100, mean=4))) # Create a jitter plot comparing across categories ggplot(data, aes(x=Category, y=Value)) + geom_jitter(width=0.2) + theme_minimal() + labs(title="One-dimensional Scatterplot by Category", x="Category", y="Value")
This allows you to see the distribution of the continuous variable for each category side-by-side.
With the techniques provided, you can create one-dimensional scatterplots in R for various data visualization needs.
R scatterplot with one variable:
# Create a numeric vector data_vector <- c(3, 5, 7, 2, 6) # Create a one-dimensional scatterplot using plot() function plot(data_vector, main = "One-Dimensional Scatterplot")
Plotting univariate data in R:
# Create a numeric vector data_vector <- c(3, 5, 7, 2, 6) # Create a one-dimensional scatterplot using plot() function plot(data_vector, main = "Univariate Data Plot")
Stripchart in R:
# Create a numeric vector data_vector <- c(3, 5, 7, 2, 6) # Create a stripchart using stripchart() function stripchart(data_vector, method = "stack", main = "Stripchart")
Dot plot for one variable in R:
# Create a numeric vector data_vector <- c(3, 5, 7, 2, 6) # Create a dot plot for one variable using dotchart() function dotchart(data_vector, main = "Dot Plot for One Variable")
One-dimensional scatterplot ggplot2:
# Create a numeric vector data_vector <- c(3, 5, 7, 2, 6) # Create a one-dimensional scatterplot using ggplot2 library(ggplot2) ggplot(data.frame(x = data_vector), aes(x = x)) + geom_point() + ggtitle("One-Dimensional Scatterplot with ggplot2")
R plot function with a single variable:
# Create a numeric vector data_vector <- c(3, 5, 7, 2, 6) # Create a one-dimensional scatterplot using plot() function plot(data_vector, main = "One-Dimensional Scatterplot")
Customizing univariate scatterplots in R:
# Create a numeric vector data_vector <- c(3, 5, 7, 2, 6) # Customize the scatterplot using additional parameters plot(data_vector, main = "Customized Scatterplot", col = "blue", pch = 16, cex = 1.5)
Add jitter to one-dimensional scatterplot in R:
# Create a numeric vector data_vector <- c(3, 5, 7, 2, 6) # Create a one-dimensional scatterplot with jitter using plot() function plot(jitter(data_vector), main = "Scatterplot with Jitter")