使用count
函数计算出现的次数一天明智和barplot
功能绘制
#generating sample dataframe
tips <- data.frame(total_bills=1:10, tip=10:19, sex=sample(c("male","female"),10, replace = TRUE), smoker=sample(c("yes","no"),10, replace = TRUE), day = sample(c("Mon","Tue","Wed","Fri","Sat","Sun"),10, replace = TRUE),time=sample(c("Breakfast","Lunch","Dinner"),10, replace = TRUE),size = sample(1:5,10, replace = TRUE), stringsAsFactors = FALSE)
#count the number of occurences using _count_ function
io <- plyr::count(tips[tips$time == "Dinner",], vars = "day")
#getting days percentage
io$count_percent <- round(io$freq/sum(io$freq) * 100,2)
> io
day freq count_percent
1 Sun 1 20
2 Tue 2 40
3 Wed 2 40
#generate barplot
require(ggplot2)
p <- ggplot(io, aes(day, count_percent))
p + geom_bar(stat = "identity")
示例输出上面的数据:
您是否尝试过做情节呢? –