小果带你认识R数据可视化之ggplot坐标系(二)
/1_r0Gd7iaumIGMiaia97aHFV15nrpdzw.png)
小果在前面的文章中简单的介绍了一下六种常见坐标系和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)
/4_dxL94zUPZhsWFzOtP88BVcu3MjrGQg.png)
> 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)
/5_fjHoLbMcQ7gUibmRRT2Ez1IDlcC3Zw.png)
> 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)
/6_uNg1I60bK7Sy22ees5aTSlP3A8ib2g.png)
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)
p3 <- cxc + coord_polar(theta = "y")p4 <- pie + coord_polar()plot_grid(p3, p4, labels = LETTERS[1:2], nrow = 1)
/8_ZBzEcwGD6eOiakGDgQ6hbFcvt4rdqw.png)
> 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")
/9_ndQXXaPXpMwGXgOaQ1oTH9svLNXUwQ.png)
小果还提供思路设计、定制生信分析、文献思路复现;有需要的小伙伴欢迎直接扫码咨询小果,竭诚为您的科研助力!
/10_B2jkicacLzPjw4xMSqVvoucZjonVDA.jpg)
定制生信分析
服务器租赁
扫码咨询小果
/11_7TvAyUibd1TPhJmZfialJtv6tu2SEg.png)
/12_9N02aBaau07RufXeXRLmvAHsrFZxtw.jpg)
往期回顾
|
01 |
|
02 |
|
03 |
|
04 |