library(ggplot2)
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(ggplot2)
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
data(mtcars)
head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
data(iris)
data(ToothGrowth)
head(ToothGrowth)
len supp dose
1 4.2 VC 0.5
2 11.5 VC 0.5
3 7.3 VC 0.5
4 5.8 VC 0.5
5 6.4 VC 0.5
6 10.0 VC 0.5
ggplot(iris, aes(x = Sepal.Length)) + geom_histogram(binwidth = 0.3, fill = "skyblue",color = "black") + facet_wrap(~ Species) +
labs(title = "Distribution of sepal length by Species", x = "Sepal Length (cm)",
y = "Frequency") +
theme_minimal()
<- function(data, numeric_var, group_var, fill = TRUE) {
plot_density_by_group ggplot(data, aes_string(x = numeric_var, color = group_var, fill = group_var)) +
geom_density(alpha = if (fill) 0.4 else 0) + labs( title = paste("Density Plot of", numeric_var, "by", group_var), x = numeric_var,
y = "Density" ) +
theme_minimal() }
plot_density_by_group(iris, "Sepal.Length", "Species")
Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
ℹ Please use tidy evaluation idioms with `aes()`.
ℹ See also `vignette("ggplot2-in-packages")` for more information.
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_boxplot( notch = TRUE,
notchwidth = 0.6,
staplewidth = 0.4,
fill = "red",
color = "skyblue" ) +
labs( title = "Box Plot for Iris",
x = "Species",
y = "Sepal Length" ) +
theme_minimal()
ggplot(iris, aes(x = Species , y = Sepal.Length ,
fill = Species))+
geom_violin(trim = FALSE,
alpha=0.6,
color= "black")+
labs(title = "Distributions of petal length by iris Species", x = "Species",
y = "Petal.Length")+
theme_minimal(base_size = 14)
data(mtcars)
<- cor(mtcars)
cor_matrix <- as.data.frame(as.table(cor_matrix))
cor_df head(cor_df)
Var1 Var2 Freq
1 mpg mpg 1.0000000
2 cyl mpg -0.8521620
3 disp mpg -0.8475514
4 hp mpg -0.7761684
5 drat mpg 0.6811719
6 wt mpg -0.8676594
# Convert dose to a factor for grouping
$dose <- as.factor(ToothGrowth$dose) # Plot with defined binwidth
ToothGrowthggplot(ToothGrowth, aes(x = dose, y = len, color = supp)) + geom_dotplot( binaxis = 'y', stackdir = 'center', position = position_dodge(width = 0.8), dotsize = 0.6, binwidth = 1.5 # Controls spacing of dots on y-axis
+ labs( title = "Dot Plot of Tooth Length by Dose and Supplement Type", x = "Dose (mg/day)", y = "Tooth Length", color = "Supplement Type" ) + theme_minimal() )
ggplot(cor_df, aes(x = Var1, y = Var2, fill = Freq)) +
geom_tile(color = "white") + # Draw tile borders
scale_fill_gradient2(
low = "blue", mid = "white", high = "red",
midpoint = 0, limit = c(-1, 1),
name = "Correlation"
+
) geom_text(aes(label = round(Freq, 2)), size = 3) + # Show values
theme_minimal() +
labs(
title = "Correlation Matrix (mtcars)",
x = "", y = ""
+
) theme(axis.text.x = element_text(angle = 45, hjust = 1))