There are two approaches you can use, depending on your data set. In the absence of your data, I'll show you both using the iris
dataset. It's not the best example dataset for this, but... it works to demonstrate these methods.
First, the basic plot:
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color=Species)) + theme_bw() +
geom_line()
p
So there's the general way to generate the plot. From your description of your dataset, it seems you have an x
, y
, and treatment
column that should correspond to the code above as iris$Sepal.Length
, iris$Sepal.Width
and iris$Species
, respectively.
Method 1: Overlay a Plot of Aggregate Data
The first method has more prep work upfront, but sometimes it's just straightforward to use. The idea is that you create a separate dataframe holding your summary data and then plot that as a separate call to geom_line()
. The advantage of this method is that you have access directly to the summary data, and it is therefore relatively easy to access an individual data point among that dataset. So, if you plan to do any further plotting or analysis of the summary data... this might be the way to go.
Create your summary dataset. There are lots of methods to do this - here I'm using dplyr
methods:
library(dplyr)
iris_summary <- iris %>%
group_by(Sepal.Length) %>%
summarize(mean_width=mean(Sepal.Width))
Then create the plot by adding another call to geom_line()
, but specifying the data=
field to correspond to the summary dataset, iris_summary
. In addition, you will need to reference the y=
aesthetic, since it is now called iris_summary$mean_width
instead of iris_summary$Sepal.Width
:
p1 <- p +
geom_line(
data=iris_summary,
aes(y=mean_width),
color='black', size=1
)
p1
Method 2: Use stat_summary()
The second method is very straightforward, but does not make it simple to access and do anything with the aggregated data. If all you want to do is plot that summary data as a line, then this is definitely the simplest.
p2 <- p +
stat_summary(
geom='line', fun=mean, aes(group=1),
size=1, color='black'
)
p2
I'm not showing the plot, since the result is identical to that of p1
using Method 1 above.
dput(your.data)
in the console and pasting the output in the body of your question (formatted as code, please). Finally, you mentioned you were able to create a plot - can you share that too? If not, at least share the code used to create it and we can help. – chemdork123