Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class Date

Tags:

r

ggplot2

I tried to create a stacked bar plot in R, but unfortunately I have several problems. R gives me the following error:

Error: data must be a data frame, or other object coercible by fortify(), not an S3 object with class Date

I searched for a while, but I can't find a solution. Thank you for helping.

options(stringsAsFactors = FALSE) 
input <- "C:\\Users\\CPOD1_time.csv" CPOD1_time <- read.csv(input, sep=";") 

CPOD1_time = as.Date(CPOD1_time$date, format = "%d.%m.%Y")
library(lubridate) 

library(ggplot2) 

p<- ggplot(CPOD1_time, aes(x=date, y=number, fill=time)) + 

geom_bar(stat="identity") +theme_bw() + geom_text(aes(y=number), vjust=1.6,

size=3.5) 

print(p)
    dput(CPOD1_time)
    structure(c(17742, 17743, 17744, 17745, 17746, 17747, 17748, 
    17749, 17750, 17751, 17752, 17753, 17754, 17755, 17756, 17757, 
    17758, 17759, 17760, 17761, 17762, 17763, 17764, 17765, 17766, 
    17767, 17768, 17769, 17770, 17771, 17772, 17773, 17774, 17775, 
    17776, 17742, 17743, 17744, 17745, 17746, 17747, 17748, 17749, 
    17750, 17751, 17752, 17753, 17754, 17755, 17756, 17757, 17758, 
    17759, 17760, 17761, 17762, 17763, 17764, 17765, 17766, 17767, 
    17768, 17769, 17770, 17771, 17772, 17773, 17774, 17775, 17776, 
    17742, 17743, 17744, 17745, 17746, 17747, 17748, 17749, 17750, 
    17751, 17752, 17753, 17754, 17755, 17756, 17757, 17758, 17759, 
    17760, 17761, 17762, 17763, 17764, 17765, 17766, 17767, 17768, 
    17769, 17770, 17771, 17772, 17773, 17774, 17775, 17776, 17742, 
    17743, 17744, 17745, 17746, 17747, 17748, 17749, 17750, 17751, 
    17752, 17753, 17754, 17755, 17756, 17757, 17758, 17759, 17760, 
    17761, 17762, 17763, 17764, 17765, 17766, 17767, 17768, 17769, 
    17770, 17771, 17772, 17773, 17774, 17775, 17776), class = "Date")

 as.data.frame(CPOD1_time)

          date time      number
1   30.07.2018    1  0.00000000
2   31.07.2018    1  0.00000000
3   01.08.2018    1  0.00000000
4   02.08.2018    1  0.62500000
5   03.08.2018    1  0.00000000
6   04.08.2018    1  0.00000000
7   05.08.2018    1  0.00000000
8   06.08.2018    1  3.26086957
9   07.08.2018    1  0.00000000
10  08.08.2018    1  0.00000000
11  09.08.2018    1  0.00000000
12  10.08.2018    1  0.00000000
13  11.08.2018    1  0.00000000
14  12.08.2018    1  0.00000000
15  13.08.2018    1  0.00000000
16  14.08.2018    1  0.00000000
17  15.08.2018    1  0.69767442
18  16.08.2018    1  0.00000000
19  17.08.2018    1  0.00000000
20  18.08.2018    1  0.71428571
21  19.08.2018    1  0.71428571
22  20.08.2018    1  0.71428571
23  21.08.2018    1  0.71428571
24  22.08.2018    1  0.00000000
25  23.08.2018    1  3.65853658
26  24.08.2018    1  0.50000000
27  25.08.2018    1  3.00751880
28  26.08.2018    1  1.50375940
29  27.08.2018    1  3.00751880
30  28.08.2018    1  3.75939850
31  29.08.2018    1  0.00000000
32  30.08.2018    1  1.53846154
33  31.08.2018    1  7.69230769
34  01.09.2018    1  5.38461539
35  02.09.2018    1  2.30769231
36  30.07.2018    2  0.16064257
37  31.07.2018    2  0.13100437
38  01.08.2018    2  0.26315789
39  02.08.2018    2  0.19823789
40  03.08.2018    2  0.06622517
41  04.08.2018    2  0.00000000
42  05.08.2018    2  0.00000000
43  06.08.2018    2  0.46927374
44  07.08.2018    2  0.26936027
45  08.08.2018    2  0.33821872
46  09.08.2018    2  0.20385051
47  10.08.2018    2  0.06825939
48  11.08.2018    2  0.27366020
49  12.08.2018    2  0.41284404
50  13.08.2018    2  0.13808976
51  14.08.2018    2  0.06936416
52  15.08.2018    2  0.62717770
53  16.08.2018    2  1.19158878
54  17.08.2018    2  0.07033998
55  18.08.2018    2  0.35335689
56  19.08.2018    2  0.14218010
57  20.08.2018    2  0.85714286
58  21.08.2018    2  0.28742515
59  22.08.2018    2  0.79422383
60  23.08.2018    2  2.10399033
61  24.08.2018    2  1.52727273
62  25.08.2018    2  1.90012180
63  26.08.2018    2  2.50000000
64  27.08.2018    2  0.29556650
65  28.08.2018    2  2.82527881
66  29.08.2018    2  2.31631382
67  30.08.2018    2  2.55319149
68  31.08.2018    2  2.41813602
69  01.09.2018    2  2.05063291
70  02.09.2018    2  2.57309942
71  30.07.2018    3  0.00000000
72  31.07.2018    3  0.00000000
73  01.08.2018    3  0.00000000
74  02.08.2018    3  0.00000000
75  03.08.2018    3  0.00000000
76  04.08.2018    3  0.00000000
77  05.08.2018    3  0.00000000
78  06.08.2018    3  0.00000000
79  07.08.2018    3  0.00000000
80  08.08.2018    3  0.00000000
81  09.08.2018    3  0.66666667
82  10.08.2018    3  0.00000000
83  11.08.2018    3  0.00000000
84  12.08.2018    3  4.13793103
85  13.08.2018    3  0.00000000
86  14.08.2018    3  0.00000000
87  15.08.2018    3  0.71428571
88  16.08.2018    3  2.79069767
89  17.08.2018    3  0.00000000
90  18.08.2018    3  0.00000000
91  19.08.2018    3  0.00000000
92  20.08.2018    3  0.00000000
93  21.08.2018    3  0.72289157
94  22.08.2018    3  0.00000000
95  23.08.2018    3  0.00000000
96  24.08.2018    3  0.00000000
97  25.08.2018    3  0.00000000
98  26.08.2018    3  0.00000000
99  27.08.2018    3  0.00000000
100 28.08.2018    3  0.00000000
101 29.08.2018    3  0.00000000
102 30.08.2018    3  9.23076923
103 31.08.2018    3  0.00000000
104 01.09.2018    3  2.30769231
105 02.09.2018    3  0.00000000
106 30.07.2018    4  0.00000000
107 31.07.2018    4  0.17964072
108 01.08.2018    4  0.17647059
109 02.08.2018    4  0.00000000
110 03.08.2018    4  0.00000000
111 04.08.2018    4  0.00000000
112 05.08.2018    4  0.00000000
113 06.08.2018    4  0.00000000
114 07.08.2018    4  0.32432432
115 08.08.2018    4  0.00000000
116 09.08.2018    4  0.00000000
117 10.08.2018    4  0.31168831
118 11.08.2018    4  1.54241645
119 12.08.2018    4  0.75757576
120 13.08.2018    4 11.89066059
121 14.08.2018    4  1.03703704
122 15.08.2018    4  7.73722628
123 16.08.2018    4  8.09638554
124 17.08.2018    4  1.56769596
125 18.08.2018    4  1.27058823
126 19.08.2018    4  0.55813954
127 20.08.2018    4  0.27522936
128 21.08.2018    4  0.68181818
129 22.08.2018    4  1.88340807
130 23.08.2018    4  2.78761062
131 24.08.2018    4  1.71428571
132 25.08.2018    4  0.39045553
133 26.08.2018    4  0.25751073
134 27.08.2018    4  0.25531915
135 28.08.2018    4  0.25263158
136 29.08.2018    4  0.24896266
137 30.08.2018    4  0.36960986
138 31.08.2018    4  1.09979633
139 01.09.2018    4  0.48387097
140 02.09.2018    4  0.00000000
like image 767
Dolphin94 Avatar asked Jan 05 '19 08:01

Dolphin94


People also ask

Why “data` must be a data frame by `fortify () `?

As you can see, the error message “data` must be a data frame, or other object coercible by `fortify ()`, not an S3 object with class uneval” was returned by the RStudio console. The reason for this is that we have specified our data at the wrong position, i.e. within the aes function. So how can we solve this problem? Keep on reading!

Why do I get error “data must be a data frame”?

As you can see, the error message “data` must be a data frame, or other object coercible by `fortify ()`, not an S3 object with class uneval” was returned by the RStudio console. The reason for this is that we have specified our data at the wrong position, i.e. within the aes function.

What is the error for `data` in S3?

Error: `data` must be a data frame, or other object coercible by `fortify ()`, not an S3 object with class uneval Did you accidentally pass `aes ()` to the `data` argument? Show activity on this post. Thanks for contributing an answer to Stack Overflow!


1 Answers

Try this: Workflow is as follows:

 library(tidyverse)
    library(lubridate)
    #`df` is `CPOD_Time` saved as `df<-as.data.frame(CPOD_Time)`
    df<-as.data.frame(CPOD_Time)

Then the plot:

df %>% 
      rename(Time=time,Number=number,Date=date) %>% 
      mutate(Date=str_replace_all(Date,"\\D","-"),Date=as.character(Date),
             Date=dmy(Date)) %>% 
    ggplot(aes(x=Date, y=Number, fill=Time)) + 
      geom_bar(stat="identity") +theme_bw() + 
geom_text(aes(label=Number), vjust=1.6,size=3.5) 
like image 104
NelsonGon Avatar answered Oct 05 '22 23:10

NelsonGon