## Can I create one lm with geom_smooth using more than one subset for the lm?

ggplot add legend manually

geom_grid

ggplot multiple scatter plots

ggplot(subset layer)

facet_grid

ggplot boxplot grid

ggplot2 grid wrap

first timer here! I have a data frame regarding Fe and Cr concentration in different soil depths. I want to plot all the data, however I want to create a linear model with only the data from 3 different depths in the same plot. The variable 'depth' is a factor. The other two are numeric.

I tried using `subset`

, but then - apparently - I can only create a `lm`

with only one subset. `filter`

didn't work either, nor `[]`

.

When I tried this:

seq15 %>% filter(depth == "0-3", depth == "3-5", depth == "5-10") %>% ggplot(aes(Fe, Cr, color=depth)) + geom_point()

The graph came out blank.

When I tried this:

seq15 %>% ggplot(aes(Fe, Cr, color=depth)) + geom_point() + geom_smooth(data = subset(seq15, depth == "0-3" & depth == "3-5" & depth == "5-10"), method = lm, se = FALSE)

All the data is plotted, but there's no `lm`

.

The head is as follow:

head(as_tibble(seq15)) # A tibble: 6 x 9 points depth Cr Cu Fe Mn Ni Pb Zn <fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 1 CAPA 0-3 50.8 17.7 48291 412. 14.2 18.3 32.8 2 P3 0-3 0-3 82.3 26.3 59696 1152 34.8 26 74.5 3 P3 3-5 3-5 19.5 1.81 21944 201. 4.61 2.66 12.1 4 P3 5-10 5-10 19.2 2.09 28234 213. 4.43 3.05 14.3 5 P3 20-25 20-25 14.1 1.53 8751 185. 4.82 1.86 7.66 6 P4 Borda Lagoa ~ 0-3 60.4 18.7 42646 336 25 17 65

My solution involves subsetting the data, fitting a linear model using this subset, adding the fitted values, and then plot the results.

##Subset the data data_subset <- filter(seq15, depth %in% c("0-3", "3-5","5-10")) lm_model <- lm(Cr~Fe, data = data_subset) ##Add the fitted values data_subset <- data_subset %>% mutate(fitted_values = fitted(lm_model)) ##Plot the data and the fitted values for the linear model seq15 %>% ggplot(aes(Fe, Cr, color=depth)) + geom_point() + geom_line(aes(x = Fe, y = fitted_values), data = data_subset, inherit.aes = FALSE)

**How to utilise multiple subsets of a (factor) variable in ggplot2 ,** By using our site, you acknowledge that you have read and understand I'm hoping to incorporate multiple subsets into one geom_smooth function rather then having multiple geom_smooths in one graph - which will make it difficult aes(valueage, meanvalue), method = "lm", formula = y ~ splines::bs(x # Use span to control the "wiggliness" of the default loess smoother. # The span is the fraction of points used to fit each local regression: # small numbers make a wigglier curve, larger numbers make a smoother curve. ggplot (mpg, aes (displ, hwy)) + geom_point + geom_smooth (span = 0.3)

It might be a filter issue in this case, i can provide an exemple with you data if you share it with `dput()`

:

library(dplyr) #> #> Attachement du package : 'dplyr' #> The following objects are masked from 'package:stats': #> #> filter, lag #> The following objects are masked from 'package:base': #> #> intersect, setdiff, setequal, union library(ggplot2) iris %>% filter(Species != "setos" & Species != "seto") %>% ggplot(data = ., aes(Sepal.Length, y = Sepal.Width, colour = Species)) + geom_point() + geom_smooth(method = "lm", formula = "y ~ x")

