ggplot error: Found object is not a stat - r

ggplot() +
geom_point(aes(x = Africa_set$Africa_Predict, y = Africa_set$Africa_Real), color ="red") +
geom_line(aes(x = Africa_set$Africa_Predict, y = predict(simplelm, newdata = Africa_set)),color="blue") +
labs(title = "Africa Population",fill="") +
xlab("Africa_set$Africa_Predict") +
ylab("Africa_set$Africa_Real")
Then show the error message:
Error: Found object is not a stat
How can fix this error?

It looks like you are trying to plot points with a fitted regression line on top. You can do this using:
library(ggplot2)
ggplot(iris, aes(Petal.Length, Petal.Width)) +
geom_point() +
geom_smooth(method = "lm")
Or, if you really do want to use the model you've stored ahead of time in a simplelm object like you have in your example, you could use augment from the broom package:
library(ggplot2)
library(broom)
simplelm <- lm(Petal.Width ~ Petal.Length, data = iris)
ggplot(data = augment(simplelm),
aes(Petal.Length, Petal.Width)) +
geom_point() +
geom_line(aes(Petal.Length, .fitted), color = "blue")

Related

Adding a separate line of regression to ggplot in R

Assuming I have a data frame with the following column headings: Height, Weight, Gender.
I am using ggplot to create a scatter graph.
ggplot(df, aes(x = height, y = weight , col = gender)) +
geom_point() +
theme_classic() +
geom_smooth(method = "lm", se = FALSE)
This plots two lines of regression one for male and female. I would like to add another regression for overall comparing height and weight. How do I do this?
You can add another geom_smooth with col set to a static label. This overrides the first aes(col = gender) argument, puts all observations back in one group and gives it the label you want to use:
library(ggplot2)
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, col = Species)) +
geom_point() +
theme_classic() +
geom_smooth(method = "lm", se = FALSE) +
geom_smooth(aes(col = "Overall"), method = "lm", se = FALSE)
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'
Created on 2020-12-11 by the reprex package (v0.3.0)
Edit: Following up questions below, this also works with scale_colour_lancet:
library(ggplot2)
library(dplyr)
iris %>%
modelr::add_predictions(model = lm(Sepal.Length ~ Sepal.Width + Species,
data = iris)) %>%
ggplot(aes(x = Sepal.Width, y = Sepal.Length, col = Species)) +
geom_point() +
theme_classic() +
geom_line(aes(y = pred)) +
geom_smooth(aes(col = "Overall"), method = "lm", se = FALSE) +
ggsci::scale_color_lancet()
#> `geom_smooth()` using formula 'y ~ x'
Created on 2020-12-12 by the reprex package (v0.3.0)
The geom_smooth used above does lm(y ~ x) for each grouping automatically, in this case giving the equivalent lines for lm(Sepal.Length ~ Sepal.Width*Species). To get lm(Sepal.Length ~ Sepal.Width + Species) a simple way would be to use modelr::add_predictions() to create a pred variable for y values before passing into ggplot.

Plotting in layers in R

I'm trying to plot individual regression lines for all of my experimental subjects (n=40) on the same plot where I show the overall regression line.
I can do the plots separately with ggplot, but I haven't found a way to superpose them on the same graph.
I can illustrate what I did with the iris data frame:
#first plot
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
theme_classic()
# second plot, grouped by species
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, colour =Species)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
theme_classic()
# and I've been trying things like this:
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
theme_classic() +
geom_point(aes(x = Sepal.Width, y = Sepal.Length, colour =Species))) +
stat_smooth(method = lm, se = FALSE) +
theme_classic()
which returns the message "Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?", so I get that this is not the right way to combine them, but what is?
How can I combine both graphs in one?
Thanks in advance!
Repeat the whole data and set Species to be something else ("Together") in example below. Attach the repeated data to the original data and just call the second plot.
d1 = iris
d2 = rbind(d1, transform(d1, Species = "Together"))
ggplot(d2, aes(x = Sepal.Width, y = Sepal.Length, colour =Species)) +
stat_smooth(method = lm, se = FALSE) +
geom_point(data = d1) +
theme_classic()
Similar to #d.b's answer, consider expanding the data frame with rbind, assigning an "All" category for Species and adjust for factor levels (so All shows at top on legend):
new_species_level <- c("All", unique(as.character(iris$Species)))
iris_expanded <- rbind(transform(iris, Species=factor("All", levels=new_species_level)),
transform(iris, Species=factor(Species, levels=new_species_level)))
ggplot(iris_expanded, aes(x=Sepal.Width, y=Sepal.Length, colour=Species)) +
geom_point() +
stat_smooth(method = lm, se = FALSE) +
theme_classic()

Adding smoother to ggplot 2

I have used the following code to create a plot in r using ggplot2:
g <- ggplot(newdata, aes(MVPAper, FMI) +
geom_smooth(method = 'lm'))
I then added the following:
p <- g + geom_point(aes(color = Age)) +
facet_grid(Age ~ .) +
stat_smooth(method = 'lm') +
theme_bw(base_family = 'Times')`
I am wanting to have a smoother for each of the four graphs I have created, using the facet grid to split the graph into four ages 8,9,12,and 15) can anyone assist with my code?
You don't need both geom_smooth() and stat_smooth(). Try this:
library(tidyverse)
df <- diamonds %>% filter(price < 10000, carat < 2.5)
g <- ggplot(df, aes(carat, price, color = cut))
g +
geom_point() +
geom_smooth(method = 'lm') +
facet_grid(cut ~ .) +
theme_bw()

All my plots in Rstudio just show up as gray boxes?

I am using ggplot and geoms to show my data, but the plot sidebar area just shows a gray box with the x and y axis correctly labeled.
Here is the output image:
The code which made the plot:
ggplot(Wc, aes(y = popsafe, x = rnground)) +
geom_jitter(aes(col = me)) +
geom_smooth(method = "lm", se = FALSE, col = "black")
Looks like your dataset is empty. We don't know what your dataset contains, so here an example with the built-in iris dataset. First a proper plot, using the same geoms and mappings you use:
library(ggplot2)
ggplot(iris, aes(y = Sepal.Length, x = Sepal.Width)) +
geom_jitter(aes(col = Species)) +
geom_smooth(method = "lm", se = FALSE, col = "black")
Now I remove all the data from the dataset and replot:
library(dplyr)
iris_empty <- filter(iris, Sepal.Length < 0)
ggplot(iris_empty, aes(y = Sepal.Length, x = Sepal.Width)) +
geom_jitter(aes(col = Species)) +
geom_smooth(method = "lm", se = FALSE, col = "black")
A simple head(Wc) would confirm whether your dataset actually contains any data.

Same code different plot in qplot vs ggplot

I get very different results, the code should be equivalent?
qplot(x = price, data = diamonds) + facet_wrap(~cut)
vs
ggplot(aes(x = diamonds$price), data = diamonds) + geom_histogram() + facet_wrap(~cut)
try this instead
ggplot(aes(x = price), data = diamonds) + geom_histogram() + facet_wrap(~cut)

Resources