常用的可视化资源导航

持续更新:R 中常用的可视化资源

Awesome Website


ggplot2

ggplot2 Plotting System

  • ggplot() 中控制的是总体设置,比如 ggplot() 中的 color 决定了所有 geom 的颜色,而 geom 中的设置只是针对各个图
  • 凡是涉及到与变量相关的设置,必须用 aes()

Geoms

geom_bar (x is discrete)
  • Bar and Line Graph_Cookbook for R/)
  • Bars, rectangles with bases on x-axis
  • 使用 ggplot2 绘制条形图
  • 按照y轴从大到小的顺序排列bar的分布:aes(x=reorder(x_name, -value), y=value, fill=name
  • geom_bar(stat="identity", position=position_dodge(0.7)) 默认的 position 是堆叠 stack,百分比堆叠是 position="fill", 而按政府值区分开是 position="identity"
  • 使用明细数据计数之后绘制条形图:ggplot(data, aes(x=gender)) + geom_bar(stat="count", width=.3, col="orange") stat 的默认值是 count
  • 对于汇总好的数据:geom_bar(stat = "identity")
  • 添加标签:geom_text(stat='count', aes(label=..count..), vjust=-0.5, hjust=0.5, size=3, colour="orange") 如果是堆叠式,则加入 position=position_stack()
geom_histogram (x is continuous)
  • Make a histogram with ggplot2
  • qplot(chol$AGE, geom="histogram", binwidth = 5, main = "Histogram for Age", xlab = "Age", fill=I("blue"), col=I("red"), alpha=I(.2), xlim=c(20,50))
  • ggplot(data, aes(x=duration)) + geom_histogram(binwidth=60)
  • How to make a histogram
  • 添加计数的标签:+ stat_bin(binwidth=0.5, geom="text", aes(label=..count..), vjust=-0.5, size=3)
geom_density
geom_line
geom_point
geom_boxplot
  • 画箱形图,x 为分组变量,y 为值:ggplot(result_all, aes(x=factor(cluster), y=WAPE_min)) + geom_boxplot()
  • 添加均值:stat_summary(fun.y=mean, colour="blue", geom="point", size=2) + stat_summary(fun.y=mean, colour="red", geom="text", vjust=0.3, hjust=-0.5, size=3, aes( label=round(..y.., digits=2)))
geom_text
  • 添加标签,避免文字重叠:geom_text(aes(label=value), position=position_dodge(width=0.9), vjust=-0.25)
  • geom_text(aes(label = Frequency), size = 3, hjust = 0.5, vjust = 3, position = "stack") stack 表示堆叠显示
geom_vline
  • 添加一条垂直线:geom_vline(xintercept=as.numeric(as.Date("1959-12-01")), linetype=2)
geom_hline
  • 添加一条水平线:geom_hline(yintercept=200)
geom_ribbon
  • 作缎带图:geom_ribbon(aes(ymin=LL, ymax=UL), fill="grey", alpha=0.5)
geom_rug
  • 向散点图添加坐标轴边际地毯:geom_rug(position = 'jitter', size = 0.1)
geom_label_repel && geom_text_repel
  • 来自 ggrepel 包,添加标签,geom_label_repel(aes(label=method),size=3)
geom_dl
  • 来自 directlabels 包,在线的两端标注分类名称,geom_dl(aes(label = method), method = list(dl.combine("first.points", "last.points"), cex = 0.8))
  • 自动选择最优位置标注:method="smart.grid"
annotate
1
2
3
4
5
6
7
8
9
10
# 添加文字
annotate("text", x = 4, y = 25, label = "Some text")
annotate("text", x = 2:5, y = 25, label = "Some text")
annotate("text", x = 2:3, y = 20:21, label = c("my label", "label 2"))
# 添加矩形框
annotate("rect", xmin = 3, xmax = 4.2, ymin = 12, ymax = 21, alpha = .2)
# 根据起始点画直线
annotate("segment", x = 2.5, xend = 4, y = 15, yend = 25, colour = "blue")

Stats

stat_smooth
  • 向散点图添加拟合线:stat_smooth(method="lm / loess")

Facets

facet_wrap
  • facet_wrap(~group_variable,scales='free', nrow=2)
    • scales='free' 表示允许不同的 facet 有不同的 x/y axis limit,如果只允许 x axis,则 scale=‘free_x’
    • nrow 设置 plot 的行数
facet_grid
  • 在同一个 facet 上作不同类型的图形:ggplot() + geom_line(data=temp[measure %in% c("MAPE_in","MAPE_out","CV")], aes(x=atc_class,y=value,group=measure,color=measure), size=1) + geom_bar(data=temp[measure=="volume"],aes(atc_class,value),stat="identity") + facet_grid(group~.,scales="free_y")

Scales

  • Aesthetic mapping (i.e., with aes()) only says that a variable should be mapped to an aesthetic. It doesn’t say how that should happen.
    scale_fill_brewer & scale_fill_manual
  • 颜色填充/)
    • scale_fill_brewer(palette="Set1")
    • scale_fill_manual(values=c("red", "blue", "green"))
