Description Usage Arguments Value Author(s) Examples
This function creates a scatter plot i.e. a two-dimensional plot
that uses dots to visualize the values of two different variables x
and
y
.
1 2 3 4 5 6 7 8 9 10 11 12 |
x |
a numeric vector representing the variable to plot in the x axis. If
set to NULL then a vector |
y |
a numeric vector representing the variable to plot in the y axis. If
set to NULL then a vector |
labels |
an optional vector of character labels for each (x, y) point. Used for color coding each point. Default is NULL. |
color_legend_name |
the name of the legend, valid only if the labels vector is provided. Default is NULL (can be set to NULL as well if labels vector is provided). |
title |
an optional string for the title of the plot. Default is NULL (no title). |
xlab |
a label for the x axis. Default is NULL (no label). |
ylab |
a label for the y axis. Default is NULL (no label). |
plot_ordered |
a logical indicating of the provided data should be
plotted ordered. Valid only if either |
order_by_axis |
the axis by which to order the data (either "x" or "y"). Default is "y". |
title_text_size |
text size of the title. Default is 20. |
A ggplot2 object representing the scatter plot.
Avishay Spitzer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # Example #1 - One variable of interest
# Generate randomly 1000 data points
y <- rnorm(1000)
# See the difference between the ordered and unordered scatter plots
scandal_scatter_plot(x = NULL, y = y, plot_ordered = TRUE, order_by_axis = "y", title = "Ordered plot")
scandal_scatter_plot(x = NULL, y = y, plot_ordered = FALSE, title = "Unordered plot")
# Example #2 - Two variables with linear correlation
library(MASS)
library(ggplot2)
samples <- 1000
r <- 0.8
# Generate 2 variables with a linear correlation between them (1000 data points with pearsnon's r 0.8)
data <- mvrnorm(n = samples, mu = c(0, 0), Sigma = matrix(c(1, r, r, 1), nrow = 2), empirical = TRUE)
# Plot the two variables. As scandal_scatter_plot returns a ggplot object we can add to it a
# linear regression line showing the positive correlation
scandal_scatter_plot(x = data[, 1], y = data[, 2], plot_ordered = FALSE) +
geom_smooth(method = "glm")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.