r - ggplot: how to choose the "proper" colors relating on a column -
suppose have simple dataframe plot, in have color points related measure contained in column. so, if have:
dataframe # x1 x2 pop # 1 -0.11092652 -1.955598e-09 448053 # 2 -0.09999865 -2.310067e-10 418231 # 3 -0.05944755 -3.475013e-09 448473 # 4 0.51378848 1.631781e-09 119548 # 5 0.09438223 -9.606475e-10 323288 # 6 0.19349045 6.074025e-10 203153 # 7 0.06685609 3.210156e-10 208339 # 8 -0.10915456 -1.407190e-09 429178 # 9 -0.10348100 -1.401948e-09 1218038 # 10 -0.08607617 -7.356602e-10 383018 # 11 1.00343465 -2.423237e-08 209550 # 12 -0.05839148 1.503955e-09 287042 # 13 -0.09960163 2.167945e-10 973129 # 14 -0.05793417 2.510107e-09 187249 # 15 0.02191610 2.479708e-09 915225 # 16 0.48877872 1.338346e-08 462999 # 17 -0.10289556 1.472368e-09 1108776 # 18 -0.10316414 2.933469e-10 402422 # 19 -0.09545279 -2.926035e-10 274035 # 20 -0.06111044 3.464014e-09 230749
and use ggplot in following way:
ggplot(dataframe) + ggtitle("somehow useful spatialization")+ # electricity / gas geom_point(aes(dataframe$x1, dataframe$x2), color = dataframe$pop, size=2 ) + theme_classic(base_size = 16) + guides(colour = guide_legend(override.aes = list(size=4)))+ xlab("x")+ylab("y")
that possible representaion. neverthless, suppose want points colored such represent column pop
, i.e., having colors (for example) light orange, passing dark red , black. how can "scale" column pop
obtain such graphics?
edit:
> dput(dataframe) structure(list(x1 = c(-0.110926520419347, -0.0999986452719714, -0.0594475526112884, 0.513788479303472, 0.0943822277852107, 0.193490454204271, 0.0668560854540437, -0.109154563987586, -0.103480996064617, -0.0860761723229372, 1.00343465471568, -0.0583914756527933, -0.0996016272609995, -0.0579341671474729, 0.0219161022704227, 0.488778719096658, -0.102895564162661, -0.103164140322136, -0.0954527927249849, -0.0611104428640883), x2 = c(-1.9555978205951e-09, -2.31006712207053e-10, -3.47501251356368e-09, 1.63178106438806e-09, -9.60647459243156e-10, 6.07402512804044e-10, 3.21015629676789e-10, -1.40718981687972e-09, -1.40194842954735e-09, -7.35660154466167e-10, -2.423237202138e-08, 1.50395541775022e-09, 2.16794489937917e-10, 2.51010717100061e-09, 2.47970820013341e-09, 1.33834570208731e-08, 1.47236816671351e-09, 2.93346922578509e-10, -2.92603459149485e-10, 3.46401369936372e-09), pop = c(448053l, 418231l, 448473l, 119548l, 323288l, 203153l, 208339l, 429178l, 1218038l, 383018l, 209550l, 287042l, 973129l, 187249l, 915225l, 462999l, 1108776l, 402422l, 274035l, 230749l)), .names = c("x1", "x2", "pop"), row.names = c(na, 20l), class = "data.frame")
with ggplot can add aesthetics (aes) in inital ggplot call. since you're telling ggplot data (in dataframe
), can refer variables directly name (without dataframe$
). color scale needs called aesthetic, inside aes()
call, , not static value. once added aesthetic, can customize how reacts adding scale
. taking account gives following code:
ggplot(dataframe, aes(x = x1, y = x2, color = pop)) + ggtitle("somehow useful spatialization")+ # electricity / gas geom_point(size=2) + theme_classic(base_size = 16) + guides(colour = guide_legend(override.aes = list(size=4))) + xlab("x")+ylab("y") + scale_color_gradient2(low = "green", mid = "red", high = "black", midpoint = mean(dataframe$pop))
this code gives following graph. colors further adjusted playing around scale_color_gradient2
part. (why green low
gives better orange choosing orange low
color beyond me, ended there coincidence)
Comments
Post a Comment