尝试构建饼图时 R 不显示百分比
R doesn't display percentages when trying to build a pie chart
这是我写的代码:
gender=Survey$`To_which_gender_you_identify_the_most?`
gender<-as.factor(gender)
count=c("Prefer not to say","Male","Female")
pie(table(gender),labels=c(paste0(count)))
我正在尝试创建一个显示百分比的饼图,但没有显示百分比。
样本数据
输入
> dput(head(Survey[1:4]))
structure(list(Horodateur = structure(c(1619171956.596, 1619172695.039,
1619173104.83, 1619174548.534, 1619174557.538, 1619174735.457
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), `To_which_gender_you_identify_the_most?` = c("Male",
"Female", "Male", "Female", "Female", "Female"), What_is_your_age_group = c("[18-24[",
"[10,18[", "[18-24[", "[18-24[", "[18-24[", "[25,34["), How_much_time_do_you_spend_on_social_media = c("1-5 hours",
"1-5 hours", ">10 hours", "5-10 hours", "5-10 hours", "1-5 hours"
)), row.names = c(NA, 6L), class = c("tbl_df", "tbl", "data.frame"
))
>
已更新。
根据更新的示例数据,此代码将根据要求生成标有百分比的饼图。
#your example data
Survey<-structure(list(
Horodateur = structure(c(1619171956.596, 1619172695.039, 1619173104.83, 1619174548.534, 1619174557.538, 1619174735.457), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
`To_which_gender_you_identify_the_most?` = c("Male", "Female", "Male", "Female", "Female", "Female"),
What_is_your_age_group = c("[18-24[", "[10,18[", "[18-24[", "[18-24[", "[18-24[", "[25,34["),
How_much_time_do_you_spend_on_social_media = c("1-5 hours", "1-5 hours", ">10 hours", "5-10 hours", "5-10 hours", "1-5 hours")),
row.names = c(NA, 6L), class = c("tbl_df", "tbl", "data.frame"))
#make a dataframe of a gender table
gender<-data.frame(table(Survey$`To_which_gender_you_identify_the_most?`))
#add a percentages to the labels. Don't use 'count' to label unless you are sure it mataches the order of your data. More on that below.
pie(gender$Freq,labels=c(paste0(gender$Var1," ",round(100*gender$Freq/length(Survey$`To_which_gender_you_identify_the_most?`), digits = 0),"%")))
结果:
原创.
您需要在 pie()
中向 label=
添加某种百分比计算。您当前的所有代码都在用名为 count
的字符向量标记饼图。试试这个:
#make a dataframe out of the table of your gender object
your_table<-data.frame(table(gender))
#add a percentages to the labels. Don't use 'count' to label unless you are sure it mataches the order of your data. More on that below.
pie(your_table$Freq,labels=c(paste0(your_table$gender," ",round(100*your_table$Freq/length(gender), digits = 0),"%")))
在您的原始代码中要注意的一件事是您正在根据名为 count
的向量应用标签,但 gender
对象中的因子顺序可能匹配也可能不匹配。让我告诉你这会如何给你的 pie() 贴上错误的标签。
#This is an example of how you might mislabel your data.
Survey<-data.frame(`To_which_gender_you_identify_the_most?`=c("Female","Male","Female","Male","Female", "Prefer not to say"))
gender=Survey$`To_which_gender_you_identify_the_most`
gender<-as.factor(gender)
count=c("Prefer not to say","Male","Female")
your_table<-data.frame(table(gender))
pie(your_table$Freq,labels=c(paste0(count," ",round(100*your_table$Freq/length(gender), digits = 0),"%")))
请注意所有百分比标签都是正确的,但是 count
中的文本标签在此示例图像中的顺序错误。
这是我写的代码:
gender=Survey$`To_which_gender_you_identify_the_most?`
gender<-as.factor(gender)
count=c("Prefer not to say","Male","Female")
pie(table(gender),labels=c(paste0(count)))
我正在尝试创建一个显示百分比的饼图,但没有显示百分比。 样本数据
输入
> dput(head(Survey[1:4]))
structure(list(Horodateur = structure(c(1619171956.596, 1619172695.039,
1619173104.83, 1619174548.534, 1619174557.538, 1619174735.457
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), `To_which_gender_you_identify_the_most?` = c("Male",
"Female", "Male", "Female", "Female", "Female"), What_is_your_age_group = c("[18-24[",
"[10,18[", "[18-24[", "[18-24[", "[18-24[", "[25,34["), How_much_time_do_you_spend_on_social_media = c("1-5 hours",
"1-5 hours", ">10 hours", "5-10 hours", "5-10 hours", "1-5 hours"
)), row.names = c(NA, 6L), class = c("tbl_df", "tbl", "data.frame"
))
>
已更新。 根据更新的示例数据,此代码将根据要求生成标有百分比的饼图。
#your example data
Survey<-structure(list(
Horodateur = structure(c(1619171956.596, 1619172695.039, 1619173104.83, 1619174548.534, 1619174557.538, 1619174735.457), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
`To_which_gender_you_identify_the_most?` = c("Male", "Female", "Male", "Female", "Female", "Female"),
What_is_your_age_group = c("[18-24[", "[10,18[", "[18-24[", "[18-24[", "[18-24[", "[25,34["),
How_much_time_do_you_spend_on_social_media = c("1-5 hours", "1-5 hours", ">10 hours", "5-10 hours", "5-10 hours", "1-5 hours")),
row.names = c(NA, 6L), class = c("tbl_df", "tbl", "data.frame"))
#make a dataframe of a gender table
gender<-data.frame(table(Survey$`To_which_gender_you_identify_the_most?`))
#add a percentages to the labels. Don't use 'count' to label unless you are sure it mataches the order of your data. More on that below.
pie(gender$Freq,labels=c(paste0(gender$Var1," ",round(100*gender$Freq/length(Survey$`To_which_gender_you_identify_the_most?`), digits = 0),"%")))
结果:
原创.
您需要在 pie()
中向 label=
添加某种百分比计算。您当前的所有代码都在用名为 count
的字符向量标记饼图。试试这个:
#make a dataframe out of the table of your gender object
your_table<-data.frame(table(gender))
#add a percentages to the labels. Don't use 'count' to label unless you are sure it mataches the order of your data. More on that below.
pie(your_table$Freq,labels=c(paste0(your_table$gender," ",round(100*your_table$Freq/length(gender), digits = 0),"%")))
在您的原始代码中要注意的一件事是您正在根据名为 count
的向量应用标签,但 gender
对象中的因子顺序可能匹配也可能不匹配。让我告诉你这会如何给你的 pie() 贴上错误的标签。
#This is an example of how you might mislabel your data.
Survey<-data.frame(`To_which_gender_you_identify_the_most?`=c("Female","Male","Female","Male","Female", "Prefer not to say"))
gender=Survey$`To_which_gender_you_identify_the_most`
gender<-as.factor(gender)
count=c("Prefer not to say","Male","Female")
your_table<-data.frame(table(gender))
pie(your_table$Freq,labels=c(paste0(count," ",round(100*your_table$Freq/length(gender), digits = 0),"%")))
请注意所有百分比标签都是正确的,但是 count
中的文本标签在此示例图像中的顺序错误。