可视化 坐标系统
2018-06-17 本文已影响13人
柳叶刀与小鼠标
坐标系可能是ggplot2中最复杂的部分。 默认坐标系是笛卡尔坐标系,其中x和y位置独立地确定每个点的位置。 还有一些偶尔有用的其他坐标系统。
ggplot2可以通过coord_flip()切换x和y轴。例如,如果你想要水平箱形图。 这对长标签也很有用:很难让它们在x轴上不重叠的情况下适合。
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot()
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()


- coord_quickmap()为地图正确设置宽高比。
nz <- map_data("nz")
ggplot(nz, aes(long, lat, group = group)) +
geom_polygon(fill = "white", colour = "black")
ggplot(nz, aes(long, lat, group = group)) +
geom_polygon(fill = "white", colour = "black") +
coord_quickmap()


- coord_polar()使用极坐标。
bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
show.legend = FALSE,
width = 1
) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
bar + coord_flip()
bar + coord_polar()

