當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。