Plot ECDF data with ggplot2

2k views Asked by At

I've a normalize data to plot ecdf but I couldn't change line shape, color and legend info. My Data is:

EDCF.df <- structure(list(Length = c(11431L, 138250L, 109935L, 7615L, 5221L, 
                      8741L, 9460L, 3102L, 2662L, 12286L, 5097L, 4995L, 4263L, 6916L, 
                      9906L, 5482L, 3884L, 8754L, 5571L, 4589L, 5159L, 4536L, 2247L, 
                      3031L, 20085L, 93534L, 31105L, 54026L, 45523L, 31975L, 5150L, 
                      1204L, 507L, 414L, 331L, 387L, 331L, 234L, 259L, 193L, 182L, 
                      278L, 481L, 152L, 185L, 201L, 207L, 182L, 346L, 316L, 315L, 257L, 
                      107L, 132L, 243L, 162L, 140L, 70L, 146L, 115L, 199L, 100L, 898L, 
                      394L, 380L, 675L, 1026L, 806L, 709L, 438L, 454L, 1592L, 2505L, 
                      4896L, 5940L, 59756L, 10408L, 5076L, 3911L, 4842L, 7769L, 5565L, 
                      4205L, 3860L, 6384L, 6323L, 5632L, 5390L, 6257L, 6848L, 6385L, 
                      7718L, 17267L, 3332L, 3748L, 3372L, 4176L, 2528L, 1540L, 1300L, 
                      2867L, 1140L, 988L, 1075L, 1127L, 815L, 955L, 759L, 1073L, 2450L, 
                      2116L, 1054L, 1078L, 3710L, 1220L, 1328L, 1691L, 1301L, 856L, 
                      466L, 1039L, 1784L, 2962L, 1492L, 5394L, 5882L, 4508L, 2675L, 
                      6629L, 4769L, 4352L, 2088L, 3458L, 6799L, 5443L, 5812L, 5798L, 
                      6031L, 5393L, 7985L, 8958L, 7217L, 9119L, 6202L, 7299L, 5021L, 
                      6130L, 9486L, 6046L, 8788L, 9210L, 8685L, 9872L, 13702L, 15259L, 
                      3202L, 3355L, 2675L, 2505L, 2044L, 2355L, 1730L, 2360L, 1892L, 
                      1602L, 932L, 2096L, 2791L, 2593L, 1220L, 1124L, 1362L, 1984L, 
                      687L, 2484L, 991L, 995L, 3102L, 8213L, 3270L, 3724L, 1508L, 776L, 
                      1379L, 2835L, 1306L, 4901L, 5889L, 5314L, 3328L, 2688L, 1447L, 
                      2003L, 1300L, 1671L, 1415L, 4545L, 7415L, 6319L, 7622L, 6300L, 
                      8464L, 12466L, 6538L, 7737L, 8244L, 9089L, 6276L, 9804L, 8551L, 
                      5763L, 6713L, 10850L, 14673L, 28028L, 19956L, 58551L, 20358L, 
                      10077L, 5395L, 4903L, 3774L, 2374L, 2473L, 2093L, 1360L, 1537L, 
                      1700L, 1392L, 1098L, 894L, 1316L, 2078L, 1555L, 2346L, 1751L, 
                      1798L, 3186L, 2583L, 2161L, 2498L, 1246L, 1867L, 1078L, 970L, 
                      1020L, 1119L, 1440L), Normalize = c(1.05322429399605, 12.7380158030753, 
                                                          10.1291411740404, 0.701627416567223, 0.481050130255741, 0.805374293921746, 
                                                          0.871621189852387, 0.285810669230666, 0.245270148772416, 1.13200189625015, 
                                                          0.469625074490234, 0.46022704474764, 0.39278236071255, 0.637223271566502, 
                                                          0.912714535589613, 0.505098029891203, 0.357862230590557, 0.806572082026194, 
                                                          0.51329827152935, 0.422819200870254, 0.475337602372988, 0.417935910905965, 
                                                          0.207033066976566, 0.279268903429449, 1.85058262137264, 8.61799327395911, 
                                                          2.86593838375883, 4.97782308699419, 4.1943775291385, 2.94609804921037, 
                                                          0.474508364454523, 0.110933605981213, 0.0467137360734842, 0.0381449442493539, 
                                                          0.0304975278901839, 0.0356572304939613, 0.0304975278901839, 0.0215601858800696, 
                                                          0.0238636245424702, 0.0177825464737326, 0.0167690334622764, 0.0256142379258947, 
                                                          0.0443181598645875, 0.0140049070673956, 0.0170454461017644, 0.0185196468457008, 
                                                          0.019072472124677, 0.0167690334622764, 0.0318795910876243, 0.0291154646927436, 
                                                          0.0290233271462476, 0.0236793494494782, 0.00985871747507457, 
                                                          0.0121621561374752, 0.0223894237985338, 0.0149262825323559, 0.0128992565094434, 
                                                          0.00644962825472168, 0.0134520817884195, 0.0105958178470428, 
                                                          0.0183353717527088, 0.0092137546496024, 0.223015329399842, 0.0978485966409107, 
                                                          0.0943717429531626, 0.167634017087855, 0.254803705973539, 0.200167433737498, 
                                                          0.176077804615243, 0.108775851088119, 0.112749398159831, 0.395367933635355, 
                                                          0.622108463414927, 1.21590540394391, 1.47517935037312, 14.8402049260768, 
                                                          2.58479237014873, 1.26060780850067, 0.971283912341629, 1.20249468257688, 
                                                          1.92940545000821, 1.38204934087987, 1.04429783978434, 0.958618231050547, 
                                                          1.58544528161313, 1.57029613340223, 1.39868856924266, 1.33858866978302, 
                                                          1.55390525173142, 1.70067814669278, 1.58569362830511, 1.91673976871713, 
                                                          4.28820233045331, 0.827491177684047, 0.930803401548562, 0.837425045363327, 
                                                          1.03709578571686, 0.627820437330513, 0.382453905652291, 0.322850699576609, 
                                                          0.712009965912414, 0.283115228859488, 0.245366531678223, 0.266972693880657, 
                                                          0.279886721863722, 0.202402553965336, 0.237171090842817, 0.188495139214343, 
                                                          0.266476000496693, 0.608449395355917, 0.525501600233927, 0.261757413349035, 
                                                          0.267717733956603, 0.921366227253246, 0.302982964218048, 0.329804406952105, 
                                                          0.419954256141574, 0.323099046268591, 0.212584768336598, 0.115729558463615, 
                                                          0.258032212969305, 0.4430504984959, 0.735602901650704, 0.370533264437154, 
                                                          1.18129861322068, 1.28817175435002, 0.987262541416168, 0.585831255165983, 
                                                          1.45176650111974, 1.0444221517333, 0.953098176628918, 0.457276882537036, 
                                                          0.757310086117371, 1.4889968986443, 1.19202972780129, 1.27284159066344, 
                                                          1.26977555792612, 1.32080310276861, 1.1810796108823, 1.74873367196276, 
                                                          1.96182294720631, 1.58053987608706, 1.99708232368546, 1.35825250263156, 
                                                          1.59849806783421, 1.09961074100501, 1.34248433426821, 2.07745618187085, 
                                                          1.32408813784431, 1.92459254968174, 2.01701153647802, 1.90203530882862, 
                                                          2.16199108448545, 3.00077004048011, 3.34175668133747, 0.701245487492141, 
                                                          0.734752845264251, 0.585831255165983, 0.548600857641416, 0.447640779648325, 
                                                          0.515750506884444, 0.378874045397065, 0.516845518576344, 0.414352424214594, 
                                                          0.35084174608445, 0.20411017936998, 0.459028901244075, 0.61123552641804, 
                                                          0.567873063418838, 0.267182852823364, 0.246158628338903, 0.298281184873297, 
                                                          0.434500639345536, 0.150454606466927, 0.54400180853544, 0.217031317334388, 
                                                          0.217907326687908, 0.67934525365416, 1.79866620511335, 0.716137646501968, 
                                                          0.8155647081264, 0.330255526276748, 0.16994581458273, 0.302004224625753, 
                                                          0.620871629306752, 0.286017053924027, 0.813988748995446, 0.978081971604608, 
                                                          0.882582373426199, 0.55273506563086, 0.446439860701848, 0.240326814894187, 
                                                          0.332670774176266, 0.215912135012055, 0.277530136619341, 0.235012054647736, 
                                                          0.754862041253683, 1.23152960085722, 1.04949906241629, 1.26590945620145, 
                                                          1.04634342351996, 1.40575408518618, 2.0704312885079, 1.08587195285293, 
                                                          1.28500937583713, 1.36921510849183, 1.50955799624967, 1.04235735333512, 
                                                          1.6283096705063, 1.42020358960621, 0.957155103134208, 1.11493704795071, 
                                                          1.80203589606215, 2.43698365925529, 4.6550656308599, 3.31441735869274, 
                                                          9.72451647468524, 3.38118403428878, 1.67365121885883, 0.896035360300027, 
                                                          0.814320921510849, 0.626809536565765, 0.394288775783552, 0.410731315296009, 
                                                          0.347618537369408, 0.225877310474149, 0.255274578087329, 0.282346638092687, 
                                                          0.2311920707206, 0.182362710956335, 0.148481114385213, 0.21856951513528, 
                                                          0.345127243503884, 0.258264130725958, 0.389638360567908, 0.290817037235467, 
                                                          0.298623091347442, 0.529150817037235, 0.429000803643182, 0.358912402893115, 
                                                          0.414883471738548, 0.206943477096169, 0.310083043128851, 0.179040985802304, 
                                                          0.161103669970533, 0.169407982855612, 0.185850522368069, 0.239164211090276
                      ), Party = c("AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", 
                                   "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", 
                                   "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", 
                                   "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", 
                                   "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", 
                                   "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", 
                                   "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", "AKP", 
                                   "AKP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", 
                                   "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", 
                                   "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", 
                                   "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", 
                                   "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", 
                                   "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", 
                                   "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", "CHP", 
                                   "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", 
                                   "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", 
                                   "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", 
                                   "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", 
                                   "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", 
                                   "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", 
                                   "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "MHP", "HDP", 
                                   "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", 
                                   "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", 
                                   "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", 
                                   "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", 
                                   "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", 
                                   "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", 
                                   "HDP", "HDP", "HDP", "HDP", "HDP", "HDP", "HDP")), .Names = c("Length", 
                                                                                                 "Normalize", "Party"), row.names = c(NA, 248L), class = c("tbl_df", 
                                                                                                                                                           "tbl", "data.frame"))

