evoalgs-r-practise/intro/simple_plotting.R

48 lines
1.1 KiB
R
Raw Permalink Normal View History

2024-04-30 08:03:37 +00:00
# If you don't have the ggplot2 package installed, you can install it using:
# install.packages("ggplot2")
# Load necessary library
library(ggplot2)
# Creating some data for plotting
x <- 1:10
y <- x^2
# Base R Plotting
# ---------------
# Scatter Plot using base R
plot(x, y, main="Scatter Plot", xlab="X Axis", ylab="Y Axis", col="blue", pch=19)
# Line Plot using base R
plot(x, y, type="l", main="Line Plot", xlab="X Axis", ylab="Y Axis", col="red")
# Histogram using base R
hist(y, main="Histogram", xlab="Values", col="green", breaks=5)
# Advanced Plotting with ggplot2
# ------------------------------
# Data frame for ggplot2
data <- data.frame(x, y)
# Scatter Plot using ggplot2
ggplot(data, aes(x=x, y=y)) +
geom_point() +
ggtitle("Scatter Plot with ggplot2") +
xlab("X Axis") +
ylab("Y Axis")
# Line Plot using ggplot2
ggplot(data, aes(x=x, y=y)) +
geom_line(color="red") +
ggtitle("Line Plot with ggplot2") +
xlab("X Axis") +
ylab("Y Axis")
# Histogram using ggplot2
ggplot(data, aes(x=y)) +
geom_histogram(bins=5, fill="green", color="black") +
ggtitle("Histogram with ggplot2") +
xlab("Values")