**Machine Learning for Hackers: Case Studies and Algorithms to Get ,** We can do that using the predict function: market.index <- predict(pca)[,1] How the DJI runs for so much longer than we want, we need to do some subsetting to dates <- with(dji.prices, rev(Date)) Now we can make some simple graphical aes(x = MarketIndex, y = DJI)) + geom_point() + geom_smooth(method = 'lm', I'm pretty sure there is more than one, but we can't give you an example if you don't provide a reproducible example including some sample data on a copy/paste friendly format. cadebunton June 8, 2019, 10:40pm #6

As far as I have unterstood, you would like to create a lm for each individual group.

There is a similar question and perhaps the answer will help you.

https://stackoverflow.com/a/17195512/6625053

All the best Michael

Hello again, please try this code:

seq15 %>% filter(depth == "0-3" | depth == "3-5" | depth == "5-10") %>% ggplot(aes(x = Fe, y = Cr)) + geom_point(mapping = aes(color = depth)) + geom_smooth(method = lm, se = FALSE)

The problem was that you didn't get any data if you used a comma in the the 'filter' command between the filter conditions. | is a logical means 'OR' otherwise you filter with an 'AND' condition.

**Plotting Data by Facets,** Sometimes you might want to split your data by one (or more) variables, and visualize the subsets of the data together. Facets allow you implement this using ggplot . color = 'black', alpha = 0.5) + geom_smooth(method=lm, color = 'red') create a function that will subset the data subsetData = function(data, sub_col, What if you want to use your own defined fitted model which includes more than 1 input variable instead of the geom_smooth? (i.e. myfit = lm(y ~ x + z + d + x:d, mydata) Is there a patch or equivalent function that allows using “myfit” in geom_smooth instead of formula = …. ?

**Web Application Development with R Using Shiny,** The slider input is set up as follows, with an ID, label, minimum value, maximum The graph is set up as before and then a subset of the data is produced on which the linear smooth can be Take a look at ?quantile for more information. + geom_smooth(data = smoothData, method = "lm", colour = "black") Not bad for a Instead of using lm() to fit a straight line, you can use loess() to fit a smooth curve. Repeat the process of model fitting, grid generation, predictions, and visualization on sim1 using loess() instead of lm(). How does the result compare to geom_smooth()?

**Exploratory Data Analysis 1,** ggplot2: to make statistical plots; we use this rather than base R for increased Next follow one or more geometric objects, geom_* and various Any subset of ggplot commands can be assigned to an object, which can save on + geom_smooth(method=lm, color="blue") + ggtitle("Linear Smoother") + In univariate regression model, you can use scatter plot to visualize model. For example, you can make simple linear regression model with data radial included in package moonBook. The radial data contains demographic data and laboratory data of 115 pateints performing IVUS(intravascular ultrasound) examination of a radial artery after

**Creating legends when aesthetics are constants in ggplot2,** I go through an example of a situation where this might be useful and In general, if you want to map an aesthetic to a variable and get a legend in ggplot2 you do it I use geom_smooth() to make the fitted regression lines, and so add a hp) ) + geom_point() + geom_smooth(method = "lm", se = FALSE, Hi ! I want to add 3 linear regression lines to 3 different groups of points in the same graph. I initially plotted these 3 distincts scatter plot with geom_point(), but I don't know how to do that. Here is an example of my data: Years ppb Gas 1998 2,56 NO 1999 3,40 NO 2000 3,60 NO 2001 3,04 NO 2002 3,80 NO 2003 3,53 NO 2004 2,65 NO 2005 3,01 NO 2006 2,53 NO 2007 2,42 NO 2008 2,33 NO 2009 2,79

##### Comments

- can you post the
`head(as_tibble(seq15))`

please? - I updated it, thanks.
- I don't understand if you want to plot one linear model on your subset or if you want 3 different lm representation. Can you specify this please ?
- I want to plot one lm with the data of three subsets.
- Right now you are asking for all rows were depth is 0-3 AND depth is 3-5. Since that never happens you get an empty dataset returned and so no plot. You can switch to using OR,
`|`

, but this is really the a use case for`%in%`

for value matching:`depth %in% c("0-3", "3-5", "5-10")`

. - Thank you very much, however I am still getting three lm instead of one. ):
- Will update my answer. I think I know what to do
- Yah, thank you. However, I want only one lm instead of three.
- Hey! I tried that one, but it seems they only used one subset to create the linear model, where I want to use more than one subset. Thanks anyway.
- Welcome to SO! You should make this a comment, because answers should be self-contained and not only link to an answer elsewhere. Once you get to 15 rep you can flag questions as a duplicate
- Hi CrunchyTopping, thanks for your hint. Sorry, I'm not quite familiar with the forum rules yet.