Plotting made easy with the power of functions.

What do you do when you have a million variables to plot (separately), and need to speed the process up a bit? Write a function to do your plots for you!

After 4-ish years of steady work in #rstats, I had a (minor) break-through last week. Instead of editing scripts for each plot (time-consuming, and confusing), why not write a function to produce the plots? Seems obvious, and I’m embarrassed that it took me so long to think of it.

So here we go.

 #your dataset
Time<- seq(1:10)
A <- rnorm(10, mean=15, sd=3)
B <- rnorm(10, mean=15, sd=3)
C <- rnorm(10, mean=15, sd=3)
D <- rnorm(10, mean=15, sd=3)
E <- rnorm(10, mean=15, sd=3)
F <- rnorm(10, mean=15, sd=3)
G <- rnorm(10, mean=15, sd=3)
H <- rnorm(10, mean=15, sd=3)
plotf <- function(Time,variable) {
df <- data.frame(Time,variable) #make variables into a data frame
library(ggplot2) #ggplot for life!
# we'll use this to label the y-axis independently
y_name <- deparse(substitute(variable))
ggplot(data=df,aes(x=Time)) +
geom_line(aes(y=variable)) +
labs(x="Time", y= y_name)+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title = element_text(size=20),
axis.text = element_text(size=16),
panel.background = element_blank(), axis.line = element_line(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA)) #all of this is just to make the graph look nice
#produce your plot!
plotf(Time, B) 
Et voila! Make 10 identical plots in ~2 minutes. 

Maybe I’m a Scientist?

I’ve made no secret about how hard I’ve found PhD life (see here, if you want to feel a bit sad, but maybe also happy?), and the decline in post frequency here (in what was already a blog of infrequent posts) probably reflects the general feelings of disconnect I have about academia. At this stage in my ‘academic journey’ – i.e. facing the gunbarrel of thesis completion, and the end of my stipend, and unemployment, and etc. etc. – I am very much in ‘head down, get it done, get out of academia’ mode.

Anyway, all of that to say that I don’t often feel like a scientist. This is likely rooted in the ivory tower idea that if you’re not in academia you’re not really doing science, and I’m pretty dead set on leaving (at least for a while). Of course, I know this is bullshit, and if any of my scientist friends outside of academia told me they weren’t scientists I would be bathing in the blood of whoever gave them that idea telling them very earnestly, with itemised examples, that they are indeed scientists.

But today. Today, I’m a scientist. Why is that? I got my first ever peer-reviewed journal article published. It took many rounds of review, some tears, alcohol, and a small tantrum, but now it’s out there for the world to see (and judge, which is a whole ‘nother fear). And I’m a scientist!

You can check it out here, (or drop me an email and I’ll send you a pdf).

Unrelated photo, but Summer has finally returned to Perth and life is beautiful!


A Tale of Two Conferences

In the last few months I have been lucky enough to attend a couple of international conferences. Perth is a long way from anywhere, so being able to get out of town and meet some scientists is pretty bloody fantastic!

In December I went to the American Geophysical Union Fall Meeting (along with half of UWA, or so it seemed). Anybody in earth sciences knows that the AGU is the big kahuna of conferences. This year 23 000 people attended, and there were talks on everything from the weather on Mars to river hydraulics. It’s awesome. It’s also very, very overwhelming. Just getting there involved 3 flights (PER-SYD-LAX-SFO) and took something like 30 hours.  Needless to say I was in peak form when I stumbled into my accommodation on the Sunday afternoon. The second thing that nobody tells you is that you can’t see everything. There is no physical way you can attend every talk you’re interested in, see every poster, shake every hand. It’s just too big. Letting go of this attendees guilt is also tricky. Choosing between a palaeo-climate talk and a groundwater hydrology talk is impossible, and sometimes you just have to sneak away and go and learn something about Mars because if someone brings up the Navier-Stokes Equation one more time you might just kill yourself. Or them.


Gleefully science-ing with the world’s biggest poster

While I felt that my poster session went well (people actually stopped and talked to me, and seemed somewhat interested), I felt disappointed with the conference as a whole. It’s not that it’s not great (it is so, so great), but at this stage in my career (i.e. as a nobody), it’s hard to escape the feeling that the people that you want to meet, aren’t there to meet you.

