r - 错误ggplot(seq.int(0,to0 - from,by)错误): 'to' must be finite)

标签 r ggplot2

为了使用 ggplot2 绘制数据,我遇到了以下错误。 可用的命令、数据和错误类型如下:

require(ggplot2)
require(reshape2)
df <- data.frame(HMn25_30$avg,dt_hmn$dt)

df[3] = c(   "Normal",   "Normal",
             "Normal",
             "Normal",
             "Normal",
             "Normal",
             "Normal",
             "Normal",
             "Normal",
             "Normal",
             "Outlier",
             "Outlier",
             "Outlier",
             "Outlier",
             "Outlier",
             "Outlier",
             "Normal",
             "Outlier",
             "Outlier",
             "Normal",
             "Normal",
             "Outlier",
             "Outlier",
             "Normal",
             "Normal"
)
names(df)[1] <- 'Node 25'
names(df)[3] <-'Results'
df.m <- melt(df, names(df)[2:3], names(df)[1])
df.m$Results <- factor(df.m$Results)
df.m$dt_hmn.dt <- strptime(as.character(df.m$dt_hmn.dt), format = "%Y-%m-%d %H:%M")
p <- ggplot(df.m, aes(x = dt_hmn.dt, y = value, group = variable, color = variable))
p <- p + scale_shape_manual(values=c(27,20))
p <- p + geom_point(aes(shape = Results), cex=13, color= "blue")
p <- p + theme(axis.text.x = element_text(angle = 90, hjust = 1, size=13,color="darkred"))
p <- p + scale_color_manual(values=c("Red"))
p <- p + ylim(-1,8)
p <- p + theme_bw()
p <- p + xlab('Date and Time') 
p <- p + ylab('Temprature') 
p <- p + ggtitle("Temporal Outliers of Node 25 ") + theme(plot.title = element_text(lineheight=3, face="bold", color="black", size=29))
    p

数据是:

HMn25_30$avg
 [1]  0.280  0.208 -0.264 -0.480 -0.708 -0.714 -0.498 -0.216  0.126  0.574  1.042  1.086  1.820
[14]  4.570  3.808  7.400  5.572  5.402  6.288  4.966  5.180  2.380  4.710  5.366  4.766

dt_hmn$dt
 [1] 9/29/2007 23:00 9/30/2007 0:00  9/30/2007 1:00  9/30/2007 2:00  9/30/2007 3:00 
 [6] 9/30/2007 4:00  9/30/2007 5:00  9/30/2007 6:00  9/30/2007 7:00  9/30/2007 8:00 
[11] 9/30/2007 9:00  9/30/2007 10:00 9/30/2007 11:00 9/30/2007 12:00 9/30/2007 13:00
[16] 9/30/2007 14:00 9/30/2007 15:00 9/30/2007 16:00 9/30/2007 17:00 9/30/2007 18:00
[21] 9/30/2007 19:00 9/30/2007 20:00 9/30/2007 21:00 9/30/2007 22:00 9/30/2007 23:00

我遇到了以下错误:

Error in seq.int(0, to0 - from, by) : 'to' must be finite

最佳答案

您代码中的第一个问题是 striptime() 中的 format= 参数格式错误 - 它与实际日期格式不符。它应该是 format = "%m/%d/%Y %H:%M"

df.m$dt_hmn.dt <- strptime(as.character(df.m$dt_hmn.dt), format = "%m/%d/%Y %H:%M")

接下来是 ggplot() 代码 - 如果您为 geom_point() 中的所有点设置 color="blue",您不会不需要使用 scale_color_manual() 并在 aes() 中使用 color=variable。对于 scale_shape_manual() 没有形状 27,将其更改为 17

如果您使用 theme_bw(),则其他 theme() 调用应放在此行之后,以确保 theme_bw() 获胜'覆盖你的参数。

ggplot(df.m, aes(x = dt_hmn.dt, y = value, group = variable)) + 
  geom_point(aes(shape = Results), cex=13, color= "blue")+
  scale_shape_manual(values=c(17,20))+
  ylim(-1,8)+
  theme_bw()+
  xlab('Date and Time') +
  ylab('Temprature')+
  ggtitle("Temporal Outliers of Node 25 ") + 
  theme(plot.title = element_text(lineheight=3, face="bold", color="black", size=29))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1, size=13,color="darkred"))

enter image description here

关于r - 错误ggplot(seq.int(0,to0 - from,by)错误): 'to' must be finite),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15897087/

相关文章:

r - data.table 中的唯一标识符

r - 在 R 中保存高分辨率图像

r - ggplot 中的自动异常值标记

r - 无法使用 scales 包格式化 xaxis

r - 在 geom_tile ggplot2 中格式化堆叠的 geom_bar 的内部线

r - R 中的 Azure 成本管理数据

r - Shiny 的嵌套/多个对话框

R 在参数包含数组和标量时使用 apply

r - 从 Kruskal-Wallis 输出中提取 p 值

r - 如何将 geom_text 标签对齐到躲避的 geom_col 上方