R:融化 data.frame 以在 ggplot2 中用于多个 y 值图
R: melting a data.frame to use in ggplot2 for multiple y-value plot
我想根据时间变量绘制多个 Y 值。我没有完整的数据框,因为我没有收集所有的 Y 值。我相信我需要在将数据框传递给 ggplot2 之前对其进行 melt(),但这似乎失败了,因为 "id" 列并不总是被填充。这是我尝试过的,严重损坏的 ggplot最后:
library("ggplot2")
library("reshape2")
#Example data
Date=c("2015-06-09", "2015-06-09", "2015-06-09")
Time=c("07:00:01", "08:00:01", "09:00:01")
src=c(47420413232, 47519749372, 47571637056)
dest=c(NA, NA, 49738231808)
df = data.frame(Date, Time, src, dest)
# process into a real "time" type, throw away the previous types
df$time = as.POSIXlt(paste(df$Date, df$Time))
df$Date = NULL
df$Time = NULL
# review df to check it's sane
df
# melt the data to transform it for plotting
molten_space = melt(df, id.vars="time")
# now look at molten_space for what I think goes wrong
molten_space
# doesn't work at all, but basically I want to plot both src(y-value) and dest(y-value) against time(x-value)
ggplot(data=molten_space, aes(x=time, y=value, group="time")) + geom_line() + scale_y_continuous("Space Used")
#ggplot(data=molten_space, aes(x=time, y=Bytes, group="time")) + geom_line() + scale_y_continuous("Space Used", labels=f2si)
这里有两个问题。
第一个问题是您使用生成列表的 as.POSIXlt
而不是生成向量的 as.POSIXct
。因此 melt
无法正常工作。试试这个:
df$time <- as.POSIXct(paste(df$Date, df$Time))
第二个问题是您对用于 x 轴的同一个变量进行分组,这对我来说意义不大。试试这个:
ggplot(data=molten_space, aes(x=time, y=value, color=variable)) +
geom_point() + geom_line() + scale_y_continuous("Space Used")
旁注
as.POSIXlt
的用法通常是从数据中提取不同的成分。
R> unlist(as.POSIXlt(df$time[1]))
sec min hour mday mon year wday yday isdst zone gmtoff
"1" "0" "7" "9" "5" "115" "2" "159" "1" "CEST" "7200"
我想根据时间变量绘制多个 Y 值。我没有完整的数据框,因为我没有收集所有的 Y 值。我相信我需要在将数据框传递给 ggplot2 之前对其进行 melt(),但这似乎失败了,因为 "id" 列并不总是被填充。这是我尝试过的,严重损坏的 ggplot最后:
library("ggplot2")
library("reshape2")
#Example data
Date=c("2015-06-09", "2015-06-09", "2015-06-09")
Time=c("07:00:01", "08:00:01", "09:00:01")
src=c(47420413232, 47519749372, 47571637056)
dest=c(NA, NA, 49738231808)
df = data.frame(Date, Time, src, dest)
# process into a real "time" type, throw away the previous types
df$time = as.POSIXlt(paste(df$Date, df$Time))
df$Date = NULL
df$Time = NULL
# review df to check it's sane
df
# melt the data to transform it for plotting
molten_space = melt(df, id.vars="time")
# now look at molten_space for what I think goes wrong
molten_space
# doesn't work at all, but basically I want to plot both src(y-value) and dest(y-value) against time(x-value)
ggplot(data=molten_space, aes(x=time, y=value, group="time")) + geom_line() + scale_y_continuous("Space Used")
#ggplot(data=molten_space, aes(x=time, y=Bytes, group="time")) + geom_line() + scale_y_continuous("Space Used", labels=f2si)
这里有两个问题。
第一个问题是您使用生成列表的 as.POSIXlt
而不是生成向量的 as.POSIXct
。因此 melt
无法正常工作。试试这个:
df$time <- as.POSIXct(paste(df$Date, df$Time))
第二个问题是您对用于 x 轴的同一个变量进行分组,这对我来说意义不大。试试这个:
ggplot(data=molten_space, aes(x=time, y=value, color=variable)) +
geom_point() + geom_line() + scale_y_continuous("Space Used")
旁注
as.POSIXlt
的用法通常是从数据中提取不同的成分。
R> unlist(as.POSIXlt(df$time[1]))
sec min hour mday mon year wday yday isdst zone gmtoff
"1" "0" "7" "9" "5" "115" "2" "159" "1" "CEST" "7200"