Home > database >  Reversing discrete axis in ggplot doesn't reverse data
Reversing discrete axis in ggplot doesn't reverse data

Time:03-21

So I have the following example table named TEST_df (true table is much larger, 40 rows and columns)

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  

I am attempting to make a graph with ggplot, I have converted the name and CellType columns to factors, and then make a ggplot using the following

TEST_df$name <- factor(TEST_df$name, levels=unique(TEST_df$name))
TEST_df$CellType <- factor(TEST_df$CellType, levels=unique(TEST_df$CellType))
levels(TEST_df$CellType)
[1] "A" "B" "C"
levels(TEST_df$name)
[1] "D" "E" "F"

ggplot(data = TEST_df, aes(x=name, y=CellType)) 
  geom_count(size = TEST_df$size, fill=TEST_df$colour,
             colour="black",pch=21)

enter image description here

This works fine but I want to reverse the y axis, and have the entries going downwards, A, B, C, etc. Looking at other advice on this website I tried using scale_y_discrete(limits = rev), which did indeed reverse the y axis labels, but then the points on the plot stopped matching their values. The A-D point should be large and red and instead the large red point stays in position despite the axes changing. The two orange point also change position for some reason.

ggplot(data = TEST_df, aes(x=name, y=CellType)) 
  geom_count(size = TEST_df$size, fill=TEST_df$colour,
             colour="black",pch=21) 
  scale_y_discrete(limits = rev)

enter image description here

So what I am looking for is a solution to how to reverse the discrete Y axis, while also reversing the points in a sensible way

CodePudding user response:

  • 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

enter image description here

  • Related