小果带你认识R数据可视化之ggplot坐标系(二)
小果在前面的文章中简单的介绍了一下六种常见坐标系和coord_cartesian函数与coord_fixed函数,今天小果将继续带大家探索坐标轴的其他函数。在这之前小果还是先强烈推荐一下自己的工具平台
(http://www.biocloudservice.com/home.html),
那么接下来就跟随着小果的步伐往下看吧。
1. 笛卡尔坐标系
1.1 coord_flip函数
> h <- ggplot(diamonds, aes(carat)) +
+ geom_histogram()
> h1 <- h + coord_flip()
> h2 <- h + coord_flip() + scale_x_reverse()
> plot_grid(h, h1, h2, labels = LETTERS[1:3], nrow = 3)
> df <- data.frame(x = 1:8, y = (1:8) ^ 2)
> p1 <- ggplot(df, aes(x, y)) +
+ geom_area()
> p2 <- p1 + coord_flip()
> plot_grid(p1, p2, labels = LETTERS[1:2], nrow = 2)
1.2 coord_trans函数
> dsamp <- diamonds[sample(nrow(diamonds), 800),]
> p1 <- ggplot(dsamp, aes(log10(carat), log10(price))) +
+ geom_point()
> p2 <- ggplot(dsamp, aes(carat, price)) +
+ geom_point() +
+ scale_x_log10() +
+ scale_y_log10()
> p3 <- ggplot(dsamp, aes(carat, price)) +
+ geom_point() +
+ coord_trans(x = "log10", y = "log10")
> plot_grid(p1, p2, p3, labels = LETTERS[1:3], nrow = 3)
> d <- subset(diamonds, carat > 0.6)
> p1 <- ggplot(d, aes(carat, price)) +
+ geom_point() ++ geom_smooth(method = "lm") +
+ scale_x_log10() +
+ scale_y_log10()
> p2 <- ggplot(d, aes(carat, price)) +
+ geom_point() +
+ geom_smooth(method = "lm") +
+ coord_trans(x = "log10", y = "log10")
> plot_grid(p1, p2, labels = LETTERS[1:2], nrow = 2)
> p1 <- ggplot(dsamp, aes(carat, price)) +
+ geom_point() +
+ geom_smooth(method = "lm")
> p2 <- ggplot(dsamp, aes(carat, price)) +
+ geom_point() +
+ geom_smooth(method = "lm") +
+ scale_x_log10() +
+ scale_y_log10() +
+ coord_trans(x = scales::exp_trans(10), y = scales::exp_trans(10))
> plot_grid(p1, p2, labels = LETTERS[1:2], nrow = 2)
2. 极坐标系
小果还是用举例的方式进行介绍吧:
> pie <- ggplot(mtcars, aes(x = factor(1), fill = factor(cyl))) +
+ geom_bar(width = 1)
> p1 <- pie + coord_polar(theta = "y")
> cxc <- ggplot(mtcars, aes(x = factor(cyl))) +
+ geom_bar(width = 1, colour = "white")
> p2 <- cxc + coord_polar()
> plot_grid(pie, p1, cxc, p2, labels = LETTERS[1:4], nrow = 2)
"y") p3 <- cxc + coord_polar(theta =
p4 <- pie + coord_polar()
plot_grid(p3, p4, labels = LETTERS[1:2], nrow = 1)
> df <- data.frame(
+ variable = c("xiao guo", "sheng xin"),
+ value = c(75, 25)+ )
> ggplot(df, aes(x = "", y = value, fill = variable)) +
+ geom_col(width = 1) +
+ scale_fill_manual(values = c("purple", "green")) +
+ coord_polar("y", start = pi / 3) +
+ labs(title = "XiaoGuo")
小果还提供思路设计、定制生信分析、文献思路复现;有需要的小伙伴欢迎直接扫码咨询小果,竭诚为您的科研助力!
定制生信分析
服务器租赁
扫码咨询小果
往期回顾
01 |
02 |
03 |
04 |