R 根据分类变量总结数据分布的最佳方法是什么?我试着根据一周中的哪一天来模拟通话
我有一个数据框,其中包含日期、一周中的某一天类别和数字呼叫数。我试图分析一周中每天的通话量分布情况。使用lattice软件包,我可以创建条形图,但仍然需要一些额外的帮助: 如何对柱状图中显示的星期几变量进行排序?我希望它从周六开始,然后是周日,然后是周一 我还想转换分布,使条形图是垂直的,而不是水平的 最后,如何添加长方体和wisker图?我还应该用tapply吗 谢谢 以下是我迄今为止所做的工作:R 根据分类变量总结数据分布的最佳方法是什么?我试着根据一周中的哪一天来模拟通话,r,bar-chart,lattice,categorical-data,tapply,R,Bar Chart,Lattice,Categorical Data,Tapply,我有一个数据框,其中包含日期、一周中的某一天类别和数字呼叫数。我试图分析一周中每天的通话量分布情况。使用lattice软件包,我可以创建条形图,但仍然需要一些额外的帮助: 如何对柱状图中显示的星期几变量进行排序?我希望它从周六开始,然后是周日,然后是周一 我还想转换分布,使条形图是垂直的,而不是水平的 最后,如何添加长方体和wisker图?我还应该用tapply吗 谢谢 以下是我迄今为止所做的工作: LatinoDRTVdata <- read.csv("//dishfs1/Marketi
LatinoDRTVdata <- read.csv("//dishfs1/Marketing/Mktg_Analytics/Team Member folders/Ryan_Chase/Ad Hoc/Latino DRTV Normalized Calls.csv")
#look at the first 10 rows
head(LatinoDRTVdata)
#look at the full dataset
LatinoDRTVdata
#look at the column names
colnames(LatinoDRTVdata)
#check the class of the Normalized.Latino.DRTV.call.volume column
class(LatinoDRTVdata$Normalized.Latino.DRTV.call.volume)
##make the call volume a numeric vector
LatinoDRTVdata$Normalized.Latino.DRTV.call.volume <- as.numeric(LatinoDRTVdata$Normalized.Latino.DRTV.call.volume)
#now check the class again
class(LatinoDRTVdata$Normalized.Latino.DRTV.call.volume)
(LatinoDRTVdata$Normalized.Latino.DRTV.call.volume)
#194 calls is the mean volume regardless of the day
mean(LatinoDRTVdata$Normalized.Latino.DRTV.call.volume)
#Day of the week is a factor
class(LatinoDRTVdata$Day.of.the.Week)
summary(LatinoDRTVdata)
str(LatinoDRTVdata)
#histogram of daily Latino DRTV call volume
hist(LatinoDRTVdata$Normalized.Latino.DRTV.call.volume)
#find the mean of each day
Daily.Latino.DRTV.Distribution<- tapply(LatinoDRTVdata$Normalized.Latino.DRTV.call.volume,LatinoDRTVdata$Day.of.the.Week,mean)
Daily.Latino.DRTV.Distribution
Daily.Latino.DRTV.Distribution$ormalized.Latino.DRTV.call.volume
##check that a new object has been added
ls()
str(Daily.Latino.DRTV.Distribution)
#make sure you install the lattice package for the graphics
#load the lattice package
library(lattice)
barchart(Daily.Latino.DRTV.Distribution)
通过重新声明来设置DotW因子顺序
levels(LatinoDRTVdata$Day.of.the.Week) <- c("Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday")`
看起来,用于绘制图形的数据并没有汇总为计数,但如果是,则需要添加一个参数geom_barstat='identity'。单杠由coord_flip函数产生
对2的轻微修改
ggplot(LatinoDRTVdata, aes(x=Day.of.the.Week, y=Normalized.Latino.DRTV.call.volume)) + geom_boxplot()
ggplot的一个好资源:基本上你想要一个水平条形图,上面有一周中的某一天,并且在条形图的顶部添加一个方框和胡须图?你能把一些样本数据粘贴到一个名为df的数据框中吗?或者粘贴10行数据的dput?我添加了我数据的前10行。谢谢。嘿,加里,谢谢你的回复!我仍然是一个新的R用户…你能在1中解释R如何知道用当前命名为Saturday的因子中的值分配Sat吗?我得到了一个错误:LevelsLattinodRTVData$Day.of.Week中的错误嗨,Ryan,我更新了答案以帮助解决您的问题,之前我不知道您的DotW因子是如何格式化的,所以我只是猜测。通过使用与Sat相反的确切因子标签,可以消除歧义。试试看。通过使用is.factordata$DotW来测试数据$DotW(如果需要),确保它实际上是一个因子
library(ggplot2)
ggplot(LatinoDRTVdata, aes(x=Day.of.the.Week, y=Normalized.Latino.DRTV.call.volume)) + geom_bar() + coord_flip()
ggplot(LatinoDRTVdata, aes(x=Day.of.the.Week, y=Normalized.Latino.DRTV.call.volume)) + geom_boxplot()