'Displaying percentages on Y-axis

How can I display % on Y-axis? I can edit the values in the Graph Editor but don't know how this can be done via a script as I am creating several graphs in a loop and tick values change with graphs.

clear
input yr v1
2005 77.01
2006 84.01
2007 83.01
2008 85.01
2009 86.01
2010 83.01
2011 98.01
2012 80.01
2013 79.01
end
graph twoway connected v1 yr

Actual Actual

Expected enter image description here



Solution 1:[1]

enter image description here

My previous answer was a bit messy given edits.

Here is a fresh self-contained answer based on nicelabels (on SSC since 10 May 2022) and mylabels (on SSC for some while, perhaps 2003).

Let's start by noting that adding % signs is not part of any official display format. So, we have to do it in our own code.

clear
input yr v1
2005 77.01
2006 84.01
2007 83.01
2008 85.01
2009 86.01
2010 83.01
2011 98.01
2012 80.01
2013 79.01
end

nicelabels v1, local(yla)
if wordcount("`yla'") < 5 nicelabels v1, local(yla) nvals(10)
mylabels `yla', suffix(%) local(yla) 

twoway connected v1 yr , yla(`yla')

So nicelabels is asked to suggest nice labels for v1. If the number suggested is < 5 it is told to try again. Once those labels exist, they are pushed through mylabels for adding % to each. The process needs no user intervention.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1