简体   繁体   中英

Add comma separator to axis labels

I have the following example where I am trying to add a comma seperator to numbers greater that 999(ie 1,000 2,000 3,000 ...)

#Dummy data
Data1 <- data.frame(flow = c(8000,8.5,6,7.1,9), SP_elev = c(20,11,5,25,50))
Data2 <- data.frame(flow = c(7000,7.2,6.5,8.2,8.5), SP_elev = c(13,15,18,25,19))
Data3 <- data.frame(flow = c(2000,3,5,7,9), SP_elev = c(20,25,28,30,35))
Data4 <- data.frame(flow = c(1000,4,6,8,9), SP_elev = c(13,15,18,25,19))
Data5 <- data.frame(flow = c(1000,4,6,8,9), SP_elev = c(13,15,18,25,19))
Data6 <- data.frame(flow = c(1000,4,6,8,9), SP_elev = c(22,23,25,27,29))

#Create Vector list (in place of list.files)
dataframes = list("Data1" = Data1, 
                  "Data2" = Data2, 
                  "Data3" = Data3,
          "Data4" = Data4,
          "Data5" = Data5,
          "Data6" = Data6)

# open the PDF device
pdf(file="Dummy_Example.pdf", paper="letter", height=10, width=8)

#Create array of plots 
par(mfrow=c(3,2))

#plot a with regression model
for (i in dataframes) {

plot (i[,c('flow', 'SP_elev')], xlab=expression(paste("Discharge (", ft^3, "/s)",sep = "")), ylab= "Elevation (m)", tck=0.02, adj = 0.5)
axis(side=1, at=axTicks(1), labels=formatC(axTicks(1), format="d", big.mark=','))

}

# close the PDF device
dev.off()

The x-axis labels are being plotted twice on the output PDF. Where am i going wrong in the axis command in the above code?

#Dummy data
Data1 <- data.frame(flow = c(8000,8.5,6,7.1,9), SP_elev = c(20,11,5,25,50))
Data2 <- data.frame(flow = c(7000,7.2,6.5,8.2,8.5), SP_elev = c(13,15,18,25,19))
Data3 <- data.frame(flow = c(2000,3,5,7,9), SP_elev = c(20,25,28,30,35))
Data4 <- data.frame(flow = c(1000,4,6,8,9), SP_elev = c(13,15,18,25,19))
Data5 <- data.frame(flow = c(1000,4,6,8,9), SP_elev = c(13,15,18,25,19))
Data6 <- data.frame(flow = c(1000,4,6,8,9), SP_elev = c(22,23,25,27,29))

#Create Vector list (in place of list.files)
dataframes = list("Data1" = Data1, 
                  "Data2" = Data2, 
                  "Data3" = Data3,
          "Data4" = Data4,
          "Data5" = Data5,
          "Data6" = Data6)

# open the PDF device
pdf(file="Dummy_Example.pdf", paper="letter", height=10, width=8)

#Create array of plots 
par(mfrow=c(3,2))

#plot a with regression model
for (i in dataframes) {

plot (i[,c('flow', 'SP_elev')], xlab=expression(paste("Discharge (", ft^3, "/s)",sep = "")), ylab= "Elevation (m)", tck=0.02, adj = 0.5, xaxt='n')
axis(side=1, at=axTicks(1), labels=formatC(axTicks(1),  big.mark=','))
}


# close the PDF device
dev.off()

To get the axis labels not not appear twice, xaxt='n' needs to be added to the plot function. The axis labels are formatted and plotted when axis is called in the for loop.

Would the block starting with:

for (i in dataframes) {  

Have any bearing on how many time axis() is being called?

I suggest moving the line:

axis(side=1, at=axTicks(1), labels=formatC(axTicks(1), format="d", big.mark=','))  

Outside the for loop, like this:

for (i in dataframes) 
{
    plot (i[,c('flow', 'SP_elev')], xlab=expression(paste("Discharge (", ft^3, "/s)",sep = "")), ylab= "Elevation  m)", tck=0.02, adj = 0.5)
}
axis(side=1, at=axTicks(1), labels=formatC(axTicks(1), format="d", big.mark=','))  

Also, You can try calling argumnent axes=F in the additional calls to plot() to suppress the multiple plotting of the x-axis.

See the link here for more on controlling axes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM