Announcement

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

  • Tim Morris
    started a topic Including line break in variable labels

    Including line break in variable labels

    Dear Stata List

    I am trying to include a line break in a variable label, but cannot quite make it work as I want. Below is a minimal example where I try to include a linebreak and it works for the axis label but not for the marker label:
    Code:
    sysuse auto
    lab def origin 0 `" "Domestic" "car" "' 1 `" "Foreign" "car" "', modify
    scatter price foreign, mlab(foreign) xlab(0 1) xsca(range(0 1.5))
    Is there a correct way to do this?

    Thanks, Tim

  • Andrew Musau
    replied
    Thanks to Masaru Nagashima and this thread https://www.statalist.org/forums/for...ng-but-not-pdf there appears to be a resolution to Tim Morris's question in #1 - at least if we are outputting the figure in PNG format. I should add that this works in Windows. I do not know if it is the case for other operating systems.

    Code:
    sysuse auto, clear
    lab def origin 0 "Domestic`=char(13)'`=char(10)'car"  1 "Foreign`=char(13)'`=char(10)'car", modify
    scatter price foreign, mlab(foreign) xlab(0 1) xsca(range(0 1.5)) scheme(s1mono)
    Res.:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	37.4 KB
ID:	1636990

    Last edited by Andrew Musau; 17 Nov 2021, 12:26.

    Leave a comment:


  • Maarten Buis
    replied
    Originally posted by Belinda Foster View Post

    Anything appears to be impossible until it is actually done!
    That is not my experience. It is way more often that I don't do something because I don't think it is worth the effort. Here I think it is actually impossible, unless you are willing to give up on the goal that the label should work in multiple contexts. Tim seems to prefer that, I prefer to keep the program clean and solve the problem of multiple lines in graphs within graph command, as Nick suggested in #11.

    Leave a comment:


  • Belinda Foster
    replied
    Originally posted by Maarten Buis View Post
    I suspect that this is not just a matter of "difficult", but a matter of "impossible" to reconcile all the different ways in which value labels are used.
    Anything appears to be impossible until it is actually done!

    Leave a comment:


  • Belinda Foster
    replied
    Originally posted by Nick Cox View Post
    Beyond work-arounds, perhaps the most valuable addition would be that any graph option that handles a value label might have an extra option that specifies a maximum preferred chunk size or a preferred number of chunks.
    I agree. Another item for the wishlist thread!

    Leave a comment:


  • Nick Cox
    replied
    I'm surprised that no one has mentioned

    Code:
    . ssc desc splitvallabels
    
    ----------------------------------------------------------------------------------------
    package splitvallabels from http://fmwww.bc.edu/repec/bocode/s
    ----------------------------------------------------------------------------------------
    
    TITLE
          'SPLITVALLABELS': module to split up value labels for multi-line graph labelling
    
    DESCRIPTION/AUTHOR(S)
          
          splitvallabels splits the value labels for a variable into
          multiple chunks, and returns those chunks in a form appropriate
          for specification as part of a graph command. When the over()
          variable in, for example, a bar graph has long labels, these
          labels can overlap each other in the graph.  This command breaks
          up these long labels and packages them in a format appropriate
          for specification as part of a relabel option. This will create
          multi-line labels on the graph.
          
          KW: graphics
          KW: labels
          
          Requires: Stata version 8.2
          
          
          Author: Nick Winter,  University of Virginia
          Support: email  [email protected]
          
          Author: Ben Jann, ETH Zurich
          Support: email  [email protected]
          
          Distribution-Date: 20080814
    
    INSTALLATION FILES                       (type net install splitvallabels)
          splitvallabels.ado
          splitvallabels.hlp
    ----------------------------------------------------------------------------------------
    (type ssc install splitvallabels to install)
    Beyond work-arounds, perhaps the most valuable addition would be that any graph option that handles a value label might have an extra option that specifies a maximum preferred chunk size or a preferred number of chunks.

    Leave a comment:


  • Tim Morris
    replied
    The implementation may be difficult but this does not mean that StataCorp should not try to address this issue. Personally I would find it incredibly useful.
    I agree Belinda Foster

    Maarten Buis
    I may have misunderstood you, but I do not see the fact that value labels are used in different ways as making this an impossible or unreasonable task. I happily include things like "{&theta}{sub:1}" as a value label, which is interpreted differently according to context. Take my labels above. One would use these labels only to include a line-break and there is no ambiguity in the user's intention. Further, I do not think there is any context in which the line-breaking would be desired besides a graph and would not complain if it looked odd in other contexts.
    In what context do you want to use this?
    I had a bar chart and wanted to put the category labels at the top of the bar with the numbers beneath the labels. More generally I would want to do this when labelling points with value labels containing more than one or two words.

    twoway already handles a text chunk (made up of a single line string) and typically puts it in a reasonable place (which involves measuring the height and length of the text for the chosen typeface, weight and size); why could it not also count the number of lines in the label and treat the whole text chunk in the same way? It handled the axis labels nicely enough in my example by placing the top of the text chunk below the axis line and the rest beneath. Andrew Musau provides a nice workaround (thanks), but because it does not treat the whole chunk at once, one could get words overlapping, so this requires care from the user. I want this to work even when I am careless!

    Leave a comment:


  • Maarten Buis
    replied
    Originally posted by Belinda Foster View Post
    The implementation may be difficult but this does not mean that StataCorp should not try to address this issue. Personally I would find it incredibly useful.
    I suspect that this is not just a matter of "difficult", but a matter of "impossible" to reconcile all the different ways in which value labels are used. In what context do you want to use this? Maybe we can find a more practical way.

    Leave a comment:


  • Andrew Musau
    replied
    There is so much flexibility with twoway that you leave on the table. Based on your example (#1), here is a workaround to the line break issue but personally, I do not see much use of long labels if you have too many markers. The graph just ends up looking cluttered.

    Code:
    sysuse auto, clear
    gen foreign2= foreign
    lab def origin2 0 "   car" 1 "   car", modify
    label values foreign2 origin2
    twoway (scatter price foreign, mlab(foreign)mlabposition(3) mlabcolor(navy) xlab(0 1) ///
     xsca(range(0 1.5)))(scatter price foreign, mlab(foreign2) mlabposition(4) ///
     mcolor(navy) mlabcolor(navy) xlab(0 1) xsca(range(0 1.5))), leg(on order(1))
    Click image for larger version

Name:	lbreak.png
Views:	1
Size:	102.7 KB
ID:	1423036

    Leave a comment:


  • Belinda Foster
    replied
    Originally posted by Maarten Buis View Post
    I suspect that the trick you showed in the beginning is just an unintentional consequence of how the labels are stored and how twoway parses those strings.
    I think this is the most plausible explanation of why this is allowed in one context but not another.

    Originally posted by Maarten Buis View Post
    The labels are used in many different contexts, and a linebreak may make sense in one context but not in another. So, I suspect that implementing that will cause many more problems than it solves. As a consequence I don't expect it will be implemented. I could be wrong, I have no inside knowledge about what is going on inside StataCorp.
    The implementation may be difficult but this does not mean that StataCorp should not try to address this issue. Personally I would find it incredibly useful.

    Leave a comment:


  • Maarten Buis
    replied
    The labels are used in many different contexts, and a linebreak may make sense in one context but not in another. So, I suspect that implementing that will cause many more problems than it solves. As a consequence I don't expect it will be implemented. I could be wrong, I have no inside knowledge about what is going on inside StataCorp.

    I suspect that the trick you showed in the beginning is just an unintentional consequence of how the labels are stored and how twoway parses those strings.

    Leave a comment:


  • Tim Morris
    replied
    Thanks Maarten. Do you think this is just something that hasn't been done yet, or is there some fundamental difficulty?

    Leave a comment:


  • Maarten Buis
    replied
    I don't think there is a concept of a "line" for a label; it is just a string. So I don't think it can be done.

    Leave a comment:


  • Tim Morris
    replied
    Belinda Foster Sadly not. Presumably StataCorp have some reason why axis labels can include line breaks but marker labels can not.
    I wonder if Chinh Nguyen (StataCorp) can shed any light?

    Leave a comment:


  • Belinda Foster
    replied
    Tim Morris Did you ever find a solution to this?

    Leave a comment:

Working...
X