简体   繁体   English

带有极坐标的条形图,调整以在ggplot中逐渐变细

[英]Bar plot with polar coordinates, adjust for tapering in ggplot

I want to create a scoring plot, like those you often see in role-playing games, with positive and negative scores, using ggplot. 我想使用ggplot创建一个得分图,就像您在角色扮演游戏中经常看到的那样,得分为正数和负数。

To do this I use geom_col and coord_polar . 为此,我使用geom_colcoord_polar The problem is that the bars are not rectangles but trapezoids, as expected since the whole plotting space coordinates system is deformed. 问题在于,由于整个绘图空间坐标系统都发生了变形,因此条形图不是矩形而是梯形,这是预期的。

在此处输入图片说明

Is it possible to adjust for this in order to have proper uniform bars (and add some space at the center of the plot to avoid overlapping)? 是否可以对此进行调整以具有适当的均匀条(并在图的中心添加一些空间以避免重叠)?

Bonus request: instead of a circular grid, is it possible to have a polygonal one, based on the number of values on the x-axis? 奖励请求:根据x轴上的值的数量,是否可以有一个多边形的网格而不是圆形的网格?

You can use line segments instead of bars. 您可以使用线段代替条形图。 The thickness would then depend on the size you choose, rather than the coordinate system: 然后,厚度将取决于您选择的尺寸,而不是坐标系:

# sample dataset
set.seed(333)
n.dim = 9
df <- data.frame(
  x = paste("attribute", seq(1, n.dim), sep = "."),
  score = rnorm(n.dim)
)

ggplot(df,
       aes(x = x, y = score, 
           xend = x, yend = 0,
           col = score >= 0)) +
  geom_segment(size = 15) +                 # adjust size for different bar thickness
  scale_y_continuous(expand = c(0.1, 0)) +  # increase space at plot centre
  coord_polar() +
  theme_minimal() +
  theme(legend.position = "none",
        axis.title = element_blank(),
        axis.text.y = element_blank(),
        panel.grid.major.x = element_blank())

情节

I think a polygon grid (rather than a circular one) would be more harder to interpret, as distance from plot centre no longer has a one-to-one relationship with the y-axis values. 我认为多边形网格(而不是圆形网格)将更难以解释,因为距绘图中心的距离不再与y轴值具有一对一的关系。 (See illustration below) It's probably possible to hack something that resembles a polygon grid in ggplot, but I've yet to see any such implementation myself. (请参见下面的插图)可能有可能破解ggplot中类似于多边形网格的内容,但是我自己还没有看到任何这种实现。

情节

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

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