在R中创建一个圆形图,用于可视化在60分钟时钟上的分钟之间的活动。

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

Create a circular plot in R that visualizes actions between minutes on a 60 minute clock

问题

我试图制作一个表示60分钟时钟的圆形图,颜色条表示在这些时间发生的动作。我已经成功制作了一个基本版本,但不知道如何继续。以下是我目前的进展:

我的目标是使“声学采样”条在15到30分钟之间显示。在0/60到15分钟之间显示“Inactive1”,在36到40分钟之间显示“数据复制”等等。总的来说,我的目标是使每个动作填充它们在数据中应该发生的时间段。

同时,将所有“Inactive”动作归为同一组将是很好的,我可能可以自己解决这个问题,我只需要一个正确方向的指针,不想让每个人都为我做一切。

代码:

attach(clock_data)
hsize <- 4

clock_data <- clock_data %>%
  mutate(x = hsize)

ggplot(clock_data, aes(x = hsize, y = Time, fill = Action)) +
  geom_col() + 
  coord_polar(theta = "y") +
  xlim(c(0.2, hsize + 0.5)) + theme_bw()

以及“clock_data”的reprex:

structure(list(Time_Between = c("0-15", "15-30", "30-33", "33-34", 
"34-35", "35-36", "36-40", "40-55", "55-56", "56-60"), Time = c(15L, 
15L, 3L, 1L, 1L, 1L, 4L, 15L, 1L, 4L), Action = c("Inactive1", 
"Acoustic Sampling", "Inactive2", "SSH Tunnel", "Inactive3", 
"Switch Pi", "Data Copy", "Data Transmission", "Switch Mini", 
"Inactive4"), x = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4)), row.names = c(NA, 
10L), class = "data.frame")
英文:

I am trying to make a circular plot that represents a 60-minute clock with the color bars representing actions that occur at those times. I've managed to make a rudimentary version of it but am at a loss about how to proceed. Here is what I have thus far:
在R中创建一个圆形图,用于可视化在60分钟时钟上的分钟之间的活动。

My goal is to make it so the 'Acoustic Sampling' bar is displayed between the 15 and 30 minute mark. 'Inactive1' be displayed between 0/60 and 15 minutes, 'Data Copy' between 36 and 40 minutes etc. The gist is to have each action fill the times they are supposed to occur in the data.

Also having all the 'Inactive' actions be part of the same group would be great, I can probably figure that out myself, I just need a pointer in the right direction. Don't want to be asking everyone to do everything for me.

Code:

attach(clock_data)
hsize &lt;- 4

clock_data &lt;- clock_data %&gt;%
  mutate(x = hsize)

ggplot(clock_data, aes(x = hsize, y = Time, fill = Action)) +
  geom_col() + 
  coord_polar(theta = &quot;y&quot;) +
  xlim(c(0.2, hsize + 0.5)) + theme_bw()

and the reprex for 'clock_data':

structure(list(Time_Between = c(&quot;0-15&quot;, &quot;15-30&quot;, &quot;30-33&quot;, &quot;33-34&quot;, 
&quot;34-35&quot;, &quot;35-36&quot;, &quot;36-40&quot;, &quot;40-55&quot;, &quot;55-56&quot;, &quot;56-60&quot;), Time = c(15L, 
15L, 3L, 1L, 1L, 1L, 4L, 15L, 1L, 4L), Action = c(&quot;Inactive1&quot;, 
&quot;Acoustic Sampling&quot;, &quot;Inactive2&quot;, &quot;SSH Tunnel&quot;, &quot;Inactive3&quot;, 
&quot;Switch Pi&quot;, &quot;Data Copy&quot;, &quot;Data Transmission&quot;, &quot;Switch Mini&quot;, 
&quot;Inactive4&quot;), x = c(4, 4, 4, 4, 4, 4, 4, 4, 4, 4)), row.names = c(NA, 
10L), class = &quot;data.frame&quot;)

答案1

得分: 2

主要问题是您没有考虑到时间间隔的起始和结束。修复的一个选项是将Time_between分成起始时间和结束时间,然后使用geom_rect来创建柱状图,而不是使用geom_col。另外,我将Inactive重新编码为属于同一组。

library(dplyr)
library(tidyr)
library(ggplot2)

hsize <- 4

clock_data <- clock_data %>%
  mutate(x = hsize) %>%
  tidyr::separate(Time_Between,
    into = c("start", "end"),
    convert = TRUE
  ) %>%
  mutate(
    Action = if_else(
      grepl("^Inactive", Action), "Inactive", Action
    ),
    # 如果需要:重新排序以供图例使用
    Action = reorder(Action, start, FUN = min)
  )

ggplot(clock_data) +
  geom_rect(aes(
    xmin = hsize - 0.45, ymin = start,
    xmax = hsize + 0.45, ymax = end,
    fill = Action
  )) +
  coord_polar(theta = "y") +
  xlim(c(0.2, hsize + 0.5)) +
  theme_bw()

在R中创建一个圆形图,用于可视化在60分钟时钟上的分钟之间的活动。

英文:

The main issue is that you do not account for the start and end of the intervals. One option to fix that would be separate Time_between into the start and end times, then use a geom_rect to create your bars instead of using a geom_col. Additionally I recoded the Inactives as belonging to one group.

library(dplyr)
library(tidyr)
library(ggplot2)

hsize &lt;- 4

clock_data &lt;- clock_data %&gt;%
  mutate(x = hsize) %&gt;%
  tidyr::separate(Time_Between,
    into = c(&quot;start&quot;, &quot;end&quot;),
    convert = TRUE
  ) |&gt;
  mutate(
    Action = if_else(
      grepl(&quot;^Inactive&quot;, Action), &quot;Inactive&quot;, Action
    ),
    # If needed: Reorder for the legend
    Action = reorder(Action, start, FUN = min)
  )

ggplot(clock_data) +
  geom_rect(aes(
    xmin = hsize - .45, ymin = start,
    xmax = hsize + .45, ymax = end,
    fill = Action
  )) +
  coord_polar(theta = &quot;y&quot;) +
  xlim(c(0.2, hsize + 0.5)) +
  theme_bw()

在R中创建一个圆形图,用于可视化在60分钟时钟上的分钟之间的活动。

huangapple
  • 本文由 发表于 2023年8月4日 07:14:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76832094.html
匿名

发表评论

匿名网友

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

确定