Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make scatterplot with colors based on a column and add a mean line through stats_summary with grouping based on another column?

I have a data.frame (see below) and I would like to build a scatterplot, where colours of dots is based on a factor column (replicate). I simultaneously want to add a line that represents the mean of y, for each x. The problem is that when I define the stat_summary it uses the colours I requested for groupingand hence I get three mean lines (for each color) instead of one. Trying to redefine groups either in ggplot() or stat_summary() function did not work.

enter image description here

if I disable colors I get what I want (a single mean line).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

How do I have colors (plot # 1), yet still have a single mean line (plot # 2)?

structure(list(conc = c(10L, 10L, 10L, 25L, 25L, 25L, 50L, 50L, 
    50L, 75L, 75L, 75L, 100L, 100L, 100L, 200L, 200L, 200L, 300L, 
    300L, 300L, 400L, 400L, 400L, 500L, 500L, 500L, 750L, 750L, 750L, 
    1000L, 1000L, 1000L), citric_acid = c(484009.63, 409245.09, 303193.26, 
    426427.47, 332657.35, 330875.96, 447093.71, 344837.39, 302873.98, 
    435321.69, 359146.09, 341760.28, 378298.37, 342970.87, 323146.92, 
    362396.98, 361246.41, 290638.14, 417357.82, 351927.66, 323611.37, 
    416280.3, 359430.65, 327950.99, 431167.14, 361429.91, 291901.43, 
    340166.41, 353640.91, 341839.08, 393392.69, 311375.19, 342103.54
    ), MICIT = c(20771.28, 18041.97, 12924.35, 49814.13, 38683.32, 
    38384.72, 106812.16, 82143.12, 72342.43, 156535.39, 128672.12, 
    119397.14, 187208.46, 167814.92, 159418.62, 350813.47, 357227.48, 
    295948.31, 505553.77, 523282.46, 489652.3, 803544.84, 704431.61, 
    654753.29, 1030485.41, 895451.64, 717698.52, 1246839.19, 1309712.63, 
    1212111.53, 1930503.38, 1499838.89, 1642091.64), replicate = structure(c(1L, 
    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
    3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L
    ), .Label = c("1", "2", "3"), class = "factor"), MICITNorm = c(0.0429150139016862, 
    0.0440859779160698, 0.0426274317575529, 0.116817357005636, 0.116285781751102, 
    0.116009395182412, 0.238903293897827, 0.238208275500519, 0.238853235263062, 
    0.359585551549246, 0.358272367659634, 0.34935932285636, 0.494869856298879, 
    0.489297881187402, 0.493331701877276, 0.968036405822146, 0.98887482369721, 
    1.01827072661558, 1.21131974956166, 1.48690347328766, 1.51308744189056, 
    1.93029754230503, 1.95985403582026, 1.99649737297637, 2.38999059622215, 
    2.47752500616233, 2.45870162403795, 3.6653801002868, 3.70350995307641, 
    3.54585417793659, 4.90731889298706, 4.81682207885606, 4.79998435561351
    )), class = "data.frame", row.names = c(NA, -33L))

ggplot(xx, aes (conc, MICIT, colour = replicate)) + geom_point () +
            stat_summary(geom = "line", fun = mean)

>Solution :

Use aes(group = 1):

ggplot(xx, aes(conc, MICIT, colour = replicate)) + 
  geom_point() +
  geom_line() +
  stat_summary(aes(group = 1), geom = "line", fun = mean)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading