我正在尝试绘制plot
以下内容:
df = (DF = c(24, 62, 37, 27, 50, 20, 23, 23, 25, 20, 27, 25, 24, 11, 23, 21, 17, 20, 17, 13, 13, 13, 15, 15, 14, 10))
plot(df$DF, type = "o", col = "red", lwd = 2, xlab = "", ylab = "Growth Rate (%)")
问题是我得到的x轴数字是1到27。
我想在那里从2月25日到3月22日休息。所以,我尝试了:
df$Date <- seq(as.Date("2020/02/25"), as.Date("2020/03/22"), by = "days")
plot(df$DF, type = "o", col = "red", lwd = 2, xlab = "", ylab = "Growth Rate (%)") + axis(1, at=1:27, labels= df$Date, cex.axis=0.5)
问题是新的x标签写在旧的x标签上。
我该如何解决?
谢谢
# Create the data
df = data.frame(DF = c(24, 62, 37, 27, 50, 20, 23, 23, 25, 20, 27, 25, 24, 11, 23, 21, 17, 20, 17, 13, 13, 13, 15, 15, 14, 10))
df$Date <- seq(as.Date("2020/02/25"), as.Date("2020/03/21"), by = "day")
df$data_line <- c(24, 62, 37, 27, 50, 20, 23, 23, 25, 20, 27, 25, 24, 11, 23, 21, 17, 20, 17, 13, 13, 13, 15, 15, 14, 10) + 2
# Open the plot area
plot(df$Date, df$DF,
type = "o", col = "red", lwd = 2, xlab = "",ylab = "Growth Rate (%)", axes = F)
# Configuring the new axes
axis.Date(1, at = seq(min(df$Date), max(df$Date), by ="day"), cex.axis= 0.5)
axis(2, las = 2)
# Add a new line
lines(df$Date, df$data_line, col = "blue")
# Close the box around the plot area
box()
嗨,罗洛,
在基本绘图中,我更喜欢分别插入每个内容。我重新创建了您的数据示例,然后再次绘制数据。我绘制了一条蓝线,上面有一个新的数据示例,该示例通过将2添加到原始DF数据中创建,并存储在df $ data_line中。
当我调用绘图时,我使用参数“ axes = F”来省略原始轴。之后,我将两个轴称为功能不同的轴。
在r基本绘图中,打开绘图时不能在函数之间使用“ +”。
希望我的代码对您有所帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句