and my script to plot df:

p1 <- ggplot(EDCF.df,aes(x=Normalize,color=Party))+geom_line(aes(y=..y..),stat="ecdf")

result: ecdf geom_line

After I checked the site I found an alternative solution for this problem. Second script is:

p2 <- ggplot(EDCF.df,aes(x=Normalize))+stat_ecdf(aes(color=Party,linetype=Party))

and result: geom_step

How can I change shape, color of the variable and legend?

Thank you in advance!

UPDATE: I'm sorry for the misunderstanding about my question so the final plot should be something like this:

Final Plot

2

There are 2 answers

1
Sandipan Dey On BEST ANSWER

Just modify your code slightly:

p1 <- ggplot(EDCF.df,aes(x=Normalize))+
geom_line(aes(y=..y..,lty=Party,col=Party), lwd=1.2, stat="ecdf")
p1

If you want to change colors / linetype, you can choose from different palettes / linetypes:

p1 + scale_color_manual(values=rainbow(length(unique(EDCF.df$Party)))) + 
scale_linetype_manual(values=c("solid", "dashed", "dotted", "dotdash"))

enter image description here

[EDITED]

ggplot(EDCF.df,aes(x=Normalize))+
  geom_point(aes(y=..y..,shape=Party, col=Party), size=2, stat="ecdf") + 
  xlab('V/<V>') +
  ylab('CDF(V/<V>)') +
  theme_bw()

