如何在ggplot中为每个data.frame放置相关线

时间:2019-02-28 17:29:32

标签: r ggplot2 correlation

我无法弄清楚如何为R中的每个data.frame放置两条相关线。代码如下:

combinedplot <- ggplot() +
geom_point(data = data.frame1, aes(x=variable1, y=variable2, color='red')) + 
geom_point(data = data.frame2, aes(x=variable1, y=variable2, color='blue')) +
labs(x="Date", y="PM 2.5 ugm3")
combinedplot

我也尝试过

combinedplot <- ggplot() +
geom_point(data = data.frame1, aes(x=variable1, y=variable2, color='red')) + 
geom_point(data = data.frame2, aes(x=variable1, y=variable2, color='blue')) +
labs(x="Date", y="PM 2.5 ugm3")
combinedplot + geom_smooth(method='lm')

combinedplot <- ggplot() +
geom_point(data = data.frame1, aes(x=variable1, y=variable2, color='red')) + 
geom_smooth(method='lm') +
geom_point(data = data.frame2, aes(x=variable1, y=variable2, color='blue')) + 
geom_smooth(method='lm') +
labs(x="Date", y="PM 2.5 ugm3")
combinedplot 

两个选项都只打印没有线条的图形,有什么建议吗?

2 个答案:

答案 0 :(得分:0)

您需要为@Bean public ClassifierCompositeItemWriter<TRSBatchEntryFormRequest> classifierCompositeItemWriter(ItemWriter<TRSBatchEntryFormRequest> databaseTableItemWriter, ItemWriter<TRSBatchEntryFormRequest> databaseTableUpdateItemWriter) { ClassifierCompositeItemWriter<TRSBatchEntryFormRequest> classifierCompositeItemWriter = new ClassifierCompositeItemWriter<>(); classifierCompositeItemWriter.setClassifier((Classifier<TRSBatchEntryFormRequest, ItemWriter<? super TRSBatchEntryFormRequest>>) trsBatchEntryFormRequest -> { if (trsBatchEntryFormRequest.getForm_status().equals("New")) { return databaseTableItemWriter; } else { return databaseTableUpdateItemWriter; } }); return classifierCompositeItemWriter; } // Writer for DB @Bean public ItemWriter<TRSBatchEntryFormRequest> databaseTableItemWriter(DataSource springBatchDatasource) { JdbcBatchItemWriter<TRSBatchEntryFormRequest> databaseItemWriter = new JdbcBatchItemWriter<TRSBatchEntryFormRequest>(); databaseItemWriter.setDataSource(springBatchDatasource); logger.info("INSERT QUERY....: " + QUERY_INSERT_TRSEntryForms); databaseItemWriter.setSql(QUERY_INSERT_TRSEntryForms); databaseItemWriter.setItemSqlParameterSourceProvider(new TRSDBInputProvider()); return databaseItemWriter; } //Update Writer for DB @Bean public ItemWriter<TRSBatchEntryFormRequest> databaseTableUpdateItemWriter(DataSource springBatchDatasource) { JdbcBatchItemWriter<TRSBatchEntryFormRequest> databaseItemWriter = new JdbcBatchItemWriter<TRSBatchEntryFormRequest>(); databaseItemWriter.setDataSource(springBatchDatasource); logger.info("UPDATE QUERY....: " + QUERY_UPDATE_TRSEntryForms); databaseItemWriter.setSql(QUERY_UPDATE_TRSEntryForms); databaseItemWriter.setItemSqlParameterSourceProvider(new TRSDBInputProvider()); return databaseItemWriter; } 函数提供数据和美观,例如:

aws rds create-db-cluster-endpoint --db-cluster-identifier AURORA-CLUSTER-NAME \
    --db-cluster-endpoint-identifier active-readers --endpoint-type reader

答案 1 :(得分:0)

如果要合并两个图,最好先合并两个data.frame,rbind在定义一个新变量来告诉它们数据来自什么data.frame之后将它们合并在一起。

类似以下内容。

library(ggplot2)

data.frame1$group <- "df1"
data.frame2$group <- "df2"
df_all <- rbind(data.frame1[, c("variable1", "variable2", "group")],
                data.frame2[, c("variable1", "variable2", "group")])


combinedplot <- ggplot(df_all, aes(x = variable1, y = variable2, colour = group)) +
  geom_point() + 
  labs(x = "Date", y = "PM 2.5 ugm3") +
  scale_color_manual(values = c('red', 'blue'))

combinedplot + geom_smooth(method = 'lm')

enter image description here

数据创建代码。

set.seed(1234)    # Make the results reproducible
data.frame1 <- data.frame(variable1 = 1:20, variable2 = 2*(1:20) + rnorm(20) )
data.frame2 <- data.frame(variable1 = 1:25, variable2 = 0.75*(1:25) + rnorm(25) )