How to use both lines and points on the same graph

Good morning, I've started recently to work with RStudio. I've been using ggplot2 to visualize the data that I've been working with (e.g., time trends for incidence and mortality data). I've just created the plot attached but I've not been able to figure out how to plot the "Modeled" data as a line (the "Observed" are being plotted as points, which is what I want). Here is the reprex for the code:

ggplot(data = metric, mapping = aes (Year, Rate, color = Metric))+geom_point()+facet_wrap(~Site)+scale_x_continuous(breaks = c(1991,2004,2017))+xlab("Year of Death")+ylab("Rate per 100,000")

Actually that is not reproducible because you are missing the sample data.
To help us help you, could you please turn this into a proper reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Hi, thanks for helping me out. Below is a sample of the dataframe. Let me know if this format works,

Thanks,
Goncalo


Year
Rate
Site
Metric
1 1991 37.77 C61 Modeled
2 1992 37.32 C61 Modeled
3 1993 36.88 C61 Modeled
4 1994 36.44 C61 Modeled
5 1995 36.01 C61 Modeled
6 1996 35.58 C61 Modeled
7 1997 35.15 C61 Modeled
8 1998 34.74 C61 Modeled
9 1999 34.32 C61 Modeled
10 2000 33.91 C61 Modeled
11 2001 33.51 C61 Modeled
12 2002 33.11 C61 Modeled
13 2003 32.72 C61 Modeled
14 2004 32.33 C61 Modeled
15 2005 31.94 C61 Modeled
16 2006 31.56 C61 Modeled
17 2007 31.19 C61 Modeled
18 2008 30.82 C61 Modeled
19 2009 30.45 C61 Modeled
20 2010 30.09 C61 Modeled
21 2011 29.73 C61 Modeled
22 2012 29.38 C61 Modeled
23 2013 29.03 C61 Modeled
24 2014 28.68 C61 Modeled
25 2015 28.34 C61 Modeled
26 2016 28.00 C61 Modeled
27 2017 27.67 C61 Modeled
28 1991 38.50 C61 Observed
29 1992 35.50 C61 Observed
30 1993 32.60 C61 Observed
31 1994 38.70 C61 Observed
32 1995 27.70 C61 Observed
33 1996 29.40 C61 Observed
34 1997 36.60 C61 Observed
35 1998 31.50 C61 Observed
36 1999 38.70 C61 Observed
37 2000 32.50 C61 Observed
38 2001 34.40 C61 Observed
39 2002 41.40 C61 Observed
40 2003 34.70 C61 Observed
41 2004 32.70 C61 Observed
42 2005 34.00 C61 Observed
43 2006 30.70 C61 Observed
44 2007 34.60 C61 Observed
45 2008 32.00 C61 Observed
46 2009 43.70 C61 Observed
47 2010 27.00 C61 Observed
48 2011 19.70 C61 Observed
49 2012 23.40 C61 Observed
50 2013 33.40 C61 Observed
51 2014 31.00 C61 Observed
52 2015 27.30 C61 Observed
53 2016 27.10 C61 Observed
54 2017 17.70 C61 Observed
Showing 1 to 25 of 54 entries, 4 total columns

It is not very copy/paste friendly and the faceting variable only has one level. Please read the guide I gave you before and try to make a proper reproducible example.

Let me know if this works:

