简体   繁体   English

如何在ggplot2中绘制具有多列的data.frame?

[英]How to plot data.frame with multiple columns in ggplot2?

I have a data frame that looks roughly like this: 我有一个大致如下所示的数据框:

aa <- c(1:7)
bb <- c(11:15)
df1 <- expand.grid(aa, bb)
val1 <- rnorm(nrow(df1))
val2 <- runif(nrow(df1))

df <- data.frame(df1, val1, val2)
names(df) <- c("aa", "bb", "val1", "val2")

What I want to do: For a fixed aa (say, 1), there is a time series of val1 and val2 for all values of bb. 我想做什么:对于固定的aa(例如1),对于bb的所有值都有一个时间序列val1和val2。 Now, I would like to plot these (for aa = 1 these are 5 for each val1 and val2) time series. 现在,我想绘制这些时间序列(对于aa = 1,每个val1和val2均为5)。 (so in total 7*5*2 time series) (因此总共7 * 5 * 2时间序列)

How can I do this with ggplot2? 我该如何使用ggplot2?

I tried the following: 我尝试了以下方法:

require(ggplot2)
require(reshape2)

df_pl <- melt(df,  id.vars = c("aa", "bb"), variable.name = 'val')

ggplot(df_pl, aes(aa, value)) + geom_point(aes(colour = val))
ggplot(df_pl, aes(bb, value)) + geom_point(aes(colour = val))

But this only produces plots of val1 and val2 as functions of aa and bb, not of a val1 / val2 series for each value of bb. 但这只会生成aa和bb的函数val1和val2的图,而不是每个bb值的val1 / val2系列的图。 I am probably using the melt function incorrectly 我可能使用了不正确的功能

I'm unsure if I understood you correctly and this is what you want to achieve, but maybe try: 我不确定我是否理解正确,这就是您想要实现的目标,但也许可以尝试:

ggplot(df_pl, aes(aa, value)) + geom_point(aes(colour = val)) + facet_wrap(~bb)
ggplot(df_pl, aes(bb, value)) + geom_point(aes(colour = val)) + facet_wrap(~aa)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ggplot2在子图中绘制data.frame中的所有列 - Plot all columns from a data.frame in a subplot with ggplot2 选择要在ggplot2中绘制的数据框列 - Selecting data frame columns to plot in ggplot2 R:ggplot2:无法从data.frame中绘制点 - R: ggplot2: Unable to plot points from a data.frame 使用ggplot2在r中绘制data.frame的列 - plot a column of a data.frame in r using ggplot2 R:融化data.frame以在ggplot2中用于多个y值图 - R: melting a data.frame to use in ggplot2 for multiple y-value plot 你如何 plot 数据框的多列都在 r 的同一个箱线图中(使用 ggplot2)? - How do you plot multiple columns of a data frame all within the same boxplot in r (using ggplot2)? 如何在ggplot2中置换data.frame列表并创建其带注释的堆栈条形图? - How to permute list of data.frame and create its annotated stack bar plot in ggplot2? 如何使用ggplot2绘制带有来自data.frame的颜色填充的geom_bar图 - How to plot with ggplot2 a geom_bar graph with color fillings from a data.frame 使用 ggplot2 绘制 data.frame 中的数据,其中列是扩展类“因子”的 S4 类 - Use ggplot2 to plot data from a data.frame where the columns are S4 classes extending the class "factor" 如何在Shiny / ggplot2应用程序的一个selectInput中使用多个data.frame对象 - How to use multiple data.frame objects in one selectInput on shiny/ggplot2 app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM