ggplot2绘图学习:单变量+绘图背景
R绘图往期回顾:
R绘图:ggeconodist,基于ggplot2的另类箱图
为了系统的学习ggplot2绘图,我们先从单变量的绘图开始。
使用数据集mpg,展示ggplot2绘制单变量图形
连续型单变量
对于一个连续变量:
面积图geom_area()
密度图geom_density()
点图geom_dotplot()
频率多边图geom_freqpoly()
直方图geom_histogram()

#首先绘制一个图层a
a <- ggplot(mpg, aes(hwy))
面积图
a + geom_area(stat = "bin")

改变透明度
a + geom_area(stat = "bin",alpha=0.5)

改变颜色
a + geom_area(stat = "bin",alpha=0.5,color='red',fill='blue')

密度图
a + geom_density()

点图
a + geom_dotplot()

频率多边图
a + geom_freqpoly()

直方图
a + geom_histogram(binwidth = 5)

一个离散变量

a + geom_bar()

改变绘图背景
我们可以通过下面的函数来改变图层的背景
p1 + theme_gray() # 默认
p1 + theme_bw()
p1 + theme_linedraw()
p1 + theme_light()
p1 + theme_dark()
p1 + theme_minimal()
p1 + theme_classic()
p1 + theme_void()

默认
p1 <- ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
labs(title = "Fuel economy declines as weight increases")
p1

去掉灰色背景
p1 + theme_bw()

增加刻度线
p1 + theme_linedraw()

刻度线变浅
p1 + theme_light()

暗色背景
p1 + theme_dark()

不要边框
p1 + theme_minimal()

经典的画法
p1 + theme_classic()

啥都不要了
p1 + theme_void()

赞 (0)