Brock Blahous
Brock Blahous

Reputation: 21

How to reverse Y axis values (GGPLOT2 R)

I'm trying to reverse the values for my Y-axis. I basically want the Y-axis to decrease as you go up so instead of there being 24, it should be 16. How can I do this?

ggplot(data=data_diff, aes(Sum.of.Diff, Sum.of.Against.Total) + 
geom_point(alpha=1) + 
ggtitle("Data Diff") + 
geom_label_repel(data=subset(data_diff, Sum.of.Against.Total> 0 | Sum.of.Diff > 0 | Sum.of.Diff < 0), aes(label=Name), box.padding = 0.5, point.padding = .11, segment.color = 'black') + 
xlab("Sum of Diff") + ylab("Sum of Against Stats Total")

example plot:

Click Here for example plot

Here is my data set:

                    Name Sum.of.Against.Total Sum.of.Diff
1                Aurorus                26.00      185.66
2               Parasect                25.00      155.66
3               Leavanny                25.00      115.66
4              Abomasnow                25.00      115.66
5               Frosmoth                24.50      115.66
6              Rhyperior                24.25      115.66
7                  Golem                24.25      115.66
8              Exeggutor                24.00      115.66
9                Weavile                23.50      115.66
10               Flapple                23.25      115.66
11              Appletun                23.25      115.66
12      Alolan Exeggutor                23.25       82.66
13             Tyranitar                23.00       70.66

Thank you! B

Upvotes: 1

Views: 4343

Answers (1)

neuron
neuron

Reputation: 2079

If you want to reverse the order of the y axis you can use scale_y_reverse() or you could use scale_y_continuous(trans = "reverse") both will produce the desired output

data:

data_diff <- structure(list(Name = structure(c(4L, 10L, 9L, 1L, 7L, 11L, 8L, 
5L, 13L, 6L, 3L, 2L, 12L), .Label = c("Abomasnow", "Alolan_Exeggutor", 
"Appletun", "Aurorus", "Exeggutor", "Flapple", "Frosmoth", "Golem", 
"Leavanny", "Parasect", "Rhyperior", "Tyranitar", "Weavile"), class = "factor"), 
    Sum.of.Against.Total = c(26, 25, 25, 25, 24.5, 24.25, 24.25, 
    24, 23.5, 23.25, 23.25, 23.25, 23), Sum.of.Diff = c(185.66, 
    155.66, 115.66, 115.66, 115.66, 115.66, 115.66, 115.66, 115.66, 
    115.66, 115.66, 82.66, 70.66)), class = "data.frame", row.names = c(NA, 
-13L))

Plot:

library(ggplot2)
library(ggrepel)
ggplot(data=data_diff, aes(Sum.of.Diff, Sum.of.Against.Total)) + 
         geom_point(alpha=1) +
         scale_y_reverse() +
         ggtitle("Data Diff") +
         geom_label_repel(data=subset(data_diff, Sum.of.Against.Total> 0 | Sum.of.Diff > 0 | Sum.of.Diff < 0), aes(label=Name), box.padding = 0.5, point.padding = .11, segment.color = 'black') + 
         xlab("Sum of Diff") + 
         ylab("Sum of Against Stats Total")

example1

ggplot(data=data_diff, aes(Sum.of.Diff, Sum.of.Against.Total)) + 
         geom_point(alpha=1) +
         scale_y_continuous(trans = "reverse") +
         ggtitle("Data Diff") +
         geom_label_repel(data=subset(data_diff, Sum.of.Against.Total> 0 | Sum.of.Diff > 0 | Sum.of.Diff < 0), aes(label=Name), box.padding = 0.5, point.padding = .11, segment.color = 'black') + 
         xlab("Sum of Diff") + 
         ylab("Sum of Against Stats Total")

example2

Upvotes: 6

Related Questions