0

I want to draw multiple graph using R code with different data files(a.xlsx, b.xlsx, c.xlsx).

I already make a code using R. But it occurs the error.

Let me know how to solve this error.

'''Example R code'''
library(readxl)
library(ggplot2)

a <- read_excel("D:/a.xlsx")
b <- read_excel("D:/b.xlsx")
c <- read_excel("D:/c.xlsx")


plot1 <- ggplot(a, aes(x=aaa, y=bbb)) + geom_line()
plot2 <- ggplot(b, aes(x=aaa, y=bbb)) + geom_line()
plot3 <- ggplot(c, aes(x=aaa, y=bbb)) + geom_line()

pt <- rbind(plot1, plot2, plot3)


ggplot(pt, aes(x=aaa, y=bbb)) + geom_line() + geom_point() + geom_line(aes(color=type)) 



#Error Message


> ggplot(plot1, aes(x=SCvector, y=qNetvector)) + geom_line()
 ERROR : `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class gg/ggplot

Run rlang::last_error() to see where the error occurred.

Plutos
  • 51
  • 1
  • 8
  • Did you mean to `rbind` the data, rather than the plots? `pt <- rbind(a, b, c)` You seem be trying to use `pt` as the data source for the last plot. It doesn't make sense to `rbind()` plots. – MrFlick Aug 07 '20 at 01:08
  • @MrFlick Thank you for answering my question. I want to draw multiple plots(a,b,c) in single canvas combining a,b,c data. rbind is not necessary. – Plutos Aug 07 '20 at 04:16

1 Answers1

0

Welcome to StackOverflow. For your next question, you might want to provide a reproducible example.

For today, I'll assume your question is: "I have 3 data.frame with the same schema, and I want to plot their content in a plot that will consist of 3 sub-plots".

One solution would be to use facet:

# Firt, let's make some fake data that anyone can reproduce.
set.seed(123)
library(magrittr)
library(tidyverse)
a <- data.frame(aaa=seq(10), bbb=rnorm(10), type=sample(letters[1:3], 10, replace=TRUE))
b <- data.frame(aaa=seq(10), bbb=rnorm(10), type=sample(letters[1:3], 10, replace=TRUE))
c <- data.frame(aaa=seq(10), bbb=rnorm(10), type=sample(letters[1:3], 10, replace=TRUE))

# This should be quite similar to your data.
# Now we can work on it. 
# First, we bind all your data in one data.frame.
# But to know which observation comes from which data.frame, we add a 'source' column.
a %<>% mutate(source="a")
b %<>% mutate(source="b")
c %<>% mutate(source="c")
z <- do.call(rbind, list(a, b, c))

# And finally we can build a plot.
z %>% ggplot(aes(x=aaa, y=bbb, group=type, color=type)) + geom_point() + geom_line() + facet_wrap("source")
Vongo
  • 1,325
  • 1
  • 17
  • 27
  • Thank you for answering my question. But what does '%<>%' mean? – Plutos Aug 09 '20 at 23:51
  • `a %<>% mutate(source="a")` is the same as `a <- a %>% mutate(source="a")`, which is the same as `a <- mutate(a, source="a")`. Use `help(magrittr)` to know more about piping in R. – Vongo Aug 10 '20 at 06:51