可以在ggplot geom_density_ridges中可视化群组吗?

huangapple go评论61阅读模式
英文:

Is it possible to visualise groups within ggplot geom_density_ridges?

问题

我已经建立了一个漂亮的图表,显示了每周每天的日常时间模式。

在每周每天的曲线中,我想通过分组来可视化数据。分组应该根据列 "ID" 进行,我可以将其设置为 True/False 或 A/B 或任何最适合的值。理想情况下,每个图表应该由两种不同的颜色组成,清楚地显示哪个组在某些高峰期间负责最多。这意味着数据点的一组位于底部,另一组叠在其上。

color = ID, fill = ID 没有起作用。

有没有办法实现这个目标?

  ggplot(aes(x = weekdaytime, y = weekday, fill = weekday, group = weekday, height = ..count..)) +
  geom_density_ridges_gradient(stat = "density", scale = 2, rel_min_height = 0.01, bw = 300 ) +
  scale_fill_viridis(name = "", option = "C") +
  scale_x_datetime(date_breaks = "2 hour", date_labels = "%H", 
                   limits = as.POSIXct(strptime(c(paste(Sys.Date(), "00:00", sep=" "),
                                                  paste(Sys.Date(), "24:00", sep=" ")), 
                                                format = "%Y-%m-%d %H:%M"))) +
  scale_y_discrete(limits=c("Monday", "Tuesday", "Wednesday", "Thursday", 
                            "Friday", "Saturday", "Sunday"))+
  theme(
    legend.position="none",
    panel.spacing = unit(0.1, "lines"),
    panel.grid.major = element_line(colour = "grey"),
    panel.grid.minor = element_line(colour = "grey"),
    
    panel.background = element_blank(),
    strip.text.x = element_text(size = 7)
  )  ```

<details>
<summary>英文:</summary>

I have built a nice graph that shows daily time patterns per day of the week. 

Within each curve for each day of the week I would like to visualise the data by group. Grouping should occur from the column &quot;ID&quot; that I can either set to True/False or A/B or whatever is best. Ideally each graph would then consist of two different colours, showing clearly which group is mostly responsible for certain peaks. Meaning that One group of data points is at the bottom and the other stacked on top.   

```color = ID, fill = ID ``` did not work. 

Any idea how i could achieve this? 



```graph %&gt;% 
  ggplot(aes(x = weekdaytime, y = weekday, fill = weekday, group = weekday, height = ..count..)) +
  geom_density_ridges_gradient(stat = &quot;density&quot;, scale = 2, rel_min_height = 0.01, bw = 300 ) +
  scale_fill_viridis(name = &quot;&quot;, option = &quot;C&quot;) +
  scale_x_datetime(date_breaks = &quot;2 hour&quot;, date_labels = &quot;%H&quot;, 
                   limits = as.POSIXct(strptime(c(paste(Sys.Date(), &quot;00:00&quot;, sep=&quot; &quot;),
                                                  paste(Sys.Date(), &quot;24:00&quot;, sep=&quot; &quot;)), 
                                                format = &quot;%Y-%m-%d %H:%M&quot;))) +
  scale_y_discrete(limits=c(&quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, 
                            &quot;Friday&quot;, &quot;Saturday&quot;, &quot;Sunday&quot;))+
  theme(
    legend.position=&quot;none&quot;,
    panel.spacing = unit(0.1, &quot;lines&quot;),
    panel.grid.major = element_line(colour = &quot;grey&quot;),
    panel.grid.minor = element_line(colour = &quot;grey&quot;),
    
    panel.background = element_blank(),
    strip.text.x = element_text(size = 7)
  )  ```

</details>


# 答案1
**得分**: 1

If you introduce another factor into your `aes()` (e.g. color or shape) it will change the number of density curves, e.g. using the [palmerpenguins dataset](https://allisonhorst.github.io/palmerpenguins/):

``` r
library(tidyverse)
library(palmerpenguins)
library(ggridges)

penguins %>%
  na.omit() %>%
  ggplot(aes(x = body_mass_g, y = species)) +
  geom_density_ridges(
    alpha = .2, point_alpha = 1, jittered_points = TRUE
  )
#> Picking joint bandwidth of 153

可以在ggplot geom_density_ridges中可视化群组吗?

penguins %>%
  na.omit() %>%
  ggplot(aes(x = body_mass_g, y = species)) +
  geom_density_ridges(
    aes(point_color = island),
    alpha = .2, point_alpha = 1, jittered_points = TRUE
  ) +
  theme(legend.position = "none")
#> Picking joint bandwidth of 172

可以在ggplot geom_density_ridges中可视化群组吗?

Created on 2023-05-18 with reprex v2.0.2

So, for the Adelie penguins, the 'groups' (the island of origin) are plotted but you have three density curves instead of the single curve in the first plot.

A potential workaround for this is to plot the ridges and the dots separately using two geom_density_ridges() calls, e.g.

library(tidyverse)
library(palmerpenguins)
library(ggridges)

penguins %>%
  na.omit() %>%
  ggplot(aes(x = body_mass_g, y = species)) +
  geom_density_ridges() +
  geom_density_ridges(
    aes(point_color = island),
    alpha = 0, color = NA, point_alpha = 1, jittered_points = TRUE
  ) +
  theme(legend.position = "none")
#> Picking joint bandwidth of 153
#> Picking joint bandwidth of 172

可以在ggplot geom_density_ridges中可视化群组吗?

Created on 2023-05-18 with reprex v2.0.2

Would this approach work for your use-case?

英文:

If you introduce another factor into your aes() (e.g. color or shape) it will change the number of density curves, e.g. using the palmerpenguins dataset:

library(tidyverse)
library(palmerpenguins)
library(ggridges)

penguins %&gt;%
  na.omit() %&gt;%
  ggplot(aes(x = body_mass_g, y = species)) +
  geom_density_ridges(
    alpha = .2, point_alpha = 1, jittered_points = TRUE
  )
#&gt; Picking joint bandwidth of 153

可以在ggplot geom_density_ridges中可视化群组吗?<!-- -->

penguins %&gt;%
  na.omit() %&gt;%
  ggplot(aes(x = body_mass_g, y = species)) +
  geom_density_ridges(
    aes(point_color = island),
    alpha = .2, point_alpha = 1, jittered_points = TRUE
  ) +
  theme(legend.position = &quot;none&quot;)
#&gt; Picking joint bandwidth of 172

可以在ggplot geom_density_ridges中可视化群组吗?<!-- -->

<sup>Created on 2023-05-18 with reprex v2.0.2</sup>

So, for the Adelie penguins, the 'groups' (the island of origin) are plotted but you have three density curves instead of the single curve in the first plot.

A potential workaround for this is to plot the ridges and the dots separately using two geom_density_ridges() calls, e.g.

library(tidyverse)
library(palmerpenguins)
library(ggridges)

penguins %&gt;%
  na.omit() %&gt;%
  ggplot(aes(x = body_mass_g, y = species)) +
  geom_density_ridges() +
  geom_density_ridges(
    aes(point_color = island),
    alpha = 0, color = NA, point_alpha = 1, jittered_points = TRUE
  ) +
  theme(legend.position = &quot;none&quot;)
#&gt; Picking joint bandwidth of 153
#&gt; Picking joint bandwidth of 172

可以在ggplot geom_density_ridges中可视化群组吗?<!-- -->

<sup>Created on 2023-05-18 with reprex v2.0.2</sup>

Would this approach work for your use-case?

huangapple
  • 本文由 发表于 2023年5月18日 06:45:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定