Reverse Order of Discrete Y Axis in Ggplot2

Reverse order of discrete y axis in ggplot2

There is a new solution, scale_*_discrete(limits=rev), example:

tibble(x=1:26,y=letters) %>% 
ggplot(aes(x,y)) +
geom_point() +
scale_y_discrete(limits=rev)

Sample Image

ggplot2: Reversing the order of discrete categories on y-axis in scatterplot

You can reverse the order of the levels of your Subclass_name variable.

## Some sample data
art_data <- data.frame(Subclass_Name=sample(c("Architecture", "Painting", "Visual Arts"), 100, rep=T),
Year=sample(1920:2015, 100, rep=T, prob=sort(rexp(96, 1/8))),
Transactions=sample(1:7, 100, rep=T))

## Initial levels
levels(art_data$Subclass_Name)
# [1] "Architecture" "Painting" "Visual Arts"

## Reverse the order of Subclass_Name levels
art_data$Subclass_Name <- factor(art_data$Subclass_Name,
levels=rev(levels(art_data$Subclass_Name)))
levels(art_data$Subclass_Name)
# [1] "Visual Arts" "Painting" "Architecture"

## Then make the plot
ggplot(art_data, aes(Year, Subclass_Name, color=Subclass_Name, size=Transactions)) +
geom_point(alpha=0.7) +
scale_color_discrete(breaks=rev(levels(art_data$Subclass_Name)),
labels=rev(levels(art_data$Subclass_Name))) # reverse labels in legend

Sample Image

Reversing discrete axis in ggplot doesn't reverse data

  • Use geom_point instead of geom_count because you're getting the point sizes from a column in your table, not by counting the rows.
  • Use scale_fill_manual to use the colours that are specified in your table.
  • Use reorder when specifying the y variable in ggplot to reverse the order.
TEST_df = tribble(~CellType,~name,~value,~size,~colour,
'A','D',16,5,'red',
'A','E',8,3,'orange',
'A','F',5,3,'orange',
'B','D',2,2,'yellow',
'B','E',16,5,'red',
'B','F',1,0,'black',
'C','D',1,0,'black',
'C','E',0,0,'black',
'C','F',11,4,'pink',
)

col_mapping <- as.character(TEST_df$colour)
names(col_mapping) <- as.character(TEST_df$colour)

ggplot(data = TEST_df, aes(x = name, y = reorder(CellType, desc(CellType)), size = size, fill=colour)) +
geom_point(pch=21) +
scale_fill_manual(values = col_mapping) +
labs(y = "CellType") + # rename the y label - otherwise it'll be shown as "reorder(CellType..."
guides(fill = FALSE, size = FALSE) # remove the legends for fill and size

Sample Image

ggplot reverse axis order for factors

Try scale_y_discrete(limits = rev)

ggplot2:Reversing the order of a discrete character variable for each facet on a free scale?

You can transform the string vector to a factor and specify the order of levels:

The following command will create a factor. The levels are in decreasing alphabetical order:

msleep.noNA.red <- within(msleep.noNA.red,
name <- ordered(name, levels = rev(sort(unique(name)))))

Now you can plot the data:

pg <- ggplot(msleep.noNA.red, aes(value, name, colour = variable)) +
geom_point() +
facet_grid(vore ~ ., scale="free_y", space = "free_y")

Sample Image

Reversing the y axis on discrete data

Use:

ylim("20220427", "20220420", "20220413") 


Related Topics



Leave a reply



Submit