[英]plotting a piechart using ggplot2 in R
Working with this dataframe使用此 dataframe
dput(df_activity)
I am trying to create a piechart using the following variables, "veryActiveMinutes","FairlyActiveMinutes","lightlyActiveMinutes", and "SedentaryMinutes".我正在尝试使用以下变量创建饼图,“veryActiveMinutes”、“FairlyActiveMinutes”、“lightlyActiveMinutes”和“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?我可以删除/添加到代码中以获得更好看的图表,并带有适当的标签/图例?
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
. 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.