How to add plot regarding the whole dataset in ggplot2

288 views Asked by At

Using ggplot function, it is possible to group/color the column of interest and plot the data based on that as follows:

ggplot(inputDataFrame, aes(as.numeric(interestingColumn) , group = AnotherColumn)) +
coord_cartesian(xlim = c(0,400)) + geom_line(stat='ecdf')

How can I also add the curve/plot regarding the whole data in "interestingColumn" regardless of the "group" criteria. So that I can compare the whole data and its subdivision groups in one plot.

For instance, running the above code, I will get the figure as follows and I will get the cumulative values for each product separately. How can I add a plot to the following plot which shows the whole products consumption regardless of the product group. enter image description here

Thanks.

1

There are 1 answers

1
shadow On BEST ANSWER

You can add a geom_line without the color aesthetics and a geom_line with the color aesthetics. Also see below how to create a reproducible example.

# create your reproducible example...
set.seed(1)
inputDataFrame <- data.frame(interestingColumn = rnorm(100, 200, 80), 
                             AnotherColumn = factor(rbinom(100, 4, .3)))
# plotting
ggplot(inputDataFrame, aes(as.numeric(interestingColumn))) +
  coord_cartesian(xlim = c(0,400)) + 
  geom_line(stat='ecdf') + 
  geom_line(aes(color=AnotherColumn), stat='ecdf')