英文:
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:
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 <- 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()
and the reprex for 'clock_data':
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")
答案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()
英文:
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 Inactive
s as belonging to one group.
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
),
# 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 = "y") +
xlim(c(0.2, hsize + 0.5)) +
theme_bw()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论