Plot mean of data within same ggplot

2019-07-13 23:52发布

问题:

I have a ggplot showing a plot of data using the geom_path expression to see how two variables move over time. The start year and end year are plotted with the geom_text function for reference points. Can you please demonstrate how I can plot within the same ggplot graph the mean of the data behind each geom_path curve? This mean would need to be shown as two points on the plot to represent the mean of the X coordinates and the Y coordinates of the data set for each of the two curves I’m showing.

Do I need two separate data sets in order to accomplish this?

Here’s my syntax:

orbit.plot <- ggplot(orbit.data, aes(x=OpM, y=INVT, colour=Subj,     label=Year)) +
geom_point(size=7, shape=20) + geom_path(size=1.5) +
ggtitle("Title Orbits") +
geom_text(data=subset(orbit.data,Year==2006 | Year==2014), aes(label=Year,     vjust=1, hjust=1)) +
theme(panel.background = element_rect(fill = 'white', colour = 'red'),     panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
geom_vline(xintercept=0, size=1) +
geom_hline(yintercept=7, size=1) +
scale_y_continuous(limits = c(7, 15), breaks=seq(7,15,1/2))

Here’s where I attempted to use another geom_point expression to plot the mean unfortunately, nothing shows up in my graph.

# geom_point(data=orbit.data, aes(x= mean(INVT), y=mean(OpM)))

Here’s my data set:

    Year    Subj    OpM INVT
    2006    HMC 0.088   14.5
    2006    TMC 0.09    10.3
    2007    HMC 0.095   13.8
    2007    TMC 0.078   10
    2008    HMC 0.085   13.3
    2008    TMC 0.08    9.5
    2009    HMC -0.023  12.3
    2009    TMC 0.019   8
    2010    HMC 0.009   13.5
    2010    TMC 0.043   8
    2011    HMC 0.025   14.3
    2011    TMC 0.065   10
    2012    HMC 0.0199  13.2
    2012    TMC 0.029   8.5
    2013    HMC 0.06    13.8
    2013    TMC 0.055   9.5
    2014    HMC 0.088   13.8
    2014    TMC 0.065   9.75

Thanks very much for your assistance.

Edit: Here is dput of the dataset as suggesed:

structure(list(Year = c(2006L, 2006L, 2007L, 2007L, 2008L, 2008L, 
2009L, 2009L, 2010L, 2010L, 2011L, 2011L, 2012L, 2012L, 2013L, 
2013L, 2014L, 2014L), Subj = structure(c(2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("TMC", 
"HMC"), class = "factor"), OPM = c(0.088, 0.09, 0.095, 0.078, 
0.085, 0.08, -0.023, 0.019, 0.009, 0.043, 0.025, 0.065, 0.0199, 
0.029, 0.06, 0.055, 0.088, 0.065), Invt = c(14.5, 10.3, 13.8, 
10, 13.3, 9.5, 12.3, 8, 13.5, 8, 14.3, 10, 13.2, 8.5, 13.8, 9.5, 
13.8, 9.75)), .Names = c("Year", "Subj", "OpM", "INVT"
), class = "data.frame", row.names = c(NA, -18L))
标签: r plot ggplot2