For a lot of academics, the AGU is the best opportunity to catch up with international colleagues and scheme. This is understandable, but without an avenue to get into this scheming it is easy to feel like the hard work in getting there and presenting isn’t worth it.

Nonetheless, the AGU is brilliant. I highly recommend going if you have established connections who are also going! As a lowly PhD candidate who knows no-one, I’m not convinced it was worth it.

Wow. So excited to be here. #Yosemite #halfdome #sunset #California @yosemitenps

A post shared by micha campbell (@micha_campbell) on

Bonus picture of Half Dome in Yosemite, because if you think I was going to go to California without going to Yosemite you’re nuts.

Last week (early February) I went to New Zealand to attend the 17th Biennial Australian and New Zealand Geomorphology Group meeting. This experience couldn’t have been more different. Just 80 attendess, ‘trapped’ in a tiny town (Greytown, north of Wellington in the Wairarapa) for a week. It was awesome. Also, I am moving to NZ. It’s like Australia but better.

Over the week I think I spoke to every single attendee. I saw every talk, because there were no concurrent sessions. By the end of the week I felt like I had not only made some good contacts, but I had met people who I could see myself becoming friends with. It was such a stark contrast to the (admittedly fun) chaos of the AGU.

So I guess my half-baked advice to PhD Candidates out there, who have limited funds to get to conferences is not to discount the smaller conferences. Talking to 80 people at a smaller conference has got to be more beneficial than talking to 20 people at a giant conference. Saying that, if you have money to burn, go to the big conferences. Variety is the spice of life, and something like the AGU is spicy as hell!

Field trip to the entrance to the Dimworld  the Putangirua Pinnacles. 

Just Another Statistic

Over the weekend I had a bit of self realisation that may or may not surprise anyone in academia – I’m a bit depressed. In retrospect, this is not a surprise. Forgetting about research that has show that many PhD Students experience depression during their candidature, I feel like I should have realised that something was up heaps sooner.

Glaringly obvious signs that I should have noticed:

1. Massive decrease in my work ethic

LOVE science. I have loved science my whole life. Whether it was dinosaurs and ancient civilisations (thanks Jurassic Park and Indiana Jones), highschool biology (shout out to my highschool biol teacher, on whom I had a bit of a crush, and who was the most enthusiastic teacher I ever had), or later climate science, geomorphology, and hydrology, I have had a lifelong love affair with science. So when I look back at all the days this year when I have hated the thought of opening my laptop, when I have devoted hours to agonising over work, I probably should have realised that something was a bit off.

2. Loss of empathy (?)

I’m not sure empathy is the right word. But I’m missing something in my personal life at the moment. I’m normally pretty happy. I have good friends, who I love, and a great family, who I adore. It’s hard to describe, but I’m missing the ‘caring.’ I haven’t seen my parents in over a year (we live ~4000 kms away, but this is still unusual). I don’t even particularly want to see them, but I don’t not want to see them either. I just haven’t seen them. I also wrote a blog (unpublished – I don’t think I’ll ever publish it) about how I was just going through the motions with my friends. I didn’t even realise I wasn’t OK then. I was starting to think I was a sociopath, but thankfully it doesn’t look like that’s the case!


Anyway, I realised on Friday night (on the train of all places), when I was talking with my besty. She is obviously more in the know than me, since her reaction was pretty much ‘yeah.’ Since then I’ve been doing a lot of thinking.

This thinking started very bizarrely with depression impostor syndrome. Which took the shape of not feeling depressed ‘enough’ to bother doing anything about it. I.e. because I’m not suicidal or self-harming, I’m not depressed enough to worry about and I should just get over it. Fuck you sub-conscious. Luckily, I think I’m over this, and pretty keen to feel better.

I had a really good discussion with a friend who’s a clinical psych researcher about links between cognition and emotions which I think will give me some tools to recognise and change some of my behaviours and mindset. I’ve definitely noticed that just being aware of the problem has made me feel better, and has made me able to recognise when I’m paralysed and find some task to do to unfreeze me. At this point just realising that I’m not 100% seems to have helped a heap.

I also told my supervisor. What a fun discussion. Thankfully, my supervisor is a bit of a hero, he’s extremely supportive, and we discussed me maybe taking some time off next year if things don’t improve.

I guess I’m putting this out there as just another piece of the puzzle in academic mental health. While I’m not bringing up my mental health in daily conversation, I’m not ashamed of not being OK either. I’m also publishing it as a bit of an explanation/apology to friends and family – I’m sorry that I haven’t been able to be as good a friend as I would like to be lately.

Merging Social Networks in R

At the moment I am interested not just how people are interacting within different co-authorship networks but how knowledge is shared between co-authorship networks. So I’m trying to figure out how to merge the different networks I’ve developed into one big, comprehensive, co-authorship network. The key to this being successful will be in retaining some identification of the original network (I think they will probably be colour coded) and the authors that collaborate across co-authorship networks will have their own colour. I haven’t figured this bit out just yet…

However, I have managed to merge the networks! And while I haven’t done it the easiest (i.e. combine the .bib files and run the network script from scratch) I think my way will be the most useful for keeping distinct sub-network attributes.

The first step was to draw up a dummy network. My co-authorship networks have thousands of unique authors, it’s much simpler to work with dummy data when working out the kinks (and then hopefully it will translate to the full data-set…).


I then wrote these simple ‘publications’ up as .bib files called ‘pink.bib’, ‘blue.bib’ and ‘orange.bib’. Original. I won’t include them all here but pink.bib looks like this:

author = {Allen, A. and Baker, B. and Cook, C.},
journal = {Fictional Journal},
number = {14},
pages = {14--17},
title = {Paper 1},
volume = {29},
year = {2002}

author = {Cook, C. and Ferrer, F.},
journal = {Fictional Journal 2},
number = {14},
pages = {14--17},
title = {Paper 4},
volume = {29},
year = {2003}

You get the idea.

Next I plotted all three networks separately using similar code to that detailed in this post.  However, I exported the co-author tables  (called dat, see below) so I didn’t have to continually re-run the network script. You can do this using write.table().

write.table(coauth.table, file = "pink.txt")


Once I had all three co-author tables for my three dummy networks (dat, dat2, and dat3) I called them into a fresh R console. I assigned them different vertex attributes (i.e. different colours) and finally merged them.

See the script below!



dat<- read.table("pink.txt")
#you can check the class (format) of the data using the command:
#dat is a data frame, need to change it to a matrix to work with igraph
dat <- as.matrix(dat)
#here is where we create the adjacency network. Same step as in previous CNA post.
auth.graph <- graph.adjacency(dat, mode='undirected', weighted=TRUE)

#To set all vertices in this network to display as green - this will hopefully come into play when I finalise the joined network
dd <- set.vertex.attribute(auth.graph, "color", value = "green")

plot(dd, vertex.label.cex=0.8, edge.width = E(dd)$weight)

This gives us the green sub-network.

dat2 <- read.table("blue.txt")
dat2 <- as.matrix(dat2)
auth.graph2<- graph.adjacency(dat2, mode='undirected', weighted=TRUE)
ee <- set.vertex.attribute(auth.graph2, "color", value = "blue")
plot(ee, vertex.label.cex=0.8, edge.width = E(ee)$weight)


dat3 <- read.table("orange.txt")
dat3 <- as.matrix(dat3)
auth.graph3<- graph.adjacency(dat3, mode='undirected', weighted=TRUE)
ff <- set.vertex.attribute(auth.graph3, "color", value = "orange")
plot(ff, vertex.label.cex=0.8, edge.width = E(ff)$weight)

Now for the final step – joining the sub-networks into one large co-authorship network. I still need to figure out how to retain the attribute information (colours) for each sub-network, but for now I’m content that I’ve got the bulk of the work done.

##Loses attributes
gg <- graph.union(dd, ee, ff)

Hopefully I’ll get the last of the scripting done this week (forever overly-optimistic about how long it takes me!) and can show you the final product soon. Any suggestions are very welcome.

