0

I am interested in using a condition and illustrating it in ggplot2- specifically, if the absolute difference between T2 and T1 is greater than one (i.e. abs(T2 - T1) > 1), then I color (in black) that particular data point on "T2".

However, the problem is that I'm unsure how to do this in code - and sufficiently, at that.

This is the code:

library(ggplot2)
library(reshape2)
library(directlabels)
df[, c(2:13)]<-lapply(lapply(df[,2:13], as.character), as.numeric)
df<-data.matrix(df)
dat = melt(subset(df, select = c("Value","Condition")),id.vars = "Condition")
dat<-dat[1:40,]
dat[,1]<-c(rep(1:20,2))
dat[,2]<-c(rep("T1",20),rep("T2",20))
colnames(dat)[2]<-"Time"
ggplot(aes(x = Var1, y = as.numeric(as.character(unlist(dat$value))), color = Time, group=Time), 
       data = dat, label = value) + 
  geom_point(aes(shape=Time, size=4)) + geom_line() + 
  geom_text(aes(label = round(as.numeric(as.character(unlist(dat$value))), 2)), 
            method="smart.grid", color = "black") +
  xlab("Items") + ylab("Values") + ggtitle("Line Graph")

This is the chart:

This is the dput() output:

dput(df)
structure(c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, -2.77799880286286, -5.50944520378118, 1.88836435580497, 
-5.58406005229873, 4.44735158239659, -6.82897889434699, -2.1716215347289, 
-6.24523609922322, 2.76458526645867, 5.48271803933499, -6.14473005987329, 
-1.5417866304646, -6.62612955305313, 3.70896578961902, -3.4179458605496, 
1.82132726703399, -0.9280760020449, -1.37160088556748, 3.19524553300007, 
-4.95347572092664, 2.85061161249751, -1.86116126313546, 5.02499132690551, 
-0.589243309301991, 4.82762042912157, 0.169968256089778, -2.19470633021516, 
-1.49426127124266, 4.0326779589342, 5.48630151034464, -3.04465732022428, 
2.74719269076656, -3.42432474774963, 7.17005454165613, 3.29173512430591, 
6.15013558100303, 3.37225798878721, 3.16447670131925, 5.76818087898048, 
-2.47667639336567, 2.9410259668896, 5.24942846129507, 2.95615407147939, 
2.53728090334437, 4.45378164065699, 4.99724304408089, 3.41151350933973, 
6.5558635012366, 2.9382584815192, 5.553396018278, 5.66712392721592, 
3.70722135586978, 4.91809317051882, 4.43567725100426, 4.85957115798564, 
1.13776426116428, 1.45147180831314, 2.93845495539568, 3.69799943125266, 
5.58188967776966, 1.70671301941748, 1.87716808779163, 4.52979801504946, 
4.02882680021941, 4.47697541167503, 2.00725924508944, 1.47797506789819, 
2.81083558050484, 1.73059487384682, 2.93638454645181, 2.11798237001241, 
1.56476900094613, 2.8051256046925, 4.91148661864085, 2.39984241054436, 
6.01680803527874, 3.45216832637263, 3.86786912841398, 5.14510980394688, 
1.96226240820569, 1.46769784381359, -4.23315686452177, 1.34462318507629, 
-4.13803301156994, 3.46433009466818, -0.688603097392773, -0.864647679558938, 
-2.41397623113989, 1.9258218879359, 2.12843971854681, -3.90465123084954, 
2.85141616984777, -0.425193435370328, 1.26215227718771, -0.443706739548623, 
1.99409077435011, 0.993384901206013, -1.2539971894181, 2.16674821850839, 
-1.14332285049104, 1.2877641319428, -5.0790640948831, 2.16491856402794, 
-5.7209806024071, 2.42987914111965, -0.0338801558976428, -4.05418787838149, 
-4.66851179473313, 2.84008251058085, 3.82691297178971, -4.84551053369274, 
0.081540160504272, -2.15526245166253, -0.176870366357385, -0.877844323553365, 
-0.258594819181801, 0.430370682726835, -3.49027845749809, 0.331486593462475, 
-4.37685606663863, 1.12112630791616, 3.84715347197235, 1.47596355786357, 
4.04476901846664, 3.22121946000162, 2.13221568496659, 0.652861523704772, 
1.86053940564217, 2.18750685665418, 2.22409039914394, 3.31684834987514, 
2.37433623873005, 1.96881662200605, 0.623734917918608, 0.938645410686527, 
1.6194531430081, 1.0572785346611, 1.80192982083783, 2.30928777991719, 
1.33090194123859, 2.30457009724777, 4.44215663384225, 1.9456455099693, 
5.5491241262749, 2.90365637378558, 1.35352785699859, 4.00797171059206, 
4.54513631393887, 4.11261546736008, 4.72222683677877, 4.77697916123432, 
0.401623824136378, 2.80147690765104, 1.44060736377897, 1.20626242635587, 
0.335776941897886, 0.870712988549501, 2.82555530959146, 1.02961044767606, 
4.40677773658637, 1.08733086714195, -4.58237221368757, 0.500775366216656, 
-2.24308867788549, 0.923483146787494, 0.983312535004998, -0.506717556382614, 
-0.704911411238621, 0.789477493179571, -0.263046689942195, -2.43579693951632, 
1.14410003345258, 0.903308056174724, 2.56662144872607, -2.83034226881492, 
1.72659090224099, 1.48023061106132, -5.30639508766639, 1.11099764546048, 
-4.19126050554502, -1.05089323769677, 1.54372391361437, -1.56352981645499, 
2.93127137918086, 0.501362777825551, 0.346307986678131, -0.344374655196894, 
1.91773468184104, -3.61476249608386, -4.5451606571318, 3.13201396026136, 
1.10416369238183, -1.6515940027554, -2.30082843813389, -3.24938747503207, 
1.64169861482598, 1.25939003291661, -3.22315899193569, 1.02901350502441, 
-0.307596191022465, 0.607172870047824, 3.18670101637946, 1.18329147532167, 
1.73931931950462, 1.08642392258211, 1.75963707713729, 0.598209932001034, 
0.71799058314335, 1.71030492189432, 1.86973358441346, 1.96214081370305, 
2.12865325016569, 1.86580109155386, 1.99385141233056, 1.9304518662167, 
1.67887046756132, 1.89380653935655, 3.51584085505365, 1.59008281821586, 
2.63776915192942, 2.01324356378045, 1.92426610560178, 2.01351363124494, 
2.09837681150714, 0.564283583646676, 1.94900394722626, 2.03932275387923, 
1.77291581898077, 3.31700177429301, 3.56054613450095, 2.48818428369487, 
1.56026700091836, 0.895001669911228, 2.45494944723334, 2.49614852057026, 
0.980970610101227, 1.2327710192819, 1.90720295041313, 1.52990252674699, 
2.24055435759981, -1.73119862850002, 1.0216815782015, -3.08104316219856, 
0.742062185068333, 0.637759900635563, 0.313639004730233, -1.04389320657185, 
-0.168343229992165, -3.50957435418526, -0.805146224350448, 1.89813728483076, 
2.11234302788144, 0.65291943976787, -1.66538732232707, 2.06761784535478, 
1.07368735890632, 1.18116854535688, 4.35475739971616, 2.4291449411867, 
2.66250155475042, -3.07984879560606, -0.982161144689049, -2.17134923344571, 
-1.04981167576708, 1.02982534690544, 0.0739956440116955, -3.53127755255401, 
-0.983920844230844, -2.4237703274689, 1.1137895914255, 1.88292910019935, 
1.88115084031971, -0.528776945568896, -2.51963703622653, -1.21224167026079, 
-0.00733001295324706, 0.489654593226242, 1.64244792747709, 1.97849384758581, 
-2.73222532916273, 1.30664000318633, 2.51152591709287, 2.3610878671388, 
2.68821086374941, 0.756179657571522, 1.31406083006702, 0.502270349719078, 
0.877048724863803, 3.27032715567304, 2.02817740776692, 3.13885298082316, 
1.69040861120208, 1.44621246031203, 2.72542768045343, 1.65168160243242, 
1.0613486997877, 1.36730424217884, 4.70242398349343, 1.47846129638809, 
2.90982977301074, 1.6415398652502, 1.80971856575705, 1.80126834813569, 
2.30402758910142, 0.453944083869317, 1.44975937874442, 2.76072379297129, 
2.05179959258098, 1.96034746487189, 2.46972904118759, 1.52646811270462, 
1.68792219492701, 0.942581896362376, 1.43412205194171, 1.32429401657411, 
0.74028817732329, 0.934463299964322, 1.75899225369897, 1.73093691569634, 
2.44604484756581, -0.81707831508849, -0.101640659501738, -0.199372098325561, 
-0.731350355341286, -1.50185595150164, -2.15385646903906, 0.341362372622556, 
0.0717794525986977, -0.674790643399441, -1.02832745657856, -0.807289055054835, 
-0.273625459413725, -1.89714842447061, -2.72809814735591, -1.87304656922355, 
-1.75211939950996, -0.179811464643785, -2.47054193194854, 0.875985668308142, 
-0.87107168324186, 2.88411862461663, 1.13350717882749, 0.881717271657503, 
-0.680906046795757, -0.00986342272375014, -1.30401468178664, 
-0.718835987756961, -0.274692056120372, 2.25741424032245, -0.733100021865329, 
0.410517365563169, 2.17391980797268, -1.72828926038821, 2.32332016354502, 
-0.35469728539741, 0.547277997817807, 1.9063894042504, -1.38302649805023, 
4.22638973141164, -0.834303241714936, 1.29217284997393, 0.920346644301893, 
1.41581152942903, 1.68422758278347, 1.46401543006383, 2.59715468920863, 
0.671463941291187, 0.391827833427845, 0.830409256044418, 1.51196965141483, 
1.25622927071156, 0.633365193686345, 2.45392708107328, 2.15333609263216, 
2.38240811335379, 1.93971167427935, 0.20678171606071, 3.13623314571654, 
1.2020914095021, 1.45795937568229, 2.79079354402284, 1.96590935476358, 
1.02143633694447, 0.891283752569684, 0.516990294238038, 1.51976159759388, 
0.454055895284961, 0.80654340174185, 2.14646393071754, 1.8189172212192, 
1.21142394057217, 1.84172112667041, 1.89688920138007, 2.59190659576094, 
0.810205134547583, 0.731657346597015, 1.70901155496863, 1.21455024297602, 
3.52967217716404, 0.754338180088601, -1.29611694379859, -2.02848235962263, 
-1.88100885357239, 0.00782681463321539, 1.26584897977176, -1.11647972403716, 
-1.087284846439, -0.0952271741106955, 0.131800629971024, 3.72882805009692, 
-0.417680991612429, -2.40929168614188, -0.954139748948513, -0.185182454834748, 
-2.31198436769972, -0.601593634688318, -0.358753304703935, 0.264121584587967, 
0.877095537027569, -1.04128766257684, -1.34634072546986, -0.711769725913958, 
1.56173816149338, 2.1056023883716, 0.849351763165274, -0.333430738703158, 
0.867470294719106, 3.06609445431155, 0.156548761957329, 3.23204999230325, 
0.594583943629732, -0.591859747610828, -0.771489333759391, -1.61849385298721, 
-0.994837577062622, -0.471029875852922, 0.494956557283429, 0.869778487578066, 
0.664401960629288, 0.705356572496125, 1.71969313558994, 1.22514664503418, 
2.43615010778863, 0.90543774831495, 1.47968455180469, 1.58025802579494, 
1.09964089759256, 0.424333680265671, 0.588266235029791, 3.134216074508, 
0.789168044524916, 1.29409026043504, 1.39999018584329, 0.685175161870907, 
2.1240505562516, 1.0118137455611, 0.274816868206689, 1.24463783903865, 
1.32271783466684, 0.839356205249546, 2.26239611885107, 0.81461450270275, 
1.04915471241655, 1.58417059541705, 1.43402870983137, 0.922256074093675, 
0.610663047712594, 1.67973206887357, 1.06535791664458, 3.55339972908477, 
1.29706172782603, 0.700609412377317, 1.10881444509497, 2.36973842570586, 
1.54286536846859, 0.39414111430605, 0.594818127352406, 0.990174530659289, 
1.66222872402804, 0.822538694893619), .Dim = c(40L, 13L), .Dimnames = list(
    c("1", "2", "3", "4", "5", 
    "6", "7", "8", "9", "10", "11", 
    "12", "13", "14", "15", "16", 
    "17", "18", "19", "20", "1", "2", "3", "4", "5", 
    "6", "7", "8", "9", "10", "11", 
    "12", "13", "14", "15", "16", 
    "17", "18", "19", "20"), c("Condition", "group1.value", 
    "group1.se", "group2.value", "group2.se", "group3.value", 
    "group3.se", "group4.value", "group4.se", "group5.value", 
    "group5.se", "group6.value", "group6.se")))

