0

I have a dataframe that I want to reorder to make a ggplot so I can easily see which items have the highest and lowest values in them. In my case, I've grouped the data into two groups, and it'd be nice to have a visual representation of which group tends to score higher. Based on this question I came up with:

library(ggplot2)
cor.data<- read.csv("https://dl.dropbox.com/s/p4uy6uf1vhe8yzs/cor.data.csv?dl=0",stringsAsFactors = F)
cor.data.sorted = cor.data[with(cor.data,order(r.val,pic)),] #<-- line that doesn't seem to be working
ggplot(cor.data.sorted,aes(x=pic,y=r.val,size=df.val,color=exp)) + geom_point()

which produces this:

enter image description here

I've tried quite a few variants to reorder the data, and I feel like this should be pretty simple to achieve. To clarify, if I had succesfully reorganised the data then the y-values would go up as the plot moves along the x-value. So maybe i'm focussing on the wrong part of the code to achieve this in a ggplot figure?

2 Answers2

1

You could do something like this?

library(tidyverse);
cor.data %>%
    mutate(pic = factor(pic, levels = as.character(pic)[order(r.val)])) %>%
    ggplot(aes(x = pic, y = r.val, size = df.val, color = exp)) + geom_point()

enter image description here

This obviously still needs some polishing to deal with the x axis label clutter etc.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
0

Rather than try to order the data before creating the plot, I can reorder the data at the time of writing the plot:

cor.data<- read.csv("https://dl.dropbox.com/s/p4uy6uf1vhe8yzs/cor.data.csv?dl=0",stringsAsFactors = F)
cor.data.sorted = cor.data[with(cor.data,order(r.val,pic)),] #<-- This line controls order points drawn created to make (slightly) more readible plot
gplot(cor.data.sorted,aes(x=reorder(pic,r.val),y=r.val,size=df.val,color=exp)) + geom_point()

to create

enter image description here

  • So what's the point of your question&answer here? I've given an earlier solution below, then you post a repeat of my solution as an answer of your own question??? – Maurits Evers Mar 30 '18 at 01:58
  • @MauritsEvers the plots look similar, but the code to generate it is different (e.g. doesn't require tidyverse library). I think a bigger problem is that my solution is based on what I found on https://stackoverflow.com/questions/30633741/ggplot2-cant-sort-x-axis-by-y-value - suggesting that this question is a duplicate of it (which is why I've commented "possible duplicate" and mentioned link). – beepingbopping Mar 30 '18 at 07:01
  • (1) `ggplot` *is* part of the `tidyverse`. It is generally cleaner/tidier to do any data manipulation (like factor reordering) on the level of the data, and then forward the tidied `data.frame` to the plotting routine (instead of reordering whilst plotting). (2) Your answer came *after* my attempt to help; you could've closed the question by accepting my answer (which is perfectly legit also for dupes); instead your kind of behaviour of posting an answer to your own question that is essentially the same as a previous answer, is generally not looked favourably upon here at SO. – Maurits Evers Mar 30 '18 at 08:07
  • 1) I didn't know that about tidyverse :-/. 2) If it helps, I'm the one who upvoted your response as helpful. 3) My solution is different as I think it uses a different methodology and reorganises the data at the time of creating the plot rather than, as you did, entirely before creating the plot. Also I got it from a different SO question rather than your code. 4) I hadn't accepted your solution yet was that my plot was (marginally) tidier than yours. I've just edited your code slightly to achieve the tidied effect - but you may want to change the edit if you're not happy with how I did that. – beepingbopping Mar 30 '18 at 09:15
  • 1
    I apologise for my earlier somewhat harsh comment. In the end what matters is that you've found a solution that works. Synthesising information from all the other SO posts can be challenging; SO is a great resource for learning about alternative computational/coding approaches in R, the diversity of which is something that continues to amaze me. Anyway, good luck with your work, and feel free to edit my post to tidy up the code. – Maurits Evers Mar 30 '18 at 11:42