繁体   English   中英

ggplot2:如何在角度为 0 度的圆形 360 度条形图中显示数据点?

[英]ggplot2: How to show a datapoint in a circular 360 degree bar plot with an angle of 0 degree?

我得到了一个圆形的 360 度条形图,我需要手动指定每个条形的角度(数字 x 轴)。 一般来说,除了角度为 0 的数据点之外,绘图工作正常。 360度。 该行被删除且不绘制,即使条宽为 1 也不绘制。

为什么会这样,如何绘制中心值为 0 且条宽为 20 的条(从 -10 到 350 度到 10 度)?

谢谢!

MWE:

library(tidyverse)

# dummy data
data <- data.frame(
  variable = c(0, 90, 180, 270), 
  value = c(50, 100, 150, 200))

# plot
ggplot(data, aes(x = variable, y = value, fill = factor(variable))) +
  geom_col(width = 20)  + # Even with a widh of 1 the first datapoint is removed from the plot.
  scale_x_continuous(breaks = seq(from=0, to=359, by=10),  limits=c(0, 360)) +
  coord_polar(start = 0)

我看不到一个简单的通用解决方案来绘制跨越极坐标环绕点的条形图,但很容易移动限制和起点以获得所需的效果:

ggplot(data, aes(x = variable, y = value, fill = factor(variable))) +
  geom_col(width = 20)  + 
  scale_x_continuous(breaks = seq(0, 359, by = 10), limits = c(-10, 350)) +
  coord_polar(start = -pi/18, clip = "off")

在此处输入图片说明

显然,如果您尝试在 350 到 359 度之间而不是在 0 到 9 度之间绘制条形图,这会导致问题,但假设您不希望任何条形重叠,我猜这不是问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM