Creating advanced plots in R can greatly enhance data visualization. Here are some examples of how to create advanced plots in R:
1. Boxplot:
You can use the `boxplot()` function to create a boxplot of a numeric variable. For example:
x <- c(1, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5) boxplot(x) # Create a boxplot of x
2. Heatmap:
You can use the `heatmap()` function or the `ggplot2` package to create a heatmap of a matrix. For example:
x <- matrix(runif(100), ncol=10) heatmap(x) # Create a heatmap of x
Alternatively, using `ggplot2`:
library(ggplot2) library(reshape2) x <- matrix(runif(100), ncol=10) df <- melt(x) ggplot(df, aes(x=Var2, y=Var1, fill=value)) + geom_tile() # Create a heatmap of x using ggplot2
3. Treemap:
You can use the `treemap()` function or the `ggplot2` package with the `treemapify` package to create a treemap of a hierarchical dataset. For example:
library(treemap) data(GNI2014) treemap(GNI2014) # Create a treemap of the GNI2014 dataset Alternatively, using `ggplot2` and `treemapify`:
library(ggplot2)
library(treemapify)
data(GNI2014)
df <- as.data.frame(GNI2014)
treemapify(df, path=c("continent", "iso3"), vSize="population", vColor="GNI") + geom_treemap() # Create a treemap of the GNI2014 dataset using ggplot2 and treemapify
4. Scatterplot matrix: You can use the `pairs()` function to create a scatterplot matrix of multiple variables. For example:
df <- data.frame(x1=rnorm(100), x2=rnorm(100), x3=rnorm(100)) pairs(df) # Create a scatterplot matrix of x1, x2, and x3
Alternatively, using `ggplot2`:
library(ggplot2)
library(reshape2)
df <- data.frame(x1=rnorm(100), x2=rnorm(100), x3=rnorm(100)) df_melted <- melt(df) ggplot(df_melted, aes(x=variable, y=value)) + geom_point() + facet_grid(rows="variable", cols="variable") # Create a scatterplot matrix of x1, x2, and x3 using ggplot2