Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class Date
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)
`data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class ranger
As per the ggplot2 documentation, you need to provide a data.frame()
or object that can be converted (coerced) to a data.frame()
. In this case, if you want to reproduce the plot above in ggplot2, you will need to manually set up the data frame yourself.
Below is an example of how you could set up the data to display the plot in ggplot2.
Data Frame
First we create a data.frame()
with the variables that we want to plot. The easiest way to do this is to just group them all in as separate columns. Note that I have used the as.numeric()
function to first coerce the predicted values to a vector, because they were previously a data.table
row, and if you don't convert them they are maintained as rows.
ggplot_data <- data.frame(unique.death.times = r_fit$unique.death.times,
pred1 = as.numeric(pred[1,]),
pred2 = as.numeric(pred[2,]),
pred3 = as.numeric(pred[3,]))
head(ggplot_data)
## unique.death.times pred1 pred2 pred3
## 1 5 0.9986676 1.0000000 0.9973369
## 2 11 0.9984678 1.0000000 0.9824642
## 3 12 0.9984678 0.9998182 0.9764154
## 4 13 0.9984678 0.9998182 0.9627118
## 5 15 0.9731656 0.9959416 0.9527424
## 6 26 0.9731656 0.9959416 0.9093876
Pivot the data
This format is still not ideal, because in order to plot the data and colour by the correct column (variable), we need to 'pivot' the data. We need to load the tidyr package for this.
library(tidyr)
ggplot_data <- ggplot_data %>%
pivot_longer(cols = !unique.death.times,
names_to = "category", values_to = "predicted.value")
Plotting
Now the data is in a form that makes it really easy to plot in ggplot2.
plot <- ggplot(ggplot_data, aes(x = unique.death.times, y = predicted.value, colour = category)) +
geom_line()
plot
If you really want to match the look of the base plot, you can add theme_classic()
:
plot + theme_classic()
Additional notes
Note that this doesn't include 95% confidence intervals, so they would have to be calculated separately. Be aware though, that a 95% confidence interval is not just 95% of the y value at a given x value. There are calculations that will give you the correct values of the confidence interval, including functions built into R.
For a quick view of a trend line with prediction intervals, you can use the geom_smooth()
function in ggplot2, but in this case it adds a loess curve by default, and the intervals provided by that function.
plot + theme_classic() + geom_smooth()
Error: `data` must be a data frame, or other object coercible by `fortify()`,...Did you accidentally pass `aes()` to the `data` argument?
We need to specify the column name as unquoted
library(dplyr)
library(ggplot2)
Social_Split %>%
rownames_to_column('rn') %>%
ggplot(aes(x = rn, y = V1)) +
geom_col()
-output
Or use barplot
from base R
barplot(t(Social_Split))
data
Social_Split <- structure(list(V1 = c(220L, 213L, 73L, 3L, 44L, 116L, 10L, 104L,
88L, 129L)), class = "data.frame", row.names = c("Facebook",
"Instagram", "Linkedin", "None", "Quora", "Reddit", "Signal",
"Snapchat", "TikTok", "Twitter"))
ERROR:`data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class reactiveExpr/reactive
d1
is not a dataframe but an expression that needs to be evaluated (reactive
does not return a dataframe). You need to active the reactive element before using it in ggplot2
server <- function(session, input, output) {
d1 <- reactive({
data %>% group_by(`Detected State`) %>%
count(`Detected District`) %>%
filter(`Detected State` == input$state)
})
output$stateplot <- renderPlot({
ggplot(d1(), aes(x = `Detected District`, y = n)) +
geom_bar(stat = "identity")
})
}
ggplot2 & spline() in R - Error in : `data` must be a data frame, or other object coercible by `fortify()`
Possible way to do that:
ggplot(data, aes(x = x, y = y)) +
geom_point() +
geom_line(data = data.frame(spline(x, y))) #+
#ggthemes::theme_base()
The problem is: spline
returns list
, you just should have convert it into data.frame
and that's it.
Related Topics
Error in Get(As.Character(Fun), Mode = "Function", Envir = Envir)
Ggplot2: How to Transparently Shade Alternate Days on a Plot
Ggplot2: Plotting Order of Factors Within a Geom
R Crashing While Displaying Ggplot After Update (Process Memory Read Out of Range)
Applying Rolling Mean by Group in R
R Ggplot2 Boxplots - Ggpubr Stat_Compare_Means Not Working Properly
Difference of Prediction Results in Random Forest Model
Rename Columns Using 'Starts_With()' Where New Prefix Is a String
Makecluster Function in R Snow Hangs Indefinitely
Change All Columns from Factor to Numeric in R
Extract Name of Data.Frame in R as Character
How Does R's Ifelse Work with Character Data
How to Ensure That a Partition Has Representative Observations from Each Level of a Factor
Package 'Pbkrtest' Is Not Available (For R Version 3.2.2)
R Remove Multiple Text Strings in Data Frame
Categorical Scatter Plot with Mean Segments Using Ggplot2 in R