user11418708
user11418708

Reputation: 902

How to use coord_polar

I have following data frame in R and want to coord_polar chart out of it.

Below the structure of my data

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, 1L, 1L, 1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L, 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")

I am doing following in 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))  

And I receive the following plot

enter image description here

Is there any easy way to format the minutes to show hours and minutes like

enter image description here

Upvotes: 0

Views: 397

Answers (1)

Jon Spring
Jon Spring

Reputation: 66490

Here's two steps to get there, plus an extra one to make the data more representative since your sample from the first 500 rows of your data only includes the first of 11 activities.

Step 0. Assign a mix of activities so the rest makes sense.

Step 1. Convert time to numeric and get the average.

Step 2. Plot.

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

Upvotes: 2

Related Questions