将 R 条形图转换为绘制 2 条线并添加数据点
Convert an R barplot to plot 2 lines and add data points
我需要绘制一个 2 行 R 程序图,其中一条线上有数据点,最小 Y 值设置为 -6。以下数据为.csv输入文件:
> cat verifyRecording.csv
Time,MaxDBK,DBChange
07:30,20,2
07:35,21,1
07:40,22,0
07:45,23,-1
07:50,24,-2
07:55,32,-5
08:00,19,3
下面是一个 R 程序,它接受上面的 .csv 输入并输出一个包含 2 条线的条形图。我只需要 2 条线,所以它需要将 barplot 转换为 2 线(绘图)图表。然后在DBChange线上添加d1$DBChange数据点,设置最小Y值为-6。
#!/usr/bin/Rscript
d1 <- read.csv(file="verifyRecording.csv",head=T,sep=",")
# Provide an image size which will ensure the x labels display
png(filename="verifyRecording.png", width=1024, bg="white")
# Replace the barplot function with a plot function.
# Fix the Y values be to actually show -6 as the minimum value.
mp <- barplot(d1$MaxDBK, ylim=c(-6,50), main="Sound Recording started: 05/21/2017 7:25 AM", xlab="Time in 24hr", ylab="Sound in Decibals", border='blue')
axis(1,at=mp,labels=d1$Time)
lines(mp,d1$DBChange,type="o",pch=19,lwd=2,col="red")
lines(mp,d1$MaxDBK,type="o",pch=19,lwd=2,col="blue")
# Display the DBChange data values on the d1$DBChange line points.
##### points(d1$Time, d1$DBChange, type="l", col="red")
legend("topright", c("Recommended_DB_Change","Max_DB_Volume"), lty=c(1,1), lwd=c(2.5,2.5), col=c("red","blue"))
dev.off()
如果您从空图而不是条形图开始,您可以获得只有线条的图。这样,您只需在其中添加所需的元素即可。
# Empty plot ("type = "n")
# xaxt = "n" in place to allow labelling with axis
# Set y axis limits
plot(1:nrow(d1), d1$MaxDBK, type = "n", ylim = c(-6, 50), xaxt = "n",
main = "Sound Recording started: 05/21/2017 7:25 AM",
xlab = "Time in 24hr",
ylab = "Sound in Decibals")
# Lines added
lines(d1$Time, d1$MaxDBK,type = "o", pch = 19, lwd = 2, col = "blue")
lines(d1$Time, d1$DBChange,type = "o", pch = 19, lwd = 2, col = "red")
# Label added in x axis. Changed at value to show properly
axis(side = 1, labels = d1$Time, at = d1$Time)
# Label each point with the values in d1$DBChange
text(d1$Time, d1$DBChange, labels = d1$DBChange, pos = 3)
我需要绘制一个 2 行 R 程序图,其中一条线上有数据点,最小 Y 值设置为 -6。以下数据为.csv输入文件:
> cat verifyRecording.csv
Time,MaxDBK,DBChange
07:30,20,2
07:35,21,1
07:40,22,0
07:45,23,-1
07:50,24,-2
07:55,32,-5
08:00,19,3
下面是一个 R 程序,它接受上面的 .csv 输入并输出一个包含 2 条线的条形图。我只需要 2 条线,所以它需要将 barplot 转换为 2 线(绘图)图表。然后在DBChange线上添加d1$DBChange数据点,设置最小Y值为-6。
#!/usr/bin/Rscript
d1 <- read.csv(file="verifyRecording.csv",head=T,sep=",")
# Provide an image size which will ensure the x labels display
png(filename="verifyRecording.png", width=1024, bg="white")
# Replace the barplot function with a plot function.
# Fix the Y values be to actually show -6 as the minimum value.
mp <- barplot(d1$MaxDBK, ylim=c(-6,50), main="Sound Recording started: 05/21/2017 7:25 AM", xlab="Time in 24hr", ylab="Sound in Decibals", border='blue')
axis(1,at=mp,labels=d1$Time)
lines(mp,d1$DBChange,type="o",pch=19,lwd=2,col="red")
lines(mp,d1$MaxDBK,type="o",pch=19,lwd=2,col="blue")
# Display the DBChange data values on the d1$DBChange line points.
##### points(d1$Time, d1$DBChange, type="l", col="red")
legend("topright", c("Recommended_DB_Change","Max_DB_Volume"), lty=c(1,1), lwd=c(2.5,2.5), col=c("red","blue"))
dev.off()
如果您从空图而不是条形图开始,您可以获得只有线条的图。这样,您只需在其中添加所需的元素即可。
# Empty plot ("type = "n")
# xaxt = "n" in place to allow labelling with axis
# Set y axis limits
plot(1:nrow(d1), d1$MaxDBK, type = "n", ylim = c(-6, 50), xaxt = "n",
main = "Sound Recording started: 05/21/2017 7:25 AM",
xlab = "Time in 24hr",
ylab = "Sound in Decibals")
# Lines added
lines(d1$Time, d1$MaxDBK,type = "o", pch = 19, lwd = 2, col = "blue")
lines(d1$Time, d1$DBChange,type = "o", pch = 19, lwd = 2, col = "red")
# Label added in x axis. Changed at value to show properly
axis(side = 1, labels = d1$Time, at = d1$Time)
# Label each point with the values in d1$DBChange
text(d1$Time, d1$DBChange, labels = d1$DBChange, pos = 3)