Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • x label too long?

    Hello everyone,

    When I run the code below (search labutil and install), I get the result shown in Figure 1.
    Code:
    * 清除当前数据
    clear
    
    * 输入示例数据
    input str20(y x markers)
    "企业跨区交易" "电子商务发展"  "[1]tly25jjyj"
    "贸易成本" "电子商务发展"  "[2]jzs20jjyj"
    "居民消费" "电子商务发展"  "[3]ws25syjjyj"
    "企业跨区交易" "企业数字化"  "[4]yzc23cjyj"
    "贸易成本" "统一大市场"  "[5]hsy24zgsl"
    "居民消费" "数字经济发展"  "[6]bcw24dysj"
    end
    
    * 创建 X 轴标签,利用宏实现换行
    local linebreak = char(10)
    label define X 1 "电子商务发展" 3 "企业数字化" 5 "统一大市场" 7 "数字经济发展" 
    encode x, gen(X) 
    
    * 创建 Y 轴标签
    encode y, gen(Y) 
    labmask Y, values(y)
    
    * 绘制散点图
    scatter Y X, ///
        ytitle("") ///
        xtitle("") ///
        mcolor(none) ///
        mlab(markers) ///
        mlabpos(0) ///
        mlabsize(small) ///
        xlab(1 2 " " 3 4 " " 5 6 " " 7 8 " ", val labsize(small) grid) ///
        xsc(r(0 9)) ///
        ylab(1(1)3, val labsize(small) grid angle(0)) ///
        ysc(r(0.5 3.5) outergap(1)) ///
        scale(0.75)
    
    * 导出图形为 PNG 文件
    graph export "HW1Answer.png", as(png) name("Graph") replace
    Click image for larger version

Name:	1.png
Views:	1
Size:	62.9 KB
ID:	1775018



    However, when I run a similar piece of code, I get the result shown in Figure 2.
    Code:
    * 清除当前数据
    clear
    
    * 输入示例数据
    input str20(y x markers)
    "企业跨区交易" "电子商务发展"  "[1]tly25jjyj"
    "贸易成本" "电子商务发展"  "[2]jzs20jjyj"
    "居民消费" "电子商务发展"  "[3]ws25syjjyj"
    "企业跨区交易" "企业数字化水平"  "[4]yzc23cjyj"
    "贸易成本" "统一大市场"  "[5]hsy24zgsl"
    "居民消费" "数字经济发展"  "[6]bcw24dysj"
    end
    
    * 创建 X 轴标签,利用宏实现换行
    local linebreak = char(10)
    label define X 1 "电子商务发展" 3 "企业数字化水平" 5 "统一大市场" 7 "数字经济发展" 
    encode x, gen(X) 
    
    * 创建 Y 轴标签
    encode y, gen(Y) 
    labmask Y, values(y)
    
    * 绘制散点图
    scatter Y X, ///
        ytitle("") ///
        xtitle("") ///
        mcolor(none) ///
        mlab(markers) ///
        mlabpos(0) ///
        mlabsize(small) ///
        xlab(1 2 " " 3 4 " " 5 6 " " 7 8 " ", val labsize(small) grid) ///
        xsc(r(0 9)) ///
        ylab(1(1)3, val labsize(small) grid angle(0)) ///
        ysc(r(0.5 3.5) outergap(1)) ///
        scale(0.75)
    
    * 导出图形为 PNG 文件
    graph export "HW1Answer.png", as(png) name("Graph") replace
    Click image for larger version

Name:	2.png
Views:	1
Size:	63.4 KB
ID:	1775019


    I would like the final figure to look like Figure 1, but with the x-axis label as "企业数字化水平" instead of the shorter "企业数字化" (which might be the reason why the figure appears incorrectly).

    Could anyone help me resolve this issue?

    Thanks in advance!
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

  • #2
    Change
    Code:
     input str20(y x markers)
    to

    Code:
     input str25(y x markers)
    should fix the issue. The reason is that 企业数字化水平 has a byte length 21 which is greater than 20.

    Code:
    . di strlen("企业数字化水平")
    21
    If you use str20, the last character is trucated in the middle, which cause the encode x, gen(X) to code it as 8 instead of 3. See help ustrlen() for details.

    Code:
    . clear
    
    .
    . * 输入示例数据
    . input str20(y x markers)
    
                            y                     x               markers
      1. "企业跨区交易" "电子商务发展"  "[1]tly25jjyj"
      2. "贸易成本" "电子商务发展"  "[2]jzs20jjyj"
      3. "居民消费" "电子商务发展"  "[3]ws25syjjyj"
      4. "企业跨区交易" "企业数字化水平"  "[4]yzc23cjyj"
      5. "贸易成本" "统一大市场"  "[5]hsy24zgsl"
      6. "居民消费" "数字经济发展"  "[6]bcw24dysj"
      7. end
    
    .
    . list x in 4
    
         +---------------+
         |             x |
         |---------------|
      4. | 企业数字化水� |
         +---------------+
    
    .
    Last edited by Hua Peng (StataCorp); 29 Mar 2025, 13:15.

    Comment


    • #3
      Many thanks, Hua Peng, It works.
      Ho-Chuan (River) Huang
      Stata 17.0, MP(4)

      Comment

      Working...
      X