X轴未显示R

时间:2018-11-14 15:16:16

标签: r

我试图将两个不同的Y轴与相同的X轴一起使用,并且当我将两个轴都设置为false时,年份将不会显示

library(lubridate)

x <- dataset$Date
y <- dataset$AvgCostPerKwh
z <- dataset$ActualkWhSold


par(mar=c(5, 4, 4, 6) + 0.1)

plot(year(x),y, pch = 16, axes = FALSE, ylim = c(0.030,0.090), xlab = "",  ylab = "",
 type = "b", col="black", main = "Wholesale Power cost")

axis(2, ylim =(range(c(y))), col = "black", las =1)

mtext("$ per KWh", side = 2, line = 2.5)
box()

par(new = TRUE)


plot(year(x),z, pch = 15, xlab = "", ylab = "",ylim=c(5000000,45000000), 
 axes = FALSE, type="b", col="red")

mtext("Kwh's Sold", side=4, col="red", line=4)
axis(4, ylim=(range(c(z))), col = "red", las=1)



mtext("Year", side = 1, col="black",line=2.5)

legend("topleft", legend = c("AvgCostPerKwh", "ActualKwhSold"),
   text.col = c("black", "red"), 
   pch=c(15,15),col=c("black", "red"))

Image 1

当我将其中一个图设置为true时,我会在一侧获得重叠的值,但底部显示的年份会显示出来。

library(lubridate)

x <- dataset$Date
y <- dataset$AvgCostPerKwh
z <- dataset$ActualkWhSold


par(mar=c(5, 4, 4, 6) + 0.1)

plot(year(x),y, pch = 16, axes = TRUE, ylim = c(0.030,0.090), xlab = "", ylab = "",
 type = "b", col="black", main = "Wholesale Power cost")

axis(2, ylim =(range(c(y))), col = "black", las =1)

mtext("$ per KWh", side = 2, line = 2.5)
box()

par(new = TRUE)


plot(year(x),z, pch = 15, xlab = "", ylab = "",ylim=c(5000000,45000000), 
 axes = FALSE, type="b", col="red")

mtext("Kwh's Sold", side=4, col="red", line=4)
axis(4, ylim=(range(c(z))), col = "red", las=1)



mtext("Year", side = 1, col="black",line=2.5)

legend("topleft", legend = c("AvgCostPerKwh", "ActualKwhSold"),
   text.col = c("black", "red"), 
   pch=c(15,15),col=c("black", "red"))

image 2

我不确定,我在这里遵循了其他有关2 Y轴的示例,但我无法继续工作。

1 个答案:

答案 0 :(得分:0)

我已经对有兴趣的人回答了我自己的问题,我忘了添加

year(as.Date(dataset$Date, format = "%m/%d/%Y"),"%Y")

在添加之前位于顶部

plot(year(x), y, pch=16, axes=FALSE, ylim=c(0.030,1), xlab="", ylab="", 
 type="b",col="black", main="Wholesale Power Cost")