1

I am trying to pull charts (.png) from the data from SQL Server using ODBC in R. The code is running fine but label on y-axis is in decimals. eg. 10.0%, 20.0%, 30.0%, 40.0% and 50.0%.

I do not want this in decimal place on y-axis. Please help me to get rid of this issue.

Please see my below code and help me with this issue.

Output Chart

# produce plots for all the pids 
for (i in 1:n_pids){
  print(paste0('working on ', i, ' pid ', def_dates$pid_names[i]))
  pidx <- def_dates[i,'pid']
  sub <- res[pid == pidx]

  png(file = paste0(pidx, '-misrated.png'), width = 750, height = 350, units = "px", pointsize = 12 )
  par(oma = c(5, 1,1,1), mar=c(0,3,3,3))
  with(sub, plot(date, edf1/100,
                 type="l", col="blue", 
                 ylim=c(0,.5), bty='n', 
                 xlab=NA, ylab=NA, cex=0.9,lwd=2,
                 axes=F,xaxs = "i",yaxs="i",
                 panel.first = 
                   c(abline(h = seq(0,1,.1), lty = 2, col = 'grey') 
                     ,NULL)

  ))
  abline(v=as.Date(def_dates[def_dates$pid == pidx,'def'],'%m-%d-%Y'),
         col="red",lwd=2)

  axis.Date(1, at = seq(min(sub$date), max(sub$date), length.out = 10),
            format= "%b %Y") # bottom dates

  var1= seq(0,0.5,.1)#pretty_breaks(n=5)(sub$edf1/100)
  axis(2, lwd=0,
       at=var1,
       label=percent(var1),
       las=1) # EDF values
  par(new=T)
  with(sub, plot(date, displaystring, type="l",
                 col="darkgreen", lwd=2,
                 axes=F,  xlab=NA, ylab=NA))
  axis(side = 4, 
       at=seq(1,NROW(unique(sub$displaystring))),
       labels=unique(sub$displaystring),
       lwd=0,cex=0.5,las=1,
       col="darkgreen"
  )
  par(oma = c(0,0,0,0))

  legend("bottom", legend=c("DENBURY RESOURCES INC [1-Yr EDF9]","MIS RATING"), xpd = TRUE, 
         horiz = TRUE,
         inset = c(0,0), bty = "n",
         col=c("blue", "darkgreen"), lty=1, cex = 1)

  dev.off()

    #ggsave(paste0(pidx, '-rated.png'), p, scale=3, height=3, width=6, unit="in")

}
  • Hi welcome to Stack overflow. Could you make your post a little more transparent by reducing it to a minimal example of your problem? Be sure that anybody here can reproduce your issue by providing a [minimal working example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Thanks colleague! :) – jay.sf Dec 05 '18 at 06:51
  • Your question is about the *plot*, so we do not need to know about how you get the data, so please remove all db-related ops. My guess, though, is in one line: `label=percent(var1)`. Perhaps if you investigate (1) options to `percent` (I don't know ... where is it defined? No idea), or (2) change `var` yourself into the values you want displayed on the axis. – r2evans Dec 05 '18 at 06:53
  • BTW: `require` in this place does you no good: if you run this script/file and a package is not available, your script will try to run anyway (with no idea). If you use `library`, you will know immediately (it will `stop`). If you really want to use `require`, then capture its output and do something with it. (If you look at the source code for `require`, you can see that it is calling `library` under the hood and catching (potentially silently) the error.) https://stackoverflow.com/a/51263513/3358272 – r2evans Dec 05 '18 at 06:56

1 Answers1

1

I can't vouch for the rest of the code, but the only portion that has affect on the y-axis here is:

  axis(2, lwd=0,
       at=var1,
       label=percent(var1),
       las=1) # EDF values

When you find scales::percent, the help page does not appear to give an easy way to control the decimal places. I suggest we use good-ole sprintf for this.

scales::percent(var1)
# [1] "0.0%"  "10.0%" "20.0%" "30.0%" "40.0%" "50.0%"

sprintf("%0.0f%%", 100*var1)
# [1] "0%"  "10%" "20%" "30%" "40%" "50%"

So your resulting code will be:

  axis(2, lwd=0,
       at=var1,
       label=sprintf("%0.0f%%", 100*var1),
       las=1) # EDF values
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Thank you so much, This resolved my issue with the code. – Ashish Talgotra Dec 05 '18 at 07:25
  • I have lengends on the bottom of the chart - legend("bottom", legend=c("BANCA CARIGE SPA - CASSA DI RISPARM [1-Yr EDF9]","MIS RATING"), xpd = TRUE, horiz = TRUE, inset = c(0,0), bty = "n", col=c("blue", "darkgreen"), lty=1, cex = 1) How to make them to center of the chart? – Ashish Talgotra Dec 05 '18 at 07:40
  • (Odd, I added this comment yesterday and refreshed to find it there ... now it's gone. Odd.) If you read `?legend`, you can see that it accepts as the first two arguments `x` and `y` which, if both provided and numeric, will place it on the user coordinates provided. This allows you to place it precisely within the plot space. Occasionally, I've had use to add `xpd=NA` for the box to not be clipped by the plot border, but with this you occasionally need to play with `par(mar=...)` ahead of time. – r2evans Dec 06 '18 at 21:05