I have a huge data set (~150000*41)
looks like
head(Data)
Time A1 A2 A3 ... A40
12:00:00 0 0 0.1 ... 0.65
12:00:30 0.15 0.32 0.2 ... 0.54
12:01:00 0 0.43 0.14 ... 0
.
.
.
I used ggplot()
to plot data as in the question here:
Data <- Data %>%
mutate(data=paste0('Data',data)) %>%
pivot_longer(-c(data,Time))
p <- ggplot(Data, aes(x=factor(Time),y=value,group=name,color=name))+
geom_line()+
facet_wrap(.~data,scales = 'free',ncol=1)+
xlab('Time')
It is known that ggplot()
will process data before plotting so it will delete outliers or missing values. Let us call the processed data by "output data" so ggplot()
will plot the output data not the original data. In my work, the data frame consists of 150000 rows while when plotting data, ggplot()
deletes 33 rows so the output data consists of (150000 - 33) rows.
I am interested, after plotting data, to return a new data frame contains the output data. i.e the data frame consists of the original data except the deleted rows. In my previous question, zx8754 suggested to get the same data as the output data manually by using filter()
. Now, I am more interested to know how to get a data frame directly from ggplot()
. This question asks for the same thing but the answers return a list not a data frame or a matrix by using :
Output_data<-ggplot_build(p)
I am trying since many days and I read many documentation but I can't find a solution, especially that I am piping data by mutate()
EDIT: The answer of jzadra in the same similar question presents a close solution for my question by using
ggplot_build(p)$plot$data
but not returning the same dimensions of the original data. Its gathering all features in the same column as
data Time name value
<chr> <chr> <chr> <dbl>
1 Data1 12:00:00 A1 0
2 Data1 12:00:00 A2 0
3 Data1 12:00:00 A3 0.1
4 Data1 12:00:00 A4 0
5 Data1 12:00:00 A5 0
6 Data1 12:00:00 A6 0
7 Data1 12:00:00 A7 0
8 Data1 12:00:00 A8 0
9 Data1 12:00:00 A9 0
10 Data1 12:00:00 A10 0
# … with ... more rows
while I am looking to get the output data as
Time A1 A2 A3 ... A40
12:00:00 0 0 0.1 ... 0.65
12:00:30 0.15 0.32 0.2 ... 0.54
12:01:00 0 0.43 0.14 ... 0
.
.
.