二十三、R语言强大工具包ggplot绘图以外的那些事

来源:互联网 发布:淘宝哪家文具店好 编辑:程序博客网 时间:2024/05/23 12:20


ggplot是R语言最为强大的作图软件包,除了绘图本身的功能之外,还有很多绘图周边的细节需要掌握才能绘出一张完美的图像,本节我们围绕绘图周边来详细讲解

请尊重原创,转载请注明来源网站www.shareditor.com以及原始链接地址

画布定位

先看这张图:

> x <- c(1,2,3)> y <- c(1,3,4)> data <- data.frame(x,y)> ggplot(data, aes(x = x, y = y)) + geom_point()

如果我们希望让画布再大一些,让这三个点集中一些怎么办?我们可以调整画布的坐标范围,以下两种方法效果是一样的:

> ggplot(data, aes(x = x, y = y)) + geom_point() + expand_limits(x = c(0, 4), y = c(0, 5))> ggplot(data, aes(x = x, y = y)) + geom_point() + xlim(0, 4) + ylim(0, 5)

 

修改点的形状

我们可以画出多种点的形状

> ggplot(data,  aes(x, z)) + geom_point(aes(shape = y))

我们也可以把形状画成空心状的:

> ggplot(data,  aes(x, z)) + geom_point(aes(shape = y))+ scale_shape(solid = FALSE)

当然我们还可以调整点的大小:

> ggplot(data,  aes(x, z, size=z)) + geom_point(aes(shape = y))+ scale_shape(solid = FALSE)

 

各种标注方法

可以通过如下两种方式来添加title、x轴标签、y轴标签,效果是一样的,如下:

> ggplot(data,  aes(x,y)) + geom_point() + labs(title = "my title") +labs(x = "New x label") +labs(y = "New y label")> ggplot(data,  aes(x,y)) + geom_point() + ggtitle("my title") + xlab("New x label") + ylab("New y label")

我们还可以在某一个坐标位置写一句话

> ggplot(data,  aes(x,y)) + geom_point() + ggtitle("my title") + xlab("New x label") + ylab("New y label") + annotate("text", x = 2, y = 25, label = "Some text")

我们还可以在某一个范围画一个矩形来重点标注

> ggplot(data,  aes(x,y)) + geom_point() + ggtitle("my title") + xlab("New x label") + ylab("New y label") + annotate("rect", xmin = 1.75, xmax = 2.25, ymin = 18, ymax = 22, alpha = .2)

也可以在某一个范围画一条线段

> ggplot(data,  aes(x,y)) + geom_point() + ggtitle("my title") + xlab("New x label") + ylab("New y label") + annotate("segment", x = 1.75, xend = 2.25, y = 18, yend = 22, colour = "blue")

 

坐标系统

我们看下面这个例子:

> x <- c(1,2,3)> y <- c(10,20,30)> data <- data.frame(x, y)>  ggplot(data,  aes(x,y)) + geom_point()

请尊重原创,转载请注明来源网站www.shareditor.com以及原始链接地址

我们发现坐标上x和y是不等比例的,x的宽度1相当于y宽度10,怎么样可以让其等比例显示呢?

>  ggplot(data,  aes(x,y)) + geom_point() + coord_fixed(ratio = 1)

如果我们希望横过来显示,那么也可以这样让坐标轴对调:

>  ggplot(data,  aes(x,y)) + geom_point() + coord_flip()

有时我们希望把坐标变换成极坐标,如下:

>  ggplot(data,  aes(x,y)) + geom_point() +coord_polar(theta = "y")

 

分网格展示

分网格显示便于把不同组数据分离观察

按x的值分成多行

>  ggplot(data,  aes(x,y)) + geom_point() + facet_grid(x ~ .)

按y的值分成多列

>  ggplot(data,  aes(x,y)) + geom_point() + facet_grid(. ~ y)

按x和y分成网格

>  ggplot(data,  aes(x,y)) + geom_point() + facet_grid(x ~ y)

按照某一个类别分成多个网格

> x[1] 1 2 3> y[1] 10 20 30> z <- c("type1", "type2", "type1")> data <- data.frame(x, y, z)>  ggplot(data,  aes(x,y)) + geom_point() + facet_wrap(~z)

如果我们希望分两行展示,那么可以:

>  ggplot(data,  aes(x,y)) + geom_point() + facet_wrap(~z, nrow=2)

 

主题风格

我们可以选择不同的主题风格,像如下几种,当然还有很多:

> ggplot(data,  aes(x,y)) + geom_point() +  theme_light()> ggplot(data,  aes(x,y)) + geom_point() +  theme_dark()> ggplot(data,  aes(x,y)) + geom_point() +  theme_gray()

快速画图

如果我们需求比较简单,不想写那么长的命令,可以使用qplot来简单画图,它实际上也是自动转化成ggplot来画图的

比如我们要画一个y = x^2的曲线,那么可以这样:

> a <- -100:100> b <- a ^ 2> qplot(a, b)

0 0
原创粉丝点击