data.frame(stringsAsFactors=FALSE,
        Year = c(1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
                 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
                 2011, 2012, 2013, 2014, 2015, 2016, 2017, 1991, 1992, 1993, 1994,
                 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
                 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
                 2016, 2017, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
                 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
                 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 1991, 1992, 1993,
                 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
                 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
                 2015, 2016, 2017, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
                 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
                 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 1991, 1992,
                 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
                 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
                 2015, 2016, 2017),
        Rate = c(41.02, 41.23, 41.45, 41.66, 41.87, 42.09, 42.31, 42.52, 42.74,
                 42.96, 43.18, 43.4, 43.63, 43.85, 44.08, 44.3, 44.53, 44.76,
                 44.99, 45.22, 45.46, 45.69, 45.92, 46.16, 46.4, 46.64, 46.88,
                 32.12, 31.59, 31.07, 30.56, 30.06, 29.56, 29.08, 28.6, 28.13,
                 27.67, 27.22, 26.77, 26.33, 25.9, 25.47, 25.05, 24.64, 24.24, 23.84,
                 23.45, 23.06, 22.69, 22.31, 21.95, 21.59, 21.23, 20.88, 37.77,
                 37.32, 36.88, 36.44, 36.01, 35.58, 35.15, 34.74, 34.32, 33.91,
                 33.51, 33.11, 32.72, 32.33, 31.94, 31.56, 31.19, 30.82, 30.45,
                 30.09, 29.73, 29.38, 29.03, 28.68, 28.34, 28, 27.67, 34.2, 33.1,
                 46.1, 37, 42.8, 40.1, 49.4, 50.6, 41.3, 50.9, 41.6, 46.3, 49.8,
                 38.3, 40, 36.5, 35.6, 35.1, 54.5, 48.5, 42.6, 50.8, 56.1, 42.8,
                 42.7, 52, 34.9, 30.6, 27.9, 32.7, 30.1, 23.4, 37.4, 27.5, 32.2,
                 18.3, 27.9, 33.2, 21.8, 26.1, 31.3, 25.9, 34.8, 20.5, 18.8, 20.7,
                 24.5, 23.6, 20.5, 26.1, 20.4, 24.2, 15.4, 20.5, 38.5, 35.5,
                 32.6, 38.7, 27.7, 29.4, 36.6, 31.5, 38.7, 32.5, 34.4, 41.4, 34.7,
                 32.7, 34, 30.7, 34.6, 32, 43.7, 27, 19.7, 23.4, 33.4, 31, 27.3,
                 27.1, 17.7),
        Site = c("C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                 "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                 "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                 "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                 "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C50",
                 "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50",
                 "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50",
                 "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C61", "C61",
                 "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61",
                 "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61",
                 "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C33-C34",
                 "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                 "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                 "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                 "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                 "C33-C34", "C33-C34", "C50", "C50", "C50", "C50", "C50", "C50",
                 "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50",
                 "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50",
                 "C50", "C50", "C50", "C61", "C61", "C61", "C61", "C61", "C61",
                 "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61",
                 "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61",
                 "C61", "C61", "C61"),
      Metric = c("Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                 "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                 "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                 "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                 "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                 "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                 "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                 "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                 "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                 "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                 "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                 "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                 "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                 "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                 "Modeled", "Observed", "Observed", "Observed", "Observed",
                 "Observed", "Observed", "Observed", "Observed", "Observed", "Observed",
                 "Observed", "Observed", "Observed", "Observed", "Observed",
                 "Observed", "Observed", "Observed", "Observed", "Observed",
                 "Observed", "Observed", "Observed", "Observed", "Observed", "Observed",
                 "Observed", "Observed", "Observed", "Observed", "Observed",
                 "Observed", "Observed", "Observed", "Observed", "Observed",
                 "Observed", "Observed", "Observed", "Observed", "Observed", "Observed",
                 "Observed", "Observed", "Observed", "Observed", "Observed",
                 "Observed", "Observed", "Observed", "Observed", "Observed",
                 "Observed", "Observed", "Observed", "Observed", "Observed", "Observed",
                 "Observed", "Observed", "Observed", "Observed", "Observed",
                 "Observed", "Observed", "Observed", "Observed", "Observed",
                 "Observed", "Observed", "Observed", "Observed", "Observed", "Observed",
                 "Observed", "Observed", "Observed", "Observed", "Observed",
                 "Observed", "Observed")
)

One way of doing this is just plotting your observed data as points and let the geom_smooth function do the modeling for you.

observed <- dplyr::filter(metric, Metric == "Observed")
ggplot(data = observed, mapping = aes (Year, Rate))+geom_point()+geom_smooth(method="lm", se=F)+facet_wrap(~Site)+scale_x_continuous(breaks = c(1991,2004,2017))+xlab("Year of Death")+ylab("Rate per 100,000")

