Reputation: 137
df <- data.frame(Country = c("Indonesia","Indonesia","Brazil","Colombia","Mexico","Colombia","Costa Rica" ,"Mexico","Brazil","Costa Rica"),
Subject = c("Boys", "Girls","Boys","Boys","Boys","Girls","Boys","Girls","Girls","Girls"),
Value = c(358.000,383.000,400.000,407.000,415.000,417.000,419.000,426.000,426.000,434.000))
I'm trying to make a plot of Country vs Value, but ordering the points by the Value ascending for the Boys rows only. I know I can use something like:
df %>%
ggplot(aes(reorder(Country, Value), Value)) +
geom_point()
This does not take into account the Boys only rows in the subject column. How do I go about doing this?
Edit: The ordering can be done outside ggplot as:
df <- df %>%
arrange(Value, Subject)
However, I just cannot yet replicate it in the ggplot reorder. Included is an example of the data in question.
Upvotes: 0
Views: 1891
Reputation: 13883
Arranging your data frame does not change the way the column Country
will be ordered on the x axis. The priority for the order on the axis for discrete variables is:
reorder
or final specification in aes()
, use that orderinglevels
of that factorAs far as I know, you can only specify one column to use in reorder()
, so the next step is to convert to a factor and specify the levels
. The way the items appear in the ordering of the data frame does not matter, since the columns are treated completely separate from the order in which they appear in the data frame. In fact, this is kind of the whole idea behind mapping.
Therefore, if you want this particular order, you'll have to convert the Country
column into a factor and specify levels
. You can do that separately, or pipe it all together using mutate()
. Just note that we have to specify to use unique()
values of the Country
column to ensure we only provide each level one time in the order in which they appear in the sorted data frame.
# color and size added for clarity on the sorting
df %>%
arrange(Subject, Value) %>%
mutate(Country=factor(Country, levels=unique(Country))) %>%
ggplot(aes(Country, Value, color=Subject)) + geom_point(size=3)
Upvotes: 2