简体   繁体   English

ggplot2主题为极坐标内的颜色

[英]ggplot2 theme to color inside of polar plot

I am using ggplot2 to make plots with polar coordinates. 我正在使用ggplot2绘制具有极坐标的图。

I would like to set the background color inside of the circle to be a different color than the background color outside of the circle. 我想将圆内的背景色设置为与圆外的背景色不同的颜色。 (The reason is that I'm going to convert the plot to a raster and make the area outside the circle transparent.) (原因是我要将绘图转换为栅格并使圆外的区域透明。)

The ggplot theme attribute for plot.background does what I expect. ggplotplot.backgroundggplot theme属性。 But I can't seem to find a way to make panel.background only paint inside the plot. 但是我似乎找不到找到使panel.background只在情节内部绘画的方法。 There's no element_circle analog for element_rect . 有没有element_circle模拟element_rect

Can anyone advise? 有人可以建议吗?

Here's a minimally reproducible example: 这是一个最小可重复的示例:

data.frame(
  x = rnorm(1000), 
  spoke = factor(sample(1:6, 1000, replace=T))
) %>% 
  ggplot(aes(x = spoke, fill=spoke, y = x)) +
  geom_violin() +
  coord_polar() +
  theme(
    plot.background = element_rect(fill = "darkblue"),
    panel.background = element_rect(fill = "lightblue",
                                    colour = "lightblue"))

Thanks in advance! 提前致谢!

Here's a hacky way: make a dummy data frame of the minimum and maximum values at each of the factor levels, then use it to draw bars filling up that space. 这是一种怪诞的方法:在每个因子级别上创建一个包含最小值和最大值的虚拟数据帧,然后使用它绘制填充该空间的条。 Setting width = 1 with polar coordinates would be used to make a pie chart—here it does the same thing, but you aren't distinguishing between pie wedges. 使用极坐标设置width = 1将用于制作饼图-在这里它做同样的事情,但是您并没有在饼形楔子之间进行区分。

You probably don't actually want to work with the exact maximum of x ; 您实际上可能并不想使用x的确切最大值; it would make sense to add some amount of padding as you see fit, but this should be a start. 可以根据需要添加一些填充,但这应该是一个开始。

If you were working with data that had a minimum at 0 or somewhere above it, you wouldn't need to repeat the data like I did—that was the best way I could figure out to get bars stacking from 0 to the maximum, which is positive, and from 0 to the minimum, which is negative. 如果您正在处理最小值为0或高于最大值的数据,则无需像我一样重复数据-这是我弄清楚让条形图从0堆叠到最大值的最佳方法,即为正,从0到最小值,为负。 geom_rect initially seemed like it made more sense, but I couldn't get it to stretch all the way around to close the circle. 最初看起来geom_rect似乎更有意义,但我无法让它一直伸展到整个圆圈。

library(tidyverse)

set.seed(1234)
df <- data.frame(
  x = rnorm(1000), 
  spoke = factor(sample(1:6, 1000, replace=T))
)
bkgnd_df <- data.frame(spoke = factor(rep(1:6, 2)), 
                      x = c(rep(min(df$x), 6), rep(max(df$x), 6)))

bkgnd_df
#>    spoke         x
#> 1      1 -3.396064
#> 2      2 -3.396064
#> 3      3 -3.396064
#> 4      4 -3.396064
#> 5      5 -3.396064
#> 6      6 -3.396064
#> 7      1  3.195901
#> 8      2  3.195901
#> 9      3  3.195901
#> 10     4  3.195901
#> 11     5  3.195901
#> 12     6  3.195901

ggplot(df, aes(x = spoke, y = x)) +
  geom_col(data = bkgnd_df, width = 1, fill = "skyblue") +
  geom_violin(aes(fill = spoke)) +
  coord_polar(theta = "x")

Also worth noting that ggforce has a geom_circle , but I don't know how it would work with polar coordinates. 同样值得注意的是, ggforce具有geom_circle ,但是我不知道它如何与极坐标一起工作。

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

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