how to change categorical variables position (y axis), ggplot2

Hello, I am using this to create my graph but I need that the HN stays over the HS, and Iam not getting it. And let the color be red instead of blue.

library(tidyverse, quietly = TRUE)
binary_data <- data.frame(stringsAsFactors=FALSE,
                          Species = c("Lutajnus synagris", "Lutjanus analis", "Lutjanus apodus",
                                      "Lutjanus cyanopterus", "Lutjanus griseus", "Lutjanus jocu",
                                      "Ocyurus chysurus", "Lutjanus analis", "Lutajnus jocu", "Lutjanus gibbus",
                                      "Lutjanus fulvus", "Symphorichthys spilurus", "Lutjanus bohar"),
                          hemisphere = c("HN", "HN", "HN", "HN", "HN", "HN", "HN", "HS", "HS", "HS",
                                         "HS", "HS", "HS"),
                          spring = c(1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1),
                          summer = c(1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1),
                          fall = c(0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1),
                          winter = c(0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1)
                          )
binary_data %>% 
    gather(Season, Presence, spring:winter) %>% 
    group_by(hemisphere, Season) %>% 
    summarise(species_count = sum(Presence)) %>% 
    ggplot(aes(x = Season, y = hemisphere)) +
    geom_point(aes(size = species_count, color = species_count)) +
    scale_size_continuous(range = c(3,18)) +
    guides(colour = guide_legend()) +
    labs(color = 'Number of Species',
         size = 'Number of Species') +
    theme_minimal()

You could make hemisphere a factor and specify the levels before you do the plot:

binary_data$hemisphere <- factor(binary_data$hemisphere, levels=c("HS","HN"))

and add +scale_colour_gradient(low="black",high="red") (with whatever colours you want) to your ggplot call to adjust the colour.

1 Like

where I should put:

binary_data$hemisphere <- factor(binary_data$hemisphere, levels=c("HS","HN"))

every time I run it gives error, I think I'm inserting in the wrong place.

I did it straight after creating the data.frame.

1 Like

Thanks!!! I got it :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.