简体   繁体   中英

How to make facet_grid aware of additional data frames?

I have the following data frame containing the prediction results of different algorithms ie dummy and xxx_simple and the repeated test data. The idea is to output a comparative plot of the accuracy of the predictions for each algorithm in addition to the error measurements. The (true) test data is repeated for each algorithm. I plot this data frame using ggplot2 facet_grid by group.

> df_all
      t    value      label      group
1  2246 869.3300       test      dummy
2  2247 873.7100       test      dummy
3  2248 870.2100       test      dummy
4  2249 866.3900       test      dummy
5  2250 850.1500       test      dummy
6  2246 865.4200      dummy      dummy
7  2247 869.3300      dummy      dummy
8  2248 873.7100      dummy      dummy
9  2249 870.2100      dummy      dummy
10 2250 866.3900      dummy      dummy
11 2246 869.3300       test xxx_simple
12 2247 873.7100       test xxx_simple
13 2248 870.2100       test xxx_simple
14 2249 866.3900       test xxx_simple
15 2250 850.1500       test xxx_simple
16 2246 855.9046 xxx_simple xxx_simple
17 2247 858.6711 xxx_simple xxx_simple
18 2248 864.8865 xxx_simple xxx_simple
19 2249 863.0154 xxx_simple xxx_simple
20 2250 860.8577 xxx_simple xxx_simple

and I plot it as follows:

ggplot(df_all, mapping=aes(x=t, y=value, color=label, shape=label)) +  
   geom_point() + ggtitle('Test vs. Predicted') + geom_line() + 
       facet_grid(. ~ group) 

Now I would like to include error segments that highlight how far the predicted value is from the test data for every point. I compute the df_error for one algorithm in the following way:

df_error <- data.frame(x=t, xend=t, y=df_test$value, yend=df_predicted$value, type=as.factor('error'), group=as.factor(output_label))

The resulting df_error_all is:

> df_error_all
      x xend      y     yend  type      group
1  2246 2246 869.33 865.4200 error      dummy
2  2247 2247 873.71 869.3300 error      dummy
3  2248 2248 870.21 873.7100 error      dummy
4  2249 2249 866.39 870.2100 error      dummy
5  2250 2250 850.15 866.3900 error      dummy
6  2246 2246 869.33 855.9046 error xxx_simple
7  2247 2247 873.71 858.6711 error xxx_simple
8  2248 2248 870.21 864.8865 error xxx_simple
9  2249 2249 866.39 863.0154 error xxx_simple
10 2250 2250 850.15 860.8577 error xxx_simple

Trying to incorporate the segment data into the facet_grid plot:

ggplot(df_all, mapping=aes(x=t, y=value, color=label, shape=label)) +  
   geom_point() + ggtitle('Test vs. Predicted') + geom_line() + facet_grid(. ~ group) +
     geom_segment(data=df_error_all, aes(x=df_error_all$x,y=df_error_all$y,xend=df_error_all$xend,yend=df_error_all$yend), size=0.3)

Yields the following error:

Error in data.frame(x = c(2246L, 2247L, 2248L, 2249L, 2250L, 2246L, 2247L,  : 
   arguments imply differing number of rows: 10, 0

This error shows that facet_grid is not recognizing that the same grouping criteria applies to the df_error_all segment data. Notice also that the df_error_all has different structure and can not be combined with the df_all data frame.

I don't get the error message about different number of rows if I use data you provided in your question but there is another error message:

Error in eval(expr, envir, enclos) : object 'label' not found

This error message is due to facet that you provide color=label and shape=label inside the ggplot() call but data frame df_error_all doesn't have such column. So you need to add inherit.aes=FALSE to your geom_segment() call to ignore those aesthetics.

ggplot(df_all, mapping=aes(x=t, y=value, color=label, shape=label)) +  
  geom_point() + ggtitle('Test vs. Predicted') + geom_line() + facet_grid(. ~ group) +
  geom_segment(data=df_error_all, aes(x=x,y=y,xend=xend,yend=yend), 
                                size=0.3,inherit.aes=FALSE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM