You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
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)
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"
}
}
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:
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)
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)
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.
The text was updated successfully, but these errors were encountered: