I'm tryng to plot with plot
the following:
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 (%)")
The problem is that I get on the x-axis numbers from 1 to 27.
I would like to have days there from Feb 25 to March 22. So, I tried:
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)
The problem is that the new x-label is written on the old one.
How can I fix it?
Thanks
# 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()
Hi Rollo,
In base r plot, I prefer to insert each thing separately. I recreated your data example and plot the data again. I plotted a blue line with a new example of data, created by adding 2 to the original DF data, it was stored in df$data_line.
When I called the plot, I used the argument 'axes = F', to omit the original axes. After this I called the two axes, with different functions.
In r base plot you can't use "+" between functions when you open a plot.
Hope my code helps you!
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加