如何使用coord_polar

时间:2019-06-26 05:05:32

标签: r ggplot2

我在R中有以下数据框,并希望从中删除coord_polar图。

数据结构下方

dim(melted_TimingOfActivities)
[1] 181863      3

    l

library(ggplot2)



dput(head(melted_TimingOfActivities, 500))
structure(list(index = 1:500, variable = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L,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 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), .Label = c("Personal care/sleep (mins per day)", "Employment (mins per day)", 
"Study (mins per day)", "Household & family care (mins per day)", 
" Volunteer work & meetings (mins per day)", "Social life & entertainment (mins per day)", 
"Sports & outdoor activities (mins per day)", "Hobbies & games (mins per day)", 
"Mass media (mins per day)", "Travel (mins per day)", "Other spec/not specfd (mins per day)"
), class = "factor"), value = c("7:30", "11:10", "11:50", "11:40", 
"10:50", "12:10", "11:30", "11:30", "9:50", "10:20", "9:30", 
"9:10", "9:10", "9:0", "10:0", "8:20", "9:20", "9:20", "14:40", 
"9:10", "12:30", "10:20", "10:30", "9:20", "12:40", "9:0", "13:10", 
"10:30", "13:50", "10:40", "14:0", "10:40", "12:20", "11:0", 
"11:30", "12:40", "10:30", "10:0", "12:0", "13:0", "11:10", "8:30", 
"15:40", "9:30", "12:40", "11:0", "10:30", "9:20", "10:30", "10:10", 
"9:40", "10:30", "11:30", "10:0", "10:20", "10:20", "11:50", 
"11:10", "8:0", "8:0", "12:50", "11:50", "12:50", "9:40", "9:50", 
"7:50", "15:0", "13:10", "13:50", "13:40", "9:50", "11:0", "6:0", 
"8:0", "12:40", "11:20", "13:50", "14:50", "9:40", "12:50", "8:30", 
"12:30", "13:0", "13:20", "9:30", "13:50", "14:30", "18:20", 
"9:50", "10:20", "9:40", "9:0", "12:40", "14:0", "12:30", "12:40", 
"12:10", "10:0", "7:50", "9:40", "12:50", "12:0", "9:20", "11:40", 
"6:0", "5:50", "13:10", "14:0", "11:0", "9:50", "8:10", "13:0", 
"14:20", "12:30", "11:0", "9:30", "9:40", "11:40", "13:0", "13:20", 
"9:20", "11:50", "11:50", "10:50", "15:0", "10:20", "16:0", "9:10", 
"12:10", "10:10", "13:10", "11:10", "12:50", "11:40", "12:20", 
"11:50", "9:50", "11:10", "9:10", "9:20", "12:0", "10:20", "7:50", 
"9:50", "11:20", "7:40", "10:40", "14:30", "13:40", "10:0", "14:20", 
"7:20", "12:50", "9:50", "12:20", "9:50", "15:10", "12:10", "12:0", 
"14:50", "13:0", "15:10", "9:10", "11:50", "9:40", "10:10", "10:50", 
"9:0", "11:0", "10:40", "9:20", "11:0", "11:0", "12:30", "12:30", 
"13:50", "9:40", "10:40", "12:50", "7:0", "9:10", "6:40", "12:30", 
"9:30", "13:30", "12:30", "14:30", "14:30", "14:30", "13:20", 
"10:30", "15:40", "14:30", "14:10", "12:30", "7:20", "9:20", 
"11:30", "12:0", "13:0", "13:40", "15:0", "14:10", "14:40", "12:0", 
"11:50", "11:40", "12:0", "9:20", "1:0", "8:0", "9:10", "14:30", 
"12:30", "11:10", "14:20", "8:0", "8:0", "7:50", "13:50", "7:50", 
"8:10", "9:30", "9:40", "11:40", "13:20", "10:0", "8:40", "11:20", 
"10:30", "11:40", "9:0", "12:50", "10:50", "9:50", "11:0", "7:20", 
"10:40", "9:20", "8:20", "7:50", "7:10", "9:30", "13:10", "9:0", 
"13:10", "8:40", "12:0", "8:40", "8:10", "9:0", "12:30", "9:10", 
"9:20", "9:30", "9:30", "10:40", "9:0", "10:20", "10:10", "9:20", 
"9:10", "10:20", "12:0", "12:40", "11:0", "11:0", "10:30", "15:0", 
"9:50", "11:30", "12:10", "11:50", "14:20", "11:10", "9:0", "10:30", 
"12:30", "14:40", "10:10", "10:40", "8:50", "10:10", "5:50", 
"11:50", "9:30", "10:50", "12:30", "12:0", "14:40", "15:0", "15:40", 
"12:50", "8:0", "10:30", "8:20", "8:20", "8:20", "8:40", "11:30", 
"9:50", "6:40", "8:50", "11:40", "10:0", "10:40", "13:20", "7:10", 
"15:0", "11:0", "13:40", "12:0", "14:30", "13:50", "14:20", "8:40", 
"13:10", "8:40", "9:40", "7:50", "12:0", "8:40", "13:20", "9:40", 
"10:0", "9:0", "11:10", "10:0", "9:50", "15:10", "0:0", "13:0", 
"10:20", "9:40", "8:50", "11:0", "9:20", "10:40", "9:10", "15:10", 
"15:20", "8:50", "11:50", "14:30", "13:0", "12:10", "12:30", 
"10:10", "9:40", "9:20", "9:20", "10:10", "4:30", "10:20", "9:50", 
"9:20", "7:50", "9:0", "8:10", "11:40", "9:0", "10:30", "12:20", 
"11:20", "8:30", "10:20", "10:20", "12:20", "12:40", "12:40", 
"13:30", "12:40", "15:30", "11:20", "11:40", "11:40", "10:40", 
"12:20", "10:40", "13:10", "15:10", "13:50", "13:20", "14:20", 
"13:30", "6:30", "6:50", "10:30", "11:30", "9:30", "12:20", "11:20", 
"12:50", "11:20", "13:40", "11:10", "13:50", "18:20", "12:20", 
"9:50", "6:30", "14:10", "12:10", "11:10", "12:50", "9:0", "7:50", 
"10:10", "8:40", "7:0", "10:40", "12:30", "12:20", "12:0", "10:20", 
"8:50", "11:40", "9:40", "10:30", "10:40", "11:40", "10:0", "10:10", 
"11:40", "11:30", "13:10", "11:40", "13:30", "10:50", "14:20", 
"14:20", "14:30", "13:0", "12:50", "11:0", "13:50", "11:50", 
"13:10", "11:40", "8:10", "12:20", "12:20", "10:20", "9:10", 
"14:20", "11:40", "9:40", "8:30", "6:50", "11:30", "11:50", "11:20", 
"9:20", "14:30", "13:0", "8:20", "8:10", "9:0", "9:50", "9:10", 
"9:50", "10:50", "10:50", "10:20", "9:50", "12:30", "12:30", 
"10:30", "10:20", "12:50", "11:30", "9:0", "11:40", "8:20", "11:10", 
"9:0", "12:50", "10:10", "10:40", "10:10", "8:30", "10:10", "11:20", 
"12:20", "7:40", "12:10", "14:50", "13:50", "7:10", "9:40", "9:20", 
"12:50", "12:10", "13:40", "14:10", "9:20", "11:20", "12:20", 
"12:20", "9:40")), row.names = c(NA, 500L), class = "data.frame")