In this case the modeling is a simple linear regression, but you can play with different formulas as shown in the documentation for [geom_smooth].(https://ggplot2.tidyverse.org/reference/geom_smooth.html)

2 Likes
library(tidyverse)

metric <- data.frame(stringsAsFactors=FALSE,
                     Year = c(1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
                              2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
                              2011, 2012, 2013, 2014, 2015, 2016, 2017, 1991, 1992, 1993, 1994,
                              1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
                              2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
                              2016, 2017, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
                              2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
                              2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 1991, 1992, 1993,
                              1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
                              2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
                              2015, 2016, 2017, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
                              1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
                              2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 1991, 1992,
                              1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
                              2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
                              2015, 2016, 2017),
                     Rate = c(41.02, 41.23, 41.45, 41.66, 41.87, 42.09, 42.31, 42.52, 42.74,
                              42.96, 43.18, 43.4, 43.63, 43.85, 44.08, 44.3, 44.53, 44.76,
                              44.99, 45.22, 45.46, 45.69, 45.92, 46.16, 46.4, 46.64, 46.88,
                              32.12, 31.59, 31.07, 30.56, 30.06, 29.56, 29.08, 28.6, 28.13,
                              27.67, 27.22, 26.77, 26.33, 25.9, 25.47, 25.05, 24.64, 24.24, 23.84,
                              23.45, 23.06, 22.69, 22.31, 21.95, 21.59, 21.23, 20.88, 37.77,
                              37.32, 36.88, 36.44, 36.01, 35.58, 35.15, 34.74, 34.32, 33.91,
                              33.51, 33.11, 32.72, 32.33, 31.94, 31.56, 31.19, 30.82, 30.45,
                              30.09, 29.73, 29.38, 29.03, 28.68, 28.34, 28, 27.67, 34.2, 33.1,
                              46.1, 37, 42.8, 40.1, 49.4, 50.6, 41.3, 50.9, 41.6, 46.3, 49.8,
                              38.3, 40, 36.5, 35.6, 35.1, 54.5, 48.5, 42.6, 50.8, 56.1, 42.8,
                              42.7, 52, 34.9, 30.6, 27.9, 32.7, 30.1, 23.4, 37.4, 27.5, 32.2,
                              18.3, 27.9, 33.2, 21.8, 26.1, 31.3, 25.9, 34.8, 20.5, 18.8, 20.7,
                              24.5, 23.6, 20.5, 26.1, 20.4, 24.2, 15.4, 20.5, 38.5, 35.5,
                              32.6, 38.7, 27.7, 29.4, 36.6, 31.5, 38.7, 32.5, 34.4, 41.4, 34.7,
                              32.7, 34, 30.7, 34.6, 32, 43.7, 27, 19.7, 23.4, 33.4, 31, 27.3,
                              27.1, 17.7),
                     Site = c("C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                              "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                              "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                              "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                              "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C50",
                              "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50",
                              "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50",
                              "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C61", "C61",
                              "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61",
                              "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61",
                              "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C33-C34",
                              "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                              "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                              "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                              "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34", "C33-C34",
                              "C33-C34", "C33-C34", "C50", "C50", "C50", "C50", "C50", "C50",
                              "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50",
                              "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50", "C50",
                              "C50", "C50", "C50", "C61", "C61", "C61", "C61", "C61", "C61",
                              "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61",
                              "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61", "C61",
                              "C61", "C61", "C61"),
                     Metric = c("Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                                "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                                "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                                "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                                "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                                "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                                "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                                "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                                "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                                "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                                "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                                "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                                "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                                "Modeled", "Modeled", "Modeled", "Modeled", "Modeled", "Modeled",
                                "Modeled", "Observed", "Observed", "Observed", "Observed",
                                "Observed", "Observed", "Observed", "Observed", "Observed", "Observed",
                                "Observed", "Observed", "Observed", "Observed", "Observed",
                                "Observed", "Observed", "Observed", "Observed", "Observed",
                                "Observed", "Observed", "Observed", "Observed", "Observed", "Observed",
                                "Observed", "Observed", "Observed", "Observed", "Observed",
                                "Observed", "Observed", "Observed", "Observed", "Observed",
                                "Observed", "Observed", "Observed", "Observed", "Observed", "Observed",
                                "Observed", "Observed", "Observed", "Observed", "Observed",
                                "Observed", "Observed", "Observed", "Observed", "Observed",
                                "Observed", "Observed", "Observed", "Observed", "Observed", "Observed",
                                "Observed", "Observed", "Observed", "Observed", "Observed",
                                "Observed", "Observed", "Observed", "Observed", "Observed",
                                "Observed", "Observed", "Observed", "Observed", "Observed", "Observed",
                                "Observed", "Observed", "Observed", "Observed", "Observed",
                                "Observed", "Observed")
)

ggplot(mapping = aes (Year, Rate, color = Metric)) + 
    geom_line(data = metric %>% filter(Metric == "Modeled")) +
    geom_point(data = metric %>% filter(Metric == "Observed")) + 
    facet_wrap(~Site) + 
    scale_x_continuous(breaks = c(1991,2004,2017)) + 
    xlab("Year of Death") + 
    ylab("Rate per 100,000")

Created on 2019-09-20 by the reprex package (v0.3.0.9000)

3 Likes

This is great to learn, I was trying something like this for that case but couldn't make it work. Excellent.

Thank you so much! This was really helpful.

Have a nice weekend.

Goncalo

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

2 posts were split to a new topic: Troubles with shape=2 for geom_point

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.