scale_shape_manual
  • 自动指定点形状的上限是6,超过则需要手动指定点的形状:scale_shape_manual(values=1:10)
scale_color_brewer & scale_color_manual
  • 设置连续颜色变化:scale_colour_gradient(low = 'lightblue', high = 'darkblue')
scale_size
  • scale_size_continuous(breaks = c(100,150,200,300,350,400), guide = guide_legend())
  • 连续变量的大小与形状的大小成比例:scale_size_area(max_size = 10)
scale_x_continuous
  • 改变X轴的坐标间隔:scale_x_continuous(breaks=seq(0, max(abc$cum_volume), by=max(abc$cum_volume)/5))
scale_size_manual
  • 将大小分成两组,并手动改变线条粗细:geom_line(aes(col=method, size=(method=="volume"))) + scale_size_manual(values = c(1, 2), name = "", labels = c())
Point Shape
Color Names

Themes

  • 更改 facet 中坐标轴 label 文字的大小:theme(strip.text.y = element_text(size=8))
  • 文字竖向:theme(axis.text.x=element_text(angle=45,hjust=1,vjust=1))
  • 标题:labs(list(title = “Cluster Result”, x = “Clustering Variables”, y = “Centroid”))
  • 标题居中:theme(plot.title = element_text(hjust = 0.5))
  • 将 Theme 的各元素设置为空:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    theme(axis.line=element_blank(),
    axis.text.x=element_blank(),
    axis.text.y=element_blank(),
    axis.ticks=element_blank(),
    axis.title.x=element_blank(),
    axis.title.y=element_blank(),
    legend.position="none",
    panel.background=element_blank(),
    panel.border=element_blank(),
    panel.grid.major=element_blank(),
    panel.grid.minor=element_blank(),
    plot.background=element_blank())
  • 白色背景:theme_bw()

  • 设置图标题:ggtitle(paste0("BSTS Holdout MAPE = ", round(100*MAPE,2), "%"))
  • 更改标题文字大小:theme(plot.title = element_text(size=10))
  • 转换坐标轴:coord_flip()

Other

Legend
  • Legend Guide
  • Guide:legend 文字更改或者分类维数合并参见 guide
  • Legend 中的分类变量排序:ts$category <- factor(ts$category, levels=c("vegetable","fruit","meat","total"))
  • 修改 legend 的标题:scale_color_discrete(guide = guide_legend(title = "Cluster")),注意 scale 要对应 legend 的维度!比如 aes(color= ) 的 legend 要对应 scale_color_discrete!
Save plot to disk
  • 注意 for 循环内的 plot 要加上 print() 才能输出
    1
    2
    3
    png(filename=paste('./png/',i,'.png',sep=''))
    print(qplot(na.omit(result_boot[,i]), geom='histogram', binwidth=0.1, xlim=c(-20,20), col='red'))
    dev.off()

ggfortify


corrplot


Time Series Plot

  • plot.ts():时序图
  • ts.plot():在同一张图上作多个时序图,ts.plot(ts,train_fit,test_fit, gpars = list(col=c("black", "blue", "red")))
  • forecast package
    • tsdisplay() :时序图+ACF+PACF
    • plot.forecast():作预测图

Table Plot

1
2
3
4
library(gridExtra)
library(grid)
d <- head(iris[,1:3])
grid.table(d)

Plotly