enter image description here

3
Fr. On

Here's some default code to get you started. As @eipi10 says, the rest is in the documentation for the different scale_ parameters of ggplot2. Check out the documentation, it is very well illustrated.

ggplot(EDCF.df, aes(x = Normalize)) +
  stat_ecdf(aes(color = Party, lty = Party)) +
  scale_linetype_manual("LINETYPES", values = c(
    "AKP" = "solid",
    "CHP" = "solid",
    "HDP" = "dotted",
    "MHP" = "dotted")
  ) +
  scale_color_brewer("PARTY", palette = "Set2") +
  labs(x = "Hello", y = "World") +
  theme_bw()

Relevant in this example:

P.S. 1. What do you mean by 'shape'? Do you man 'linetype'? Shapes (a.k.a. glyphs) are for points.

P.S. 2. Typo: your ECDF data frame is called "EDCF".

Add. Here's what OP asked for in the comments:

ggplot(EDCF.df, aes(x = Normalize)) +
  stat_ecdf(aes(color = Party, lty = Party)) +
  stat_ecdf(aes(color = Party, shape = Party), geom = "point") +
  scale_linetype_manual("LINETYPES", values = c(
    "AKP" = "solid",
    "CHP" = "solid",
    "HDP" = "dotted",
    "MHP" = "dotted")
  ) +
  scale_shape_manual("SHAPES", values = c(
        "AKP" = 16,
        "CHP" = 17,
        "HDP" = 18,
        "MHP" = 19) # see docs for shape identifiers
      ) +
  scale_color_brewer("PARTY", palette = "Set2") +
  labs(x = "Hello", y = "World") +
  theme_bw()