Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: controlling order of plotting different factors #22

Open
SamPaplauskas opened this issue Apr 10, 2019 · 2 comments
Open

Question: controlling order of plotting different factors #22

SamPaplauskas opened this issue Apr 10, 2019 · 2 comments
Labels
question Further information is requested

Comments

@SamPaplauskas
Copy link

SamPaplauskas commented Apr 10, 2019

Issue description

My points are plotted sequentially so that different layers exist.

My plotted points belonging to factor B (in the legend) overlap plotted points belonging to factor A (see Fig)

image

I want to change this so that the factor A is on the top layer.

What I have tried

I might have read that the 'order' aes was decommissioned a couple of years previous

arrange() so that the data are plotted by factors in the legend in a different order, this did not work because plotting occurs alphabetically through A and B regardless of their position in the dataset.

My working - but imperfect - soln is to rename A and B in the data to B and A respectively, then rename the legend after plotting. However, this results in a legend in non-alphabetical order (see Fig)

image

N.B. For the example dataset, there are two points 22.8 (one is A, the other is B) in Group two. I do not know why their ordering does not conform to the rest of the data...

Example data

Get the data to plot correctly

convert the numbers into discrete variables

mtcars.new <- mtcars

for (i in 1:length(mtcars$vs)){
if (mtcars$vs[i] == 0){
mtcars.new$vs[i] = "Group one"
} else {
mtcars.new$vs[i] = "Group two"
}
}

for (i in 1:length(mtcars$am)){
if (mtcars$am[i] == 0){
mtcars.new$am[i] = "A"
} else {
mtcars.new$am[i] = "B"
}
}

Plot the data

ggplot(mtcars.new, aes (y = mpg, x = vs, fill = am)) +
geom_dotplot(binaxis = "y",
stackdir = "center", colour = "transparent") +
ylab(expression(paste("mpg"), cex=1.5)) +
labs(fill = "Legend") +
theme(
panel.grid = element_blank(),
panel.background = element_rect(fill = NA),
axis.line = element_line(colour = "black"),
legend.key=element_rect(fill=NA))

Desired outcome

My plotted points belonging to factor A overlapping plotted points belonging to factor B, and a legend in alphabetical order.

@SamPaplauskas SamPaplauskas added the question Further information is requested label Apr 10, 2019
@adamaki
Copy link
Member

adamaki commented Apr 10, 2019

R will always plot in alphabetical or numerical order, so you need to change your groups to factors and then change the factor levels so they plot in the order you want like so:

mtcars.new$am <- factor(mtcars.new$am, levels = c('B', 'A'))

Then you can reverse the order of the legend by adding this line to the end of your ggplot:

guides(fill = guide_legend(reverse = T))

@SamPaplauskas
Copy link
Author

That works spot on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants