如何在Stata中为多个时隙创建不同活动的“堆积条形图”?

如何解决如何在Stata中为多个时隙创建不同活动的“堆积条形图”?

我正在使用Stata比较一天中8个不同时段(每个3小时,从00:00-03:00到21.00-00)在7种不同活动(例如工作,休闲)上花费的时间百分比:00)。我想制作一个“堆积的条形图”:条形图显示每个时隙上活动的相对大小(因此以百分比为单位,每个条形图总计为一百)。

下面是三个示例受访者的数据结构(现实中有363个受访者):

[variables] 

respnummer  domain_nr   domain_type 
time_00_03  time_03_06  time_06_09  time_09_12  time_12_15  time_15_18  time_18_21  time_21_00

[data]

1   1   Home        1   0   0   0   0   0   0   0

1   2   Work        0   0   0   0   1   0   0   0

1   3   Sports      0   0   0   0   0   0   1   0

1   4   Shopping    0   0   0   1   0   0   0   0

1   5   Going out   0   0   0   0   0   0   1   0

1   6   Other       0   0   0   0   0   1   0   0

2   1   Home        1   1   1   1   0   0   0   1

2   2   Education   0   0   0   1   1   1   0   0

2   3   Shopping    0   0   0   0   0   1   1   0

2   4   Going out   0   0   0   0   0   0   0   0

2   5   Other       1   1   1   1   1   1   1   1

3   1   Home        1   1   1   0   0   1   1   1

3   2   Sports      0   0   0   0   0   0   0   0

3   3   Shopping    0   0   0   1   1   0   0   0

3   4   Other       0   0   0   0   0   0   0   0

see image of data here

到目前为止,我仅在Stata中使用以下代码:

graph bar time_*,over(domain_type) stack //可以,但是反之亦然,而不是百分比加起来等于100%

see here the 'wrong' graph

但这是我想要得到的结果的另一种方式:我希望X轴上有8个不同的时隙,并且每个时隙要有7种不同活动(以百分比表示)的堆叠条形。当我尝试对变量重新排序时(见下文),Stata给我以下错误:“指定的变量太多”。

graph bar domain_type,over(time_*) stack //错误:指定了太多变量

此外,第一个“错误”图表中的百分比之和不等于百分之一百。因此,此外,需要将活动计数标准化为每个时间间隔的总计数的百分比。例如,对于第一个时隙(00:00-03:00),应将75%的活动(4个活动中的3个)归类为“首页”(第3/4大条),将25%的活动( 4中的1)被归类为“其他活动”(小条为1/4)。

如何正确创建此条形图?

解决方法

请参见并发线程Formatting catplot - stata,它引起了类似的挑战。在这里,我使用来自SSC的Main.class,这里只是catplot的包装器,但我对百分比计算的语法更加熟悉。

您的变量graph bar看起来像是带有值标签的数字变量,但您不提供标签映射。

domain_type

一样,也可以使用 Stata Journal 中的clear input respnummer domain_nr str9 Domain_Type time_00_03 time_03_06 time_06_09 time_09_12 time_12_15 time_15_18 time_18_21 time_21_00 1 1 Home 1 0 0 0 0 0 0 0 1 2 Work 0 0 0 0 1 0 0 0 1 3 Sports 0 0 0 0 0 0 1 0 1 4 Shopping 0 0 0 1 0 0 0 0 1 5 "Going out" 0 0 0 0 0 0 1 0 1 6 Other 0 0 0 0 0 1 0 0 2 1 Home 1 1 1 1 0 0 0 1 2 2 Education 0 0 0 1 1 1 0 0 2 3 Shopping 0 0 0 0 0 1 1 0 2 4 "Going out" 0 0 0 0 0 0 0 0 2 5 Other 1 1 1 1 1 1 1 1 3 1 Home 1 1 1 0 0 1 1 1 3 2 Sports 0 0 0 0 0 0 0 0 3 3 Shopping 0 0 0 1 1 0 0 0 3 4 Other 0 0 0 0 0 0 0 0 end reshape long time_,i(respnummer domain_nr) j(when) string replace when = subinstr(when,"_","-",.) * install first with code: ssc install catplot catplot Domain_Type when [fw=time_],percent(when) asyvars stack recast(bar) 查看结果
tabplot

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-