Skip to content

Instantly share code, notes, and snippets.

@DSLituiev
Last active October 5, 2016 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DSLituiev/31063fc862b35a729142d3949e803c73 to your computer and use it in GitHub Desktop.
Save DSLituiev/31063fc862b35a729142d3949e803c73 to your computer and use it in GitHub Desktop.
ggplot error on colouring points
dfstr = '''{"FDR":{"0":0.0000001434,"1":0.0974543339,"2":0.5637882089,"3":0.7592318769,"4":0.82376489,"5":0.876232133,"6":0.891150062,"7":0.9234070548,"8":0.9421697501,"9":0.9421697501,"10":0.9534575004,"11":0.9686912495,"12":0.9792339965,"13":0.9800272087,"14":0.9814635178,"15":0.9814635178,"16":0.9822809925,"17":0.9832209543,"18":0.9832209543,"19":0.9832209543,"20":0.0079716299,"21":0.6863477759,"22":0.7846481266,"23":0.8596431336,"24":0.8940558933,"25":0.9071788194,"26":0.9269123036,"27":0.9323909829,"28":0.936505473,"29":0.9477474057,"30":0.9479119432,"31":0.9479119432,"32":0.9479119432,"33":0.9479119432,"34":0.9479179113,"35":0.9577011312,"36":0.9589141116,"37":0.9601000647,"38":0.9614078093,"39":0.9657393252,"40":0.0000000308,"41":0.1179302537,"42":0.6074557222,"43":0.7534315164,"44":0.8290719564,"45":0.8917851883,"46":0.9325332126,"47":0.9495263504,"48":0.9532127733,"49":0.9552555496,"50":0.9594234241,"51":0.9675706204,"52":0.9737916651,"53":0.9767958687,"54":0.9767958687,"55":0.9767958687,"56":0.9767958687,"57":0.9817147714,"58":0.9890245893,"59":0.9904360708},
"FPR":{"0":0.0,"1":0.0044089859,"2":0.0554272517,"3":0.1079151795,"4":0.1604031073,"5":0.2128910351,"6":0.2653789628,"7":0.3178668906,"8":0.3703548184,"9":0.4228427462,"10":0.4753306739,"11":0.5278186017,"12":0.5803065295,"13":0.6327944573,"14":0.6852823851,"15":0.7377703128,"16":0.7902582406,"17":0.8427461684,"18":0.8952340962,"19":0.9477220239,"20":0.0,"21":0.029813143,"22":0.0734830989,"23":0.122821751,"24":0.1721604031,"25":0.2227587655,"26":0.2735670796,"27":0.3247952971,"28":0.3762334663,"29":0.4272517321,"30":0.4795297082,"31":0.5315977325,"32":0.5836657569,"33":0.6359437329,"34":0.6880117573,"35":0.7402897334,"36":0.7923577577,"37":0.8444257821,"38":0.896073903,"39":0.9481419274,"40":0.0,"41":0.0041990342,"42":0.0556372034,"43":0.1081251312,"44":0.160613059,"45":0.2131009868,"46":0.2653789628,"47":0.3178668906,"48":0.3703548184,"49":0.4228427462,"50":0.4753306739,"51":0.5278186017,"52":0.5803065295,"53":0.6327944573,"54":0.6852823851,"55":0.7377703128,"56":0.7902582406,"57":0.8427461684,"58":0.8952340962,"59":0.9477220239},
"TPR":{"0":0.0042194093,"1":0.970464135,"2":1.0,"3":1.0,"4":1.0,"5":1.0,"6":1.0,"7":1.0,"8":1.0,"9":1.0,"10":1.0,"11":1.0,"12":1.0,"13":1.0,"14":1.0,"15":1.0,"16":1.0,"17":1.0,"18":1.0,"19":1.0,"20":0.0042194093,"21":0.4599156118,"22":0.6371308017,"23":0.7004219409,"24":0.7637130802,"25":0.8016877637,"26":0.835443038,"27":0.8607594937,"28":0.8818565401,"29":0.9113924051,"30":0.9156118143,"31":0.9240506329,"32":0.9324894515,"33":0.9367088608,"34":0.9451476793,"35":0.9493670886,"36":0.9578059072,"37":0.9662447257,"38":0.9831223629,"39":0.9915611814,"40":0.0042194093,"41":0.9746835443,"42":0.9957805907,"43":0.9957805907,"44":0.9957805907,"45":0.9957805907,"46":1.0,"47":1.0,"48":1.0,"49":1.0,"50":1.0,"51":1.0,"52":1.0,"53":1.0,"54":1.0,"55":1.0,"56":1.0,"57":1.0,"58":1.0,"59":1.0},
"key":{"0":"A","1":"A","2":"A","3":"A","4":"A","5":"A","6":"A","7":"A","8":"A","9":"A","10":"A","11":"A","12":"A","13":"A","14":"A","15":"A","16":"A","17":"A","18":"A","19":"A","20":"C","21":"C","22":"C","23":"C","24":"C","25":"C","26":"C","27":"C","28":"C","29":"C","30":"C","31":"C","32":"C","33":"C","34":"C","35":"C","36":"C","37":"C","38":"C","39":"C","40":"B","41":"B","42":"B","43":"B","44":"B","45":"B","46":"B","47":"B","48":"B","49":"B","50":"B","51":"B","52":"B","53":"B","54":"B","55":"B","56":"B","57":"B","58":"B","59":"B"}}'''
import pandas as pd
import numpy as np
"read data"
data0 = pd.read_json(xstr).sort_values("key")
"find points at given threshold"
FDR_thr = 0.1
data0["ind"] = np.tile(np.arange(data0.shape[0]//3), 3)
data_piv = pd.pivot_table(data0, index="ind", columns="key").swaplevel(0,1, axis=1)
data_piv_at_fdr = {}
for kk, vv in (data_piv.swaplevel(0,1, axis=1)["FDR"] - FDR_thr).apply(lambda x : x.argmax()).items():
data_piv_at_fdr[kk] = data_piv[kk].loc[vv]
# plt.scatter(cumq[kk].loc[vv]["FPR"], cumq[kk].loc[vv]["TPR"], )
data_piv_at_fdr = pd.concat(data_piv_at_fdr,axis=1).T
################################################################
"PLOT"
"this works as expected:"
ggplot(data0, aes(x='FPR', y='TPR', colour="key")) +\
geom_line(size=3) + xlim(-0.05,1.05) + ylim(-0.05,1.05)
"this kind of works, but colour is ignored"
ggplot(data0, aes(x='FPR', y='TPR')) + \
geom_line(colour="r")
"""adding points from another dataframe works
(though the point colour is black instead of requested red)"""
ggplot(data0, aes(x='FPR', y='TPR')) + \
geom_line() + \
geom_point(data=data_piv_at_fdr, size = 100, shape = "o", colour="r")
"adding points from another dataframe, while setting colouring groups in the first breaks"
ggplot(data0, aes(x='FPR', y='TPR', colour="key")) + \
geom_line(colour="r") + \
geom_point(data=data_piv_at_fdr, size = 100, shape = "o", colour="r")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment