R package: ggplot2.

Data from Hadle Wickham ‘ggplot2 Elegant Graphics for Data Analysis’.

1
2
3
4
5
6
7
Data
Aesthetics
Geometries #几何对象
Facets
Statistics
Coordinates #坐标系
Themes

Prerequisites

1
2
3
install.packages("ggplot2")

library(ggplot2)

Dataset

1
2
3
data(mpg)
# if data is from outside: data <- read.table(file, header=T, row.names=NULL, sep=",)
data(economics)

dataset

Basic Plot

1. scatter

1
2
ggplot (mpg, aes (x = displ, y = hwy)) +
geom_point()

scatter

2. scatter add line

1
2
3
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(method= "lm")

scatter2

3. scatter add color

1
2
ggplot(mpg, aes (displ, hwy, colour=class)) +
geom_point()

scatter3

4. scatter and wrap

1
2
3
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~class)

scatter4

5. boxplot

1
2
ggplot(mpg, aes(drv, hwy)) +
geom_boxplot()

boxplot

6. violin plot

1
2
ggplot(mpg, aes(drv, hwy)) +
geom_violin()

violin

7. overlap

1
2
3
ggplot(mpg, aes(drv, hwy)) +
geom_violin(trim=FALSE, fill='orange', color='red') +
geom_boxplot(width=0.1) + theme_minimal()

overlap

8. histogram

1
2
ggplot(mpg, aes(hwy)) +
geom_histogram()

histogram

9. freqpoly

1
2
ggplot(mpg, aes(hwy)) +
geom_freqpoly()

freqpoly

10. bar

1
2
ggplot(mpg, aes(manufacturer)) +
geom_bar()

barplot

11. timeplot

1
2
ggplot(economics, aes(date, unemploy / pop)) +
geom_line()

timeplot

1
2
ggplot(economics, aes(date, uempmed)) +
geom_line()

timeplot2

12. path

1
2
3
ggplot(economics, aes(unemploy / pop, uempmed)) +
geom_path() +
geom_point()

path

13. contour

1
2
ggplot(faithfuld, aes(eruptions, waiting)) +
geom_contour(aes(z = density, colour = ..level..))

contour

14. raster

1
2
ggplot(faithfuld, aes(eruptions, waiting)) +
geom_raster(aes(fill = density))

raster

Fix with alpha

1
2
3
4
norm <- ggplot(df, aes(x, y)) + xlab(NULL) + ylab(NULL)
norm + geom_point (alpha = 1 / 3)
norm + geom_point (alpha = 1 / 5)
norm + geom_point (alpha = 1 / 10)

Setting theme

1
2
3
4
different theme
theme_classic()
theme_minimal()
theme_economist()+ scale_colour_economist()

ggplot2 extensions

  • ggthemes Extra Themes, Scales and Geoms for ‘ggplot2’.
  • ggpubr Based Publication Ready Plots.
  • patchwork Expands the API, provide mathematical operators for combining multiple plots.
  • ggridges Provide a convenient way of visualizing changes in distributions over time or space.
  • ggdendro Create Dendrograms and Tree Diagrams Using ‘ggplot2’.
  • ggmap Spatial Visualization with ggplot2.
  • ggcorrplot Visualize easily a correlation matrix using ‘ggplot2’.
  • ggiraph Create interactive ‘ggplot2’ graphics using ‘htmlwidgets’.
  • GGally Extension to ‘ggplot2’,by adding several functions to reduce the complexity of combining geometric objects with transformed data.
  • ggalt Extra Coordinate Systems, Geoms and Statistical Transformations for ‘ggplot2’.
  • ggforce Accelerating ‘ggplot2’.
  • ggrepel Automatically Position Non-Overlapping Text Labels with ‘ggplot2’.
  • gganimate A Grammar of Animated Graphics.
  • ggradar Create radar charts.
  • ggraph 绘制网络状、树状等特定形状的图形
  • ggstance 实现常见图形的横向版本
  • ggpmisc 光生物学相关扩展
  • ggbio 提供基因组学数据图形
  • geomnet 绘制网络状图形
  • ggExtra 绘制图形的边界直方图
  • plotROC 绘制交互式ROC曲线图
  • ggspectra 绘制光谱图
  • ggnetwork 网络状图形的geoms
  • ggTimeSeries 时间序列数据可视化
  • ggtree 树图可视化
  • ggtern 绘制三元图
  • ggseas 季节调整工具
  • ggenealogy 浏览和展示系谱学数据

In summary, more knowledge by systematically learning R related course.