Co-authorship Network Analysis (CNA) in R

I’m planning to do some meta analysis of co-authorship in my field, which is really cool, but there is one small problem: I have no experience in co-authorship analysis, or any kind of complex network analysis! Luckily, I’ve got some pretty excellent Google-fu, and I found a heap of blogs and other sources (posted at the bottom) to work with.

I plan to pull bibliographic data from Google Scholar or Web of Science, so this code uses bibtex files as the data source. First I made a dummy .bib file of ‘co-authors’ from the seven Harry Potter books. HP uber fans, please forgive any overt errors. The files were based on the first few characters from each book that I could think of, and now that I look at my final network I’m tempted to go back and do it properly. I named this file HP.bib and put it in my directory.

author = {Harry and Ron and Hermione and Malfoy and Hagrid},
title = {{Get that Stone!}},
journal = {The World of JKR},
year = {1996}
author = {Harry and Ron and Hermione and Fred and George and Neville and Myrtle},
title = {{Big Snakes are Scary!}},
journal = {The World of JKR},
year = {1998}
author = {Harry and Ron and Hermione and Sirius and Lupin and Malfoy and Wormtail},
title = {{Big Dog! I'm Going to Diel}},
journal = {The World of JKR},
year = {1999}
author = {Harry and Ron and Hermione and and and Malfoy and Wormtail and Tonks and Voldemort and Cedric and Cho and Dumbledore},
title = {{Kill the Spare}},
journal = {The World of JKR},
year = {2000}
author = {Harry and Ron and Hermione and Sirius and Lupin and Molly and Dumbledore and Bellatrix and Voldemort and Lucius},
title = {{It's a Trap}},
journal = {The World of JKR},
year = {1999}
author = {Harry and Ron and Hermione and Dumbledore and Snape and Slughorn and Voldemort},
title = {{Harry gets an A}},
journal = {The World of JKR},
year = {2001}
author = {Harry and Ron and Hermione and Fred and George and Hagrid and Voldemort and Lucius and Malfoy and Lupin and Tonks},
title = {{Harry Dies}},
journal = {The World of JKR},
year = {2003}

The script for R is below. I tried to annotate the script as I went, but my Google powers exceed my #rstats skills, so there are cases when I’m not exactly sure why something works, just that it does work.


#you may need to add the igraph, bibtex, and ggplot2 packages to R


#read the .bib file in

citations <- read.bib("C:/Users/me/HP.bib")
#gives list of all citations and authors
authors <- lapply(citations, function(x) x$author)
#gives list of unique authors
unique.authors <- unique((unlist(authors))[grepl('family', names(unlist(authors)))])

#take a look!

#sets up the co-author adjacency matrix

coauth.table <- matrix(nrow=length(unique.authors),
ncol = length(unique.authors),
dimnames = list(unique.authors, unique.authors), 0)

#for loop that fills the adjacency matrix. Adds 1 to the matrix each time co-authors work together

for(i in 1:length(citations)){
paper.auth <- unlist(authors[[i]])[names(unlist(authors[[i]])) == 'family']
coauth.table[paper.auth,paper.auth] &amp;lt;- coauth.table[paper.auth,paper.auth] + 1

#stops authors co-authoring with themselves - I was getting weird loops back to each node, and this fixed them!
coauth.table <- coauth.table[rowSums(coauth.table)>0, colSums(coauth.table)>0]
diag(coauth.table) <- 0

#inspect table

#remove ('and') from coauthor table. I used 'and' to separate the authors in the .bib file, and while 'and' wasn't listed in the author list, it was populated in the matrix table. Don't know why, but this fixed it.
coauth.table <- coauth.table[-13,-13]

#somehow this fixed the problem of double links when voldemort and tonks, and tonks and voldemort co-authored 😀
auth.graph <- graph.adjacency(coauth.table, mode='undirected', weighted=TRUE)
plot(auth.graph, vertex.label.cex=0.8, edge.width = E(auth.graph)$weight)

#gets edge list from the adjacency graph.

edge <- get.edgelist(auth.graph)

#re-loading edgelist
igraph <-, directed=FALSE)

#Tcl/Tk Network Graph
tkplot(igraph, layout=layout.fruchterman.reingold)

#to make the co-authorship network less quantitative and a bit more qualitative, you can get numbers out of the CNA!

#Calculating centrality measures (dgree, betweeness, closeness, eigenvector centrality, coreness)
metrics <- data.frame(
#print metrics

#visualising metrics. I like ggplot, but any old plotting in R is fine. It just won't look so nice!
aes(x=bet, y=eig,
label=rownames(metrics)) #,
#colour=res, size=abs(res))
xlab("Betweeness Centrality")+
ylab("Eigenvector Centrality")+
ggtitle("Key Players in Harry Potter";)

And this is the network that you get. It’s definitely not the prettiest, but I couldn’t be bothered screwing around with aesthetics for a dummy run, I’m just pleased that it works!

And now, to all the people who’s code I unashamedly stole: thanks for hosting your scripts online! This is also why I blog my forays into coding, even though I’m not great – I’ve learned so much from other people’s code, it seems only fair to put it back out there.


A Sneak’s Guide to Avoiding Loneliness in Higher Education (Informed by Personal Experience): A self-directed workshop to kick-start your new friend group

This post was written to fulfil the last requirement for the How to Survive Your PhD MOOC. It is fairly tongue-in-cheek, but I actually used all of the methods I proposed. I hope none of my friends think I’m a sociopath based on this. 

Objective: To weasel your way into the potentially close-knit, established, friendship group at your university.

Rationale: Starting a PhD can be lonely. Often it involves moving to a new city, potentially a very long way from home (possibly overseas) and it’s fairly unlikely you will know anyone, not just in your office, school, or university, but even in the new city. What’s more, in a recent study, 47% of UC Berkely PhD students were found to be depressed – a strong social network can help to ameliorate or prevent the symptoms of depression. Unfortunately, it is not your supervisor’s job to help you make friends (although a good supervisor will make sure you are introduced to at least some of the cohort). So, it is up to you to kick-start your new friend group!

Resources: You, and a willingness to ‘put yourself out there’.

Methods: We will go through 4 tried and tested (by yours truly) ways to make friends in higher education.

  • Method 1: The hopeful glance
  • Method 2a: The pro-active organiser
  • Method 2b: The pro-active committee-joiner
  • Method 3: The not-so-lonely Tweeter.

Method 1: The hopeful glance is a technique that works around developing a certain ‘puppy dog’ look. This glance is most effective when office mates are heading to lunch or to the pub. The hopeful glance is useful with empathetic office mates who won’t need to much pushing to invite you into the fold and should be your first course of action.

Method 2a: The pro-active organiser is a method best used when there is not much cohesion in your office. Have you found yourself in an office full of people who prefer to eat lunch at their desks? Pick a nice sunny day and suggest taking lunch outside for an apparently impromptu picnic. Once initial in-roads are made the pro-active organiser method can be used to take the friendship off-campus. Suggest a group activity (my office went cliff-jumping followed by a picnic. It was a great afternoon out and helped to bring everyone together).

Method 2b: The pro-active committee-joiner SHOULD ONLY BE USED WITH CAUTION. Joining committees (such as your postgraduate student association, or a club) can be a great way to meet people. I joined my School’s postgraduate representative group which allowed me to work closely with a small group of really cool people and to get to know just about everyone in the School. However, care must be taken not to join too many committees – this will result in no thesis work being done, and much stress and anxiety!

Method 3: If the above haven’t worked out, don’t despair! We live in a hyper-connected society, and social interaction is only a click away. Twitter is a great way to interact with other PhD students, as well as find mentors and laugh at the many, many research-related novelty accounts. Find a quick summary of my favourite tweeters and hashtags below:

  • @AcademicsSay
  • @AcademicBatgirl
  • @ThesisWhisperer (of course)
  • @ResearchMark
  • @AstroKatie
  • @realscientists
  • #phdchat
  • #survivephd15

I hope that by working through these methods you will find yourself surrounded by brilliant, like-minded people who support you, boost your creativity and self-esteem, and help you really enjoy the PhD Experience!

Good luck!

Micha (@michcampbell_)