我正在R中关注

 ggplot(melted_TimingOfActivities, 
        aes(x=variable, y=value), fill = variable)+ 
   geom_col(width=1)+ 
   coord_polar(theta = "y")+
   labs(x="Minutes", y="Activities", colour="Activties", fill="Activites" ) + 
   theme(legend.position="right", axis.text.x = element_text(angle = 0, hjust = 0))  

我收到以下情节

enter image description here

有什么简单的方法可以格式化分钟,以显示小时和分钟,例如

enter image description here

1 个答案:

答案 0 :(得分:2)

这里有两个步骤,再加上一个额外的步骤使数据更具代表性,因为数据的前500行中的样本仅包括11个活动中的第一个。

第0步。分配一系列活动,以便其他活动有意义。

步骤1。将时间转换为数字并获得平均值。

第2步。绘图。

library(dplyr)
library(lubridate)
set.seed(42)

# Step 0. Randomly assigning other values to "variable" so we have variety
activities <- levels(melted_TimingOfActivities$variable)
melted_TimingOfActivities2 <- melted_TimingOfActivities %>%
  mutate(variable = sample(activities, n(), replace = T)) %>%

  # Step 1. Convert text minutes:seconds into numeric, take average
  mutate(minutes = ms(value) / minutes(1)) %>%
  group_by(variable) %>%
  summarize(avg_time = mean(minutes))

# Step 2: plot
ggplot(melted_TimingOfActivities2, 
       aes(x = 1, y = avg_time, fill = variable)) +
  geom_col() +
  coord_polar(theta = "y")

enter image description here