Hi Ed,
The answer you received on Stack Overflow is basically the way I would
do it. Here is my variant:
mydata$Instance <- with(mydata, {
factor(Instance,
levels=levels(reorder(Instance[System=="a"],
Value[System=="a"])))
})
qplot(data=mydata, weight=Value, Instance, fill=System, position="dodge")
This may not be the "most elegant" way, but in my experience R is
often a bit inelegant. You can always fake elegance by hiding the
complexity, e.g.
reorder2 <- function(data, x, y, sub.var, sub.value) {
factor(data[[x]],
levels = levels(
reorder(
data[data[[sub.var]] == sub.value, x],
data[data[[sub.var]] == sub.value, y])))
}
qplot(data=mydata,
weight=Value,
reorder2(mydata,
"Instance",
"Value",
"System",
"a"),
fill=System,
position="dodge")
Best,
Ista
On Fri, Nov 2, 2012 at 11:49 AM, Edward Schwartz <
edm...@gmail.com> wrote:
> Hi folks,
>
> I've been using ggplot2 for a while now to make beautiful plots for papers,
> and also to visualize experiment results. It's really great!
>
> Recently, I ran into a situation where it seemed difficult to do something
> rather straightforward in the ggplot2/reshape way. I asked my question on
> stackoverflow, and got a response, but I still feel like there should be a
> much more elegant way of handling this.
>
> It's more easy to phrase the question in the form of an example, so I'll
> point to my original stackoverflow question:
>
> Here's a link to the original question:
>
http://stackoverflow.com/questions/13127195/re-ordering-by-multi-dimensional-data-for-ggplot2-plotting
>
> I'll paste it below, too, for convenience. I'd appreciate any pointers. It
> just feels like doing something so simple should not be so difficult!
>
> Thank you for any help or insights.
>
> Ed
>
> === Begin paste ===
>
> I'm having some trouble producing what I think should be a fairly
> straightforward ggplot2 graph.
>
> I have some experimental data in a data frame. Each data entry is identified
> by the system that was being measured, and the instance (problem) it was run
> on. Each entry also has a value measured for the particular system and
> instance.
>
> For instance:
>
> mydata <- data.frame(System=c("a","b","a","b","a","b"),
> Instance=factor(c(1,1,2,2,3,3)), Value=c(10,5,4,2,7,8))
>
> Now, I'd like to plot this data in a boxplot where the x-axis contains the
> instance identifier, and the color of the bar indicates which system the
> value is for. The bar heights should be weighted by the value in the
> dataframe.
>
> This almost does what I want:
>
> qplot(data=mydata, weight=Value, Instance, fill=System, position="dodge")
>
> The final thing that I would like to do is reorder the bars so they are
> sorted by the value of system A. However, I can't figure out an elegant way
> to do this.
>
> My first instinct was to use qplot(data=mydata, weight=Value,
> reorder(Instance, Value), fill=System, position="dodge"), but this will
> order by the mean value for each instance, and I just want to use the value
> from A. I could use qplot(data=mydata, weight=Value, reorder(Instance,
> Value, function(x) { x[1] } ), fill=System, position="dodge") to order the
> instances by "the first value", but this is dangerous (what if the order
> changes?) and unclear to a reader.
>
> What is a more elegant solution?
>
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
>
https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email
ggp...@googlegroups.com
> To unsubscribe: email
ggplot2+u...@googlegroups.com
> More options:
http://groups.google.com/group/ggplot2