Applying the suggested commanded, the majority of the 20 data points for "T2" should be in black.

5
  • do you mean change the label to a bold black font or make the data point itself black? In the second case, what do you mean by bold data point (how should it differ from the other data points except for the color?) Commented Jul 21, 2015 at 7:47
  • 1
    also, could you dput(df) Commented Jul 21, 2015 at 7:48
  • 1
    @scoa, I see you point. I meant that instead of the data point being colored by the "T2" condition, it is colored in as black. Let me make the edits in the original post. Commented Jul 21, 2015 at 7:50
  • can you post your data? Commented Jul 21, 2015 at 7:58
  • 1
    @scoa, I just did. Sorry it took a while. Commented Jul 21, 2015 at 7:59

1 Answer 1

2

You could just add a second geom_point() subsetting dat to the only values where the diff between T1 and T2 > 1.

library(ggplot2)
library(dplyr)
library(tidyr)
library(directlabels)

df <- as.matrix(df)

rn <- row.names(df)
df <- data.frame(df)

dat <- df %>% select(Condition,group1.value) %>%
  mutate(item=as.numeric(rn)) %>%
  spread(Condition,group1.value) %>%
  rename(T1 = `1`,T2 = `2`) %>%
  mutate(diff=abs(T2-T1)) %>%
  gather(Time,value,-item,-diff)


