将逗号分隔符添加到轴标签

时间:2014-04-15 19:39:01

标签: r plot axis-labels

我有以下示例,我试图将逗号分隔符添加到大于999的数字(即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()

x轴标签在输出PDF上绘制两次。我在上面的代码中的axis命令中出错了什么?

2 个答案:

答案 0 :(得分:4)

#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()

要使轴标签不会出现两次,需要将xaxt='n'添加到plot功能中。在for循环中调用axis时,会对轴标签进行格式化和绘制。

答案 1 :(得分:1)

该块是否以:

开头
for (i in dataframes) {  

与被调用的时间axis()有关吗?

我建议移动这条线:

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

在for循环之外,像这样:

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=','))  

此外,您可以尝试 在plot()的附加调用中调用argumnent axes = F来抑制x轴的多重绘图。

有关控制轴的更多信息,请参阅 link here