Plotting dataframes in the same ggplot with for-loop in a function

时间:2016-07-11 20:43:19

标签: r for-loop ggplot2

I have a bunch of dataframes and I want to plot 2 columns of each dataframe on the same ggplot. I already have a plot from another function, coloured in blue and red and I want the new ones to be added to it. Although the way I'm trying works on the console, I can't get to save the function, call it and have it work. The error I get is :

Discrete value supplied to continuous scale.

So, the dataframes are in my environment and named BEFMORN1 to BEFMORN9. The initial plot is test_plot.

The first part that gives me the test_plot works.

test_plot<-ggplot()+geom_point(data=yy4, aes(x=Time, y=Dist), colour="red")+geom_point(data=zz4, aes(x=Time, y=Dist), colour="blue")
test_plot<-test_plot+scale_x_continuous(name="Time (Seconds from the beginning)")
test_plot<-test_plot+scale_y_continuous(name="Distance (Metres from the beginning)")

The second part will be the new function

plot_all_runs<-function(r,test_plot) {
for (i in 1:(length(r[[1]]))) {
z<-as.data.frame(mget(ls(pattern=paste0("BEFMORN",i))))
test_plot2<-test_plot+geom_point(data=z, aes_string(x=names(z)[12], y=names(z)[17]))
}print(test_plot2)
}

r is a list of 6 lists of different dataframes, so BEFMORN came from r[[1]]. BEFNOON will come from r[[2]] etc. So my plan is to have 6 identical functions with different arguments in paste0.

I'm using aes_string(x=names(z)[12] because the data frames z will have different column names in each iteration.

Does someone understand why I'm getting an error? I have played around with the scales (removing them from the initial plot or adding them again in the next one) but no improvement.

EDIT: All columns to be plotted have been transformed to numeric. Others are factors and integers.

EXAMPLE

BEFMORN1<-data.frame(BEFMORN1.Time=seq(0:10, 0.5), BEFMORN1.Dist=1:20)
BEFMORN2<-data.frame(BEFMORN2.Time=seq(0:13, 0.5), BEFMORN2.Dist=c(1:8,8,8,9,10,13,13,13,13.5,14,14,14 14:20))
yy4<-data.frame(Time=seq(0:10, 0.5). Dist=c(1:8,8,8,9,10,13,14:20))
ZZ4<-data.frame(Time=seq(0:12, 0.5). Dist=c(1:8,8,8,9,9.5,10,10.5,12,12.5,13,14:20))

test_plot<-ggplot()+geom_point(data=yy4, aes(x=Time, y=Dist), colour="red")+geom_point(data=zz4, aes(x=Time, y=Dist), colour="blue")

plot_all_runs<-function(test_plot) {
for (i in 1:9) {
z<-as.data.frame(mget(ls(pattern=paste0("BEFMORN",i))))
test_plot2<-test_plot+geom_point(data=z, aes_string(x=names(z)[12], y=names(z)[17]))
}print(test_plot2)
}

1 个答案:

答案 0 :(得分:1)

生成长格式@biomiha和@joran的示例建议:

library(ggplot2)

BEFMORN1<-data.frame(Time=seq(0,10, 0.5)
                     , Dist=1:21, Group = "BEFMORN1")
BEFMORN2<-data.frame(Time=seq(0,13, 0.5)
                     , Dist=c(1:8,8,8,9,10,13,13,13,13.5,14,14,14,14:21)
                     , Group = "BEFMORN2")
yy4<-data.frame(Time=seq(0,10, 0.5)
                , Dist=c(1:8,8,8,9,10,13,14:21)
                , Group = "yy4")
zz4<-data.frame(Time=seq(0,12, 0.5)
                , Dist=c(1:8,8,8,9,9.5,10,10.5,12,12.5,13,14:21)
                , Group = "zz4")


allData <-
  rbind(BEFMORN1, BEFMORN2, yy4, zz4)


ggplot(allData
       , aes(x = Time
             , y = Dist
             , col = Group)) +
  geom_point()

请注意,如果您的数据已经到位,则可能需要更加谨慎地添加“组”列。但是,一般原则是一样的。如果需要,您可以使用任何scale_color_*函数更改默认颜色,包括scale_color_manual,如果您想自己设置它们。