楼主: Lee_iris
23834 14

[学习心得] 关于stata作图的各种option [推广有奖]

已卖:1252份资源

教授

46%

还不是VIP/贵宾

-

威望
0
论坛币
1167 个
通用积分
1195.9565
学术水平
36 点
热心指数
43 点
信用等级
35 点
经验
14294 点
帖子
893
精华
0
在线时间
1514 小时
注册时间
2019-3-5
最后登录
2025-12-5

楼主
Lee_iris 在职认证  学生认证  发表于 2020-8-4 18:42:55 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
编辑逼人学stata在编辑要求下修改图形,解锁了stata作图好多option,总结一下





二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:stata作图 Option Stata tata OPT stata 作图 option

沙发
Lee_iris 在职认证  学生认证  发表于 2020-8-4 22:56:47
1.关于图例

(1)是否显示图例
用 legend(on) legend(off)

(2)设置图例大小

size从小到大分别为vtiny、tiny、vsmall、small、medsmall、medium、medlarge、large、vlarge、huge、vhuge、ehuge

  1. legend(size(large))
复制代码

(3)设置图例位置


主要有几个options:
    position(clockposstyle) :where legend appears
    ring(ringposstyle) :where legend appears (detail)
    bplacement(compassdirstyle)  :   placement of legend when positioned in the plotregion
    span:"centering" of legend
    at(#)  : allowed with by() only

其中position指定legend 的大概方位,按时钟方向。如 position(10) 表示图例在10点钟方向。ring指定具体位置, ring(0) 表示图例在 plot region,即在图的内部,如图。


微信图片_20200804225515.png

截图参考:
  1. help title_options
复制代码

(4)设置图例的线条长短

用 symxsize(#)括号里可用数字  

(5)设置部分图例显示部分不显示

比如对照组有很多,没有必要全部表明某个线条是哪个对照组,对照组的线条样式也都一样,这时候图例里只需要出现一个对照组,其他的不显示,打开图形编辑,手动操作选择隐藏(omit)可以实现,但是很难调整位置,不太容易美化图形,这时候最好还是通过命令完成绘图。例如:

  1. ,legend(label(1 "处理组")  label(2 "对照组") order(1 2)
复制代码

这里的“1”和“2”应该是绘图的顺序,需要看情况调整。

藤椅
Lee_iris 在职认证  学生认证  发表于 2021-2-6 18:49:46
2.关于坐标轴

关于坐标轴的命令可以查询

  1. help axis_options
复制代码

然后会出现:

    axis_options are grouped into four classes.


        axis_title_options specify the titles to appear next to the axes.  They also allow you to
            format the title fonts.  See [G-3] axis_title_options.

        axis_label_options specify how the axes should be labeled and ticked.  These options allow you
            to control the placement of major and minor ticks and labels.  axis_label_options also
            allow you to add or to suppress grid lines on your graphs.  See [G-3] axis_label_options.

        axis_scale_options specify how the axes should be scaled -- either logarithmic scaled or
            reverse scaled to run from maximum to minimum.  These options also allow you to change the
            range of the axes and the look of the lines that are the axes, including placement.  See
            [G-3] axis_scale_options.

        axis_choice_options control the specific axis on which a plot appears when there are multiple x
            or y axes.  See [G-3] axis_choice_options.

每一项里都有很多细节可以调整

(1)设置横纵坐标标题的字体、方向和大小等

ytitle、xtitle、 ttitle或者ztitle的option里用以下格式写命令,其中“某个字体的名字”和“坐标轴的标题”用相应的名称替换:

  1. `"{fontface 某个字体的名字:坐标轴的标题}"'
复制代码

至于ytitle、xtitle、 ttitle或者ztitle是什么参见:

  1. help axis_title_options
复制代码

    axis_title_options         Description
    -----------------------------------------------------
    ytitle(axis_title)         specify y axis title
    xtitle(axis_title)         specify x axis title
    ttitle(axis_title)         specify t (time) axis title
    ztitle(axis_title)         specify contour legend axis title

如果想让坐标轴的标题横过来,需要再加axis_title的option“,orientation(horizontal)”但同时坐标轴的每个字都要分开写,否则一行字都会横过来,相当占地方。调整坐标名称大小的话直接加axis_title的option“size( ) ”,具体字符大小的参数和前面提到的一样。举个例子:

  1. ,ytitle(`"{fontface 楷体:地}"'`"{fontface 楷体:方}"'`"{fontface 楷体:财}"'`"{fontface 楷体:政}"'`"{fontface 楷体:一}"'`"{fontface 楷体:般}"'`"{fontface 楷体:预}"'`"{fontface 楷体:算}"'`"{fontface 楷体:支}"'`"{fontface 楷体:出}"'`"{fontface 楷体:(万元)}"',orientation(horizontal)  size( medium  ) )
复制代码


(2)设置横纵坐标轴刻度线的位置

可以选(outside|crossing| inside)

  1. scatter  trade  date, xlabel( ,tposition(inside) ) ylabel( ,tposition(inside) )
复制代码

(3)坐标轴起始值显示为0

需要用到plotregion的option margin
  1. [backcolor=rgb(247, 247, 247)]scatter  trade  date, [/backcolor]plotregion(margin(0))
复制代码


(4)坐标轴是否向外延伸

  1. [backcolor=rgb(247, 247, 247)]scatter  trade  date, [/backcolor]xscale(extend  outergap(5pt) )  yscale(extend)
复制代码

板凳
Lee_iris 在职认证  学生认证  发表于 2021-2-6 19:01:26
Tips:

1. 关于option

option意味着要加逗号“,”,有的option还有option的,就继续加逗号和括号,括号要有始有终,stata16里写do文件的时候,把鼠标点到括号的前面或者后面,对应的括号会变红,可以方便检查。

推荐一个b站宝藏up主silencedream,通俗地讲了很多计量的知识,作为入门很适用。




报纸
qnsryhm 发表于 2023-3-15 11:42:27
感谢分享,学到了很多

地板
小啃啃KK 发表于 2023-3-31 22:03:31
十分感谢您的分享,学到了很多东西,再次表示感谢!

7
Lee_iris 在职认证  学生认证  发表于 2023-5-26 12:58:54
中间的代码有点乱了,和网站本身的代码有点冲突

8
一位不知名的momo 发表于 2023-8-5 17:30:51
Lee_iris 发表于 2023-5-26 12:58
中间的代码有点乱了,和网站本身的代码有点冲突
你好,请问一下 可以解释一下这个命令嘛yaxis和xaxis
      twoway (line le_wmale year, yaxis(1 2) xaxis(1 2))
              (line le_bmale year)
              (line diff  year)
全部代码为:
         sysuse uslifeexp, clear
     gen diff = le_wmale - le_bmale
     label var diff "寿命差异"
     #delimit ;  
      twoway (line le_wmale year, yaxis(1 2) xaxis(1 2))
              (line le_bmale year)
              (line diff  year)
              ,
              ylabel(0 20(10)80,    gmax angle(0))
              ylabel(0(5)20, axis(2) gmin angle(0))
              xlabel(1918, axis(2))
              title("图2:白人和黑人预期寿命")
              subtitle("美国, 1900-1999")
              ytitle("预期寿命 (年)")
              xtitle("年份")
              ytitle("", axis(2))
              xtitle("", axis(2))
              note("数据来源:  美国国家重要统计资料报告, 第5卷-第6期"
                   "(1918 巨降: 源于1918年全国性流行感冒)",linegap(1.2))
              legend(label(1 "白人男性") label(2 "黑人男性")
                     rows(1) size(*0.7));
     #delimit cr

9
friesfrie 发表于 2023-9-23 14:22:26
,ytitle(`"{fontface 楷体:地}"'`"{fontface 楷体:方}"'`"{fontface 楷体:财}"'`"{fontface 楷体:政}"'`"{fontface 楷体:一}"'`"{fontface 楷体:般}"'`"{fontface 楷体:预}"'`"{fontface 楷体:算}"'`"{fontface 楷体:支}"'`"{fontface 楷体:出}"'`"{fontface 楷体:(万元)}"',orientation(horizontal)  size( medium  ) )
按照这个命令输入后显示option size not allowed, 有没有uu懂怎么调节坐标轴字体大小

10
Lee_iris 在职认证  学生认证  发表于 2023-9-24 13:21:12
一位不知名的momo 发表于 2023-8-5 17:30
你好,请问一下 可以解释一下这个命令嘛yaxis和xaxis
      twoway (line le_wmale year, yaxis(1 2) xa ...
yaxis(1 2) xaxis(1 2)设定双坐标轴,后面axis(2)再指定对某一条坐标轴进行设定

其实现在很多代码都可以chatgpt问一下

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-5 16:47