当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R ggplot2 coord_polar 极坐标


极坐标系最常用于饼图,饼图是极坐标中的堆叠条形图。

用法

coord_polar(theta = "x", start = 0, direction = 1, clip = "on")

参数

theta

将角度映射到的变量(xy)

start

起点距 12 点钟的弧度偏移量。根据 direction 的值顺时针或逆时针应用偏移。

direction

1、顺时针; -1,逆时针

clip

是否应该将绘图裁剪到绘图面板的范围内?设置"on"(默认)表示是,设置"off"表示否。详情请参见coord_cartesian()

例子

# NOTE: Use these plots with caution - polar coordinates has
# major perceptual problems.  The main point of these examples is
# to demonstrate how these common plots can be described in the
# grammar.  Use with EXTREME caution.

#' # A pie chart = stacked bar chart + polar coordinates
pie <- ggplot(mtcars, aes(x = factor(1), fill = factor(cyl))) +
 geom_bar(width = 1)
pie + coord_polar(theta = "y")


# \donttest{

# A coxcomb plot = bar chart + polar coordinates
cxc <- ggplot(mtcars, aes(x = factor(cyl))) +
  geom_bar(width = 1, colour = "black")
cxc + coord_polar()

# A new type of plot?
cxc + coord_polar(theta = "y")


# The bullseye chart
pie + coord_polar()


# Hadley's favourite pie chart
df <- data.frame(
  variable = c("does not resemble", "resembles"),
  value = c(20, 80)
)
ggplot(df, aes(x = "", y = value, fill = variable)) +
  geom_col(width = 1) +
  scale_fill_manual(values = c("red", "yellow")) +
  coord_polar("y", start = pi / 3) +
  labs(title = "Pac man")


# Windrose + doughnut plot
if (require("ggplot2movies")) {
movies$rrating <- cut_interval(movies$rating, length = 1)
movies$budgetq <- cut_number(movies$budget, 4)

doh <- ggplot(movies, aes(x = rrating, fill = budgetq))

# Wind rose
doh + geom_bar(width = 1) + coord_polar()
# Race track plot
doh + geom_bar(width = 0.9, position = "fill") + coord_polar(theta = "y")
}
#> Loading required package: ggplot2movies

# }
源代码:R/coord-polar.R

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Polar coordinates。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。