This question already has an answer here:
ggplot2 geom_smooth line not showing up on my graph
(1 answer)
Closed 6 years ago.
Learning ggplot2 and don't understand why the second set of code produces an error. All I had to do was add the aesthetics to the stat_smooth command in the third set of code and it ran fine, but I don't understand why.
ggplot(df, aes(x=wave.height, y=ship.deploy)) + geom_point() +
stat_smooth(method="glm", method.args=list(family="binomial"), se=FALSE)
ggplot(data = df) +
geom_point(mapping = aes(x = wave.height, y = ship.deploy)) +
stat_smooth(method = "glm", method.args = list(family = "binomial"), se = FALSE)
Error: stat_smooth requires the following missing aesthetics: x, y
ggplot(data = df) +
geom_point(mapping = aes(x = wave.height, y = ship.deploy)) +
stat_smooth(mapping = aes(x = wave.height, y = ship.deploy),method = "glm", method.args = list(family = "binomial"), se = FALSE)
Only aesthetic mappings specified at the top level, ggplot(aes()), are inherited by subsequent layers. Aesthetics specified in a single layer, geom_point(aes()) apply only to that layer.
To avoid re-specifying the same mappings, put them at the top, as in your first code.
Related
How do you use geom_smooth() using a formula with the following form:
log(Unit.Sales_1) ~ log(Price) + A
On top of a ggplot that is using a completely different dataset?
I'm currently able to use a transformed axis, a different dataset, but not both at the same time. I get the following error message:
Computation failed in stat_smooth(): object 'Unit.Sales_1' not found
And the rest of my ggplot looks like this:
ggplot() +
geom_point(data = hist_data1, aes(x = Price, y = Unit.Sales, color = "Historicals")) +
geom_line(data = est_data1, aes(x = P1, y = Q1, color = "Estimated")) +
geom_smooth(data = wide_oj_data,
formula = Unit.Sales_1 ~ log(Price_1) + log(Price_3) + Promotion_1 + Holiday,
method = "glm",
method.args = list(family = gaussian(link = 'log')),
aes(x = Price_1, y = Unit.Sales_1)
)
Thank you :)
I have 50 data points of temperature and humidity that I would like to plot on geom_point and add a linear model to my ggplot. however, I am unable to do so. I have tried abline, geom_line, geom_smooth and lm.
temp_humidity_data <- dplyr::select(data, temperature:humidity)
lm(formula = humidity ~ temperature, data = temp_humidity_data)
ggplot(temp_humidity_data) +
geom_point(aes (x = temperature , y = humidity))
geom_smooth()
How can I go about adding an lm to my `ggplot? any help is appreciated. thank you. And how could i differentiate the temperature and humidity points by colour as well on the plot?
this is what I have currently ^
As mentioned in the comment section, you missed a + sign after geom_point. Besides that, you are also missing a few arguments in geom_smooth:
library(ggplot2)
ggplot(iris) +
geom_point(aes(x = Petal.Length , y = Petal.Width)) +
geom_smooth(aes(x = Petal.Length, y = Petal.Width),
method = "lm", formula = y ~ x)
You need to supply "aesthetics" for x and y, otherwise you would get the following error:
Error: stat_smooth requires the following missing aesthetics: x, y
method = "lm" tells geom_smooth that you want to use the linear model method while formula specifies the model formula to plot. If we don't specify the method, geom_smooth defaults to "loess" (as stated by #Lyngbakr) and gives the warning message:
geom_smooth() using method = 'loess' and formula 'y ~ x'
Since we have to supply the same aesthetics in both geom_point and geom_smooth, a more convenient way would be to write:
ggplot(iris, aes(x = Petal.Length , y = Petal.Width)) +
geom_point() +
geom_smooth(method = "lm", formula = y ~ x)
Output:
To answer OP's second question of "how could i differentiate the temperature and humidity points by colour as well on the plot?", we can add the color and size aesthetics to geom_point like the following:
ggplot(iris, aes(x = Petal.Length , y = Petal.Width)) +
geom_point(aes(color = Petal.Length, size = Petal.Width)) +
geom_smooth(method = "lm", formula = y ~ x)
Output:
To change the range of sizes and colors, we use scale_fill_continuous (or scale_color_continuous for color) and scale_size_continuous:
ggplot(iris, aes(x = Petal.Length , y = Petal.Width)) +
geom_point(aes(fill = Petal.Length, size = Petal.Width), pch = 21) +
geom_smooth(method = "lm", formula = y ~ x) +
scale_fill_continuous(low = "red", high = "blue") +
scale_size_continuous(range = c(1, 10))
Notice that as you increase the size range, some points start to overlap with each other. To make it less confusing, I've used fill instead of color and added pch = 21 ("plot character" of a circle) to wrap around each point. This gives a nice border that separates each point.
Output:
I'm having trouble substituting color() for facet_grid() when I want to 'split' my data by a variable. Instead of generating individual plots with regression lines, I'm looking to generate a single plot with all regression lines.
Here's my code:
ggplot(data, aes(x = Rooms, y = Price)) +
geom_point(size = 1, alpha = 1/100) +
geom_smooth(method = "lm", color = Type) # Single plot with all regression lines
ggplot(data, aes(x = Rooms, y = Price)) +
geom_point(size = 1, alpha = 1/100) +
geom_smooth(method = "lm") + facet_grid(. ~ Type) # Individual plots with regression lines
(The first plot doesn't work) Here's the output:
"Error in grDevices::col2rgb(colour, TRUE) : invalid color name 'Type'
In addition: Warning messages:
1: Removed 12750 rows containing non-finite values (stat_smooth).
2: Removed 12750 rows containing missing values (geom_point)."
Here's a link to the data:
Dataset
You need to supply an aesthetic mapping to geom_smooth, not just a parameter, which means you need to put colour inside aes(). This is what you need to do any time you want to have an graphical element correspond to something in the data rather than a fixed parameter.
Here's an example with the built-in iris dataset. In fact, if you move colour to the ggplot call so it is inherited by geom_point as well, then you can colour the points as well as the lines.
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point() +
geom_smooth(aes(colour = Species), method = "lm")
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
geom_point() +
geom_smooth(method = "lm")
Created on 2018-07-20 by the reprex package (v0.2.0).
I am pretty new in modelling. I have three groups of data (by period), which I want to display by lines over scatter plot.
I figured out how to put my method and formula in geom_smooth, and I am able to display a single line.
However, when I want to add lines per group, which could be accomplished by ggplot(.., aes(..,group = period)), I've got back a Warning:
Warning message:
Computation failed in `stat_smooth()`:
number of iterations exceeded maximum of 50
and the line is not displayed.
My working code:
ggplot(tab, aes(x=distance, y=grad)) + #
geom_point() + theme_bw() +
geom_smooth(method = "nls",
formula = y ~ a*x^(-b),
method.args = list(start=c(a=20, b=0.01)), #
se = F)
results:
Code providing error (with added group = period in aes), and not displaying lines per group:
ggplot(tab, aes(x=distance, y=grad, group = period)) + #
geom_point() + theme_bw() +
geom_smooth(method = "nls",
formula = y ~ a*x^(-b),
method.args = list(start=c(a=20, b=0.01)), #
se = F)
Do you have some ideas how can I increase the number of iteration in ggplot2 by geom_smooth function?
I found some information to increase number of iteration by control=nls.control(maxiter=200) https://stat.ethz.ch/pipermail/r-help/2006-June/107606.html relative to R base modelling, but I can't find solution or directions for ggplot2.
Based on #Axeman comment, I added the control=nls.control(maxiter=200) to the
method.args = list(start=c(a=20, b=0.01),
control=nls.control(maxiter=200))
The whole script is thus:
ggplot(tab, aes(x=distance, y=grad, group = period, col = period)) + #
geom_point(col = "grey") + theme_bw() +
geom_smooth(method = "nls",
formula = y ~ a*x^(-b),
method.args = list(start=c(a=20, b=0.01),
control=nls.control(maxiter=200)), #
se = F)
And the result is:
ggplot(data = wheatX,
aes(x = No.of.species,
y = Weight.of.weed,
color = Treatment)) +
geom_point(shape = 1) +
scale_colour_hue(l = 50) +
geom_smooth(method = glm,
se = FALSE)
This draws a straight line.
But the species number will decrease at somepoint. I want to make the line curve. How can I do it. Thanks
This is going to depend on what you mean by "smooth"
One thing you can do is apply a loess curve:
ggplot() + ... + stat_smooth(method = "loess", formula = biomass ~ numSpecies, size = 1)
Or you can manually build a polynomial model using the regular lm method:
ggplot() + ... + stat_smooth(method = "lm", formula = biomass ~ numSpecies + I(numSpecies^2), size = 1)
You'll need to figure out the exact model you want to use for the second case, hence what I originally meant by the definition of the term "smooth"