0

Working with this dataframe

dput(df_activity)

I am trying to create a piechart using the following variables, "veryActiveMinutes","FairlyActiveMinutes","lightlyActiveMinutes", and "SedentaryMinutes". This is the code I used:

VeryActiveMin <- sum(df_activity$VeryActiveMinutes)
FairlyActiveMin <- sum(df_activity$FairlyActiveMinutes)
LightlyActiveMin <- sum(df_activity$LightlyActiveMinutes)
SedentaryMin <- sum(df_activity$SedentaryMinutes)
TotalMin <- VeryActiveMin + FairlyActiveMin + LightlyActiveMin + SedentaryMin

slices <- c(VeryActiveMin,FairlyActiveMin,LightlyActiveMin,SedentaryMin)
lbls <- c("VeryActive","FairlyActive","LightlyActive","Sedentary")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct)
lbls <- paste(lbls, "%", sep="")
pie(slices, labels = lbls, col = rainbow(length(lbls)), main = "Percentage of Activity in Minutes")

and this is the result I got click here

what can I remove/add to the code to get a better looking chart, with proper labels/legends?

Mimikul
  • 3
  • 1
  • 1
    Please insert the output of dput(df_activity), not the literal command. – dcsuka Aug 13 '22 at 00:19
  • 1
    As @dcsuka said the output of ```dput(df_activity)``` is necessary. We have to see what the data that goes in your code looks like before anyone can help you with fixing the problem you are having. Also! what kind of pie-chart are you expecting? the ```pie()``` function is in the graphics (base R) package, and has nothing to do with ```ggplot2``` (the tag you used) and also named in the title. Maybe the documentation of ```ggplot2``` or this Q&A https://stackoverflow.com/q/47238098/17270192 has some hints/pointers for you on how to make a pie chart with ```ggplot2``` – Omniswitcher Aug 13 '22 at 00:35
  • Does this answer your question? [Plotting pie charts in ggplot2](https://stackoverflow.com/questions/47238098/plotting-pie-charts-in-ggplot2) – zephryl Aug 13 '22 at 01:04
  • See this first https://blog.livealytics.com/why-pie-charts-suck – Dave2e Aug 13 '22 at 03:17

1 Answers1

0

Since you tagged this as ggplot2, I have recreated your plot using the ggplot2 package (with random data) and couldn't use much of your original code that relied on graphics::pie. I also recommend looking into waffle charts and bar plots for visualization purposes.

library(tidyverse)
set.seed(123)

df_activity <- 
  data.frame(
    VeryActiveMinutes = sample(1:10, size=100, replace=TRUE),
    FairlyActiveMinutes = sample(1:25, size=100, replace=TRUE),
    LightlyActiveMinutes = sample(1:15, size=100, replace=TRUE),
    SedentaryMinutes = sample(1:30, size=100, replace=TRUE))

VeryActiveMin <- sum(df_activity$VeryActiveMinutes)
FairlyActiveMin <- sum(df_activity$FairlyActiveMinutes)
LightlyActiveMin <- sum(df_activity$LightlyActiveMinutes)
SedentaryMin <- sum(df_activity$SedentaryMinutes)
TotalMin <- VeryActiveMin + FairlyActiveMin + LightlyActiveMin + SedentaryMin

pie_df <- data.frame(VeryActiveMin, FairlyActiveMin, LightlyActiveMin, SedentaryMin, TotalMin) %>%
  pivot_longer(cols = everything()) %>%
  mutate(name = factor(name, levels=c('VeryActiveMin', 'FairlyActiveMin', 
                                      'LightlyActiveMin', 'SedentaryMin'))) %>%
  mutate(percent = value/(TotalMin)) %>%
  filter(!name == "TotalMin") %>%
  mutate(label = scales::percent(percent))

ggplot(pie_df, aes(x = "", y = value, fill = name)) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar("y", start = 0) + theme_void() +
  ggtitle("Figure 1. Minutes by Activity Type") +
  theme(plot.title = element_text(hjust = 0.5, vjust = -2)) +
  scale_fill_manual(name = "Activity Type", 
                    labels = c("Very Active",
                                "Fairly Active",
                                "Lightly Active",
                                "Sedentary"),
                    values = c("red", "blue", "yellow", "green")) +
  geom_label(aes(label = label), 
             size = 5,
             position = position_stack(vjust = .4),
             show.legend = FALSE)

enter image description here

jrcalabrese
  • 2,184
  • 3
  • 10
  • 30