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)
|
Basic Plot
1. scatter
1
2
|
ggplot (mpg, aes (x = displ, y = hwy)) +
geom_point()
|
2. scatter add line
1
2
3
|
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(method= "lm")
|
3. scatter add color
1
2
|
ggplot(mpg, aes (displ, hwy, colour=class)) +
geom_point()
|
4. scatter and wrap
1
2
3
|
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~class)
|
5. boxplot
1
2
|
ggplot(mpg, aes(drv, hwy)) +
geom_boxplot()
|
6. violin plot
1
2
|
ggplot(mpg, aes(drv, hwy)) +
geom_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()
|
8. histogram
1
2
|
ggplot(mpg, aes(hwy)) +
geom_histogram()
|
9. freqpoly
1
2
|
ggplot(mpg, aes(hwy)) +
geom_freqpoly()
|
10. bar
1
2
|
ggplot(mpg, aes(manufacturer)) +
geom_bar()
|
11. timeplot
1
2
|
ggplot(economics, aes(date, unemploy / pop)) +
geom_line()
|
1
2
|
ggplot(economics, aes(date, uempmed)) +
geom_line()
|
12. path
1
2
3
|
ggplot(economics, aes(unemploy / pop, uempmed)) +
geom_path() +
geom_point()
|
13. contour
1
2
|
ggplot(faithfuld, aes(eruptions, waiting)) +
geom_contour(aes(z = density, colour = ..level..))
|
14. raster
1
2
|
ggplot(faithfuld, aes(eruptions, waiting)) +
geom_raster(aes(fill = density))
|
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.