ggplot(aes(x = item, y = as.numeric(as.character(unlist(dat$value))), color = Time, group=Time), 
       data = dat, label = value) + 
  geom_point(aes(shape=Time, size=4)) + geom_line() + 
  geom_text(aes(label = round(as.numeric(as.character(unlist(dat$value))), 2)), 
            method="smart.grid", color = "black",hjust=1,vjust=1) +
  xlab("Items") + ylab("Values") + ggtitle("Line Graph") +
  geom_point(data=filter(dat,diff>1,Time=="T2"),aes(x=item,y=value),color="black",size=4)

enter image description here

On a side-note, you may want to rethink your data organization. Your df is half-way between a long and wide data.frame and that doesn't work well with ggplot (that's why I used spread and then gather.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for putting in the time to do this. I definitely agree with you in that I should better streamline my code.
how did you deal with the error Error in mutate_impl(.data, dots) : object 'dat' not found? I got the below code to work, but I had to run it twice:.... dat <- df %>% select(Condition,dim1.scale.value) %>% mutate(Subscale=rn) %>% spread(Condition,dim1.scale.value) %>% mutate(diff=abs(as.numeric(dat$Post)-as.numeric(dat$Pre))) %>% gather(Time,value,-Subscale,-diff)
mutate(diff=abs(as.numeric(dat$Post)-as.numeric(dat$Pre))) should be ` mutate(diff=abs(as.numeric(Post)-as.numeric(Pre)))` because dat does not exist before the chain has been evaluated. In a dplyr pipe, you can call variables directly, without their dataframe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.