[英]ggplot2 theme to color inside of polar plot
我正在使用ggplot2
繪制具有極坐標的圖。
我想將圓內的背景色設置為與圓外的背景色不同的顏色。 (原因是我要將繪圖轉換為柵格並使圓外的區域透明。)
我ggplot
是plot.background
的ggplot
theme
屬性。 但是我似乎找不到找到使panel.background
只在情節內部繪畫的方法。 有沒有element_circle
模擬element_rect
。
有人可以建議嗎?
這是一個最小可重復的示例:
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"))
提前致謝!
這是一種怪誕的方法:在每個因子級別上創建一個包含最小值和最大值的虛擬數據幀,然后使用它繪制填充該空間的條。 使用極坐標設置width = 1
將用於制作餅圖-在這里它做同樣的事情,但是您並沒有在餅形楔子之間進行區分。
您實際上可能並不想使用x
的確切最大值; 可以根據需要添加一些填充,但這應該是一個開始。
如果您正在處理最小值為0或高於最大值的數據,則無需像我一樣重復數據-這是我弄清楚讓條形圖從0堆疊到最大值的最佳方法,即為正,從0到最小值,為負。 最初看起來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")
同樣值得注意的是, ggforce
具有geom_circle
,但是我不知道它如何與極坐標一起工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.