1

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 
  • 1
    can you post the `head(as_tibble(seq15))` please? – Stephan Sep 26 '19 at 14:36
  • I updated it, thanks. – Isa Bragantini Sep 26 '19 at 14:43
  • 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 ? – cbo Sep 26 '19 at 14:50
  • I want to plot one lm with the data of three subsets. – Isa Bragantini Sep 26 '19 at 14:54
  • 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")`. – aosmith Sep 26 '19 at 14:55
  • I tried it like you said, and it plotted 3 lm, each one regarding one depth. But, I want only one lm for all the three depths (0-3, 3-5, 5-10). Thank you, though. – Isa Bragantini Sep 26 '19 at 15:01

3 Answers3

1

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)

enter image description here

Henry Cyranka
  • 2,970
  • 1
  • 16
  • 21
1

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")

cbo
  • 1,664
  • 1
  • 12
  • 27
-2

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.

  • 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. – Isa Bragantini Sep 26 '19 at 14:53
  • 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](https://stackoverflow.com/help/privileges/flag-posts) – CrunchyTopping Sep 26 '19 at 15:01
  • Hi CrunchyTopping, thanks for your hint. Sorry, I'm not quite familiar with the forum rules yet. – Michael Pfister Sep 26 '19 at 15:15