Home > Enterprise >  Order of categorical x and y axis in bubble chart reversed? How to unreverse?
Order of categorical x and y axis in bubble chart reversed? How to unreverse?

Time:03-14

This is the head of my data:

structure(list(asutus = c("Eesti Draamateater", "Eesti Draamateater", 
"Eesti Draamateater", "Eesti Noorsooteater", "Eesti Noorsooteater", 
"Eesti Noorsooteater", "Rahvusooper Estonia", "Rahvusooper Estonia", 
"Rakvere Teatrimaja", "Rakvere Teatrimaja", "Rakvere Teatrimaja", 
"Rakvere Teatrimaja", "Sakala Teatrimaja", "Vene Teater", "Vene Teater", 
"Vene Teater", "Vene Teater"), hoone_liik = structure(c(2L, 1L, 
3L, 2L, 4L, 1L, 2L, 1L, 2L, 4L, 1L, 3L, 1L, 2L, 4L, 1L, 3L), .Label = c("teatrihoone", 
"ladu", "töökojad", "muu"), class = "factor"), hoone_suletud_netopind = c(2441.1, 
6004, 705.8, 672, 970.8, 3509, 2337.6, 12426.7, 973, 3307.4, 
4478, 446.6, 3220, 394.8, 281.5, 5873.9, 373.7)), row.names = c(NA, 
-17L), groups = structure(list(asutus = c("Eesti Draamateater", 
"Eesti Noorsooteater", "Rahvusooper Estonia", "Rakvere Teatrimaja", 
"Sakala Teatrimaja", "Vene Teater"), .rows = structure(list(1:3, 
    4:6, 7:8, 9:12, 13L, 14:17), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, 6L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

My colors:

prx_col_palette <- function(){
  c("#E69F00",
    "#56B4E9",
    "#009E73",
    "#F0E442",
    "#0072B2",
    "#D55E00",
    "#CC79A7")
}
prx_cols <- prx_col_palette()

My bubble chart:

ggplot(pindalad_joonisele,
       aes(x = str_to_title(hoone_liik), 
           y = str_to_title(reorder(asutus, desc(asutus))),
           colour = str_to_title(hoone_liik),
           size = hoone_suletud_netopind))  
  geom_point()  
  geom_text(aes(label = hoone_suletud_netopind), 
            colour = "white", 
            size = 3.5)  
  scale_x_discrete(position = "top") 
  scale_size_continuous(range = c(13, 35))   # Adjust as required.
  scale_colour_manual(values = prx_cols) 
  labs(x = NULL, y = NULL)  
  theme(legend.position = "none",
        panel.background = element_blank(),
        panel.grid = element_blank(),
        axis.text.x = element_text(size=11),
        axis.text.y = element_text(size=11),
        axis.ticks = element_blank())

I would like that the order of y and x axis would be as follows:

pindalad_joonisele$asutus <- factor(pindalad_joonisele$asutus, levels = c("Eesti Draamateater", "Eesti Noorsooteater",
                                                                          "Rahvusooper Estonia", "Rakvere Teatrimaja", "Sakala Teatrimaja",
                                                                          "Vene Teater"))

pindalad_joonisele$hoone_liik <- factor(pindalad_joonisele$hoone_liik, levels = c("teatrihoone", "ladu", "töökojad", "muu"))

However, nothing seems to work and they are in a reversed or random order. Can somebody please help me with this? Thank you!

CodePudding user response:

Again, I don't understand what this question is all about. I like the plot, it's simple to follow. In my opinion, the ggplot's x and y variables are incorrectly defined. Also added scale_y_discrete(limits = rev) to change the order top-to-bottom.

Sample code:

pindalad_joonisele$asutus <- factor(pindalad_joonisele$asutus, levels = c("Eesti Draamateater", "Eesti Noorsooteater",
                                                                          "Rahvusooper Estonia", "Rakvere Teatrimaja", "Sakala Teatrimaja",
                                                                          "Vene Teater"))

pindalad_joonisele$hoone_liik <- factor(pindalad_joonisele$hoone_liik, levels = c("teatrihoone", "ladu", "töökojad", "muu"))


ggplot(pindalad_joonisele,
       aes(x = hoone_liik, 
           y = asutus,
           colour = hoone_liik,
           size = hoone_suletud_netopind))  
  geom_point()  
  geom_text(aes(label = hoone_suletud_netopind), 
            colour = "white", 
            size = 3.5)  
  scale_x_discrete(position = "top") 
  scale_y_discrete(limits = rev) # reverse y axis
  scale_size_continuous(range = c(13, 35))   # Adjust as required.
  scale_colour_manual(values = prx_cols) 
  labs(x = NULL, y = NULL)  
  theme(legend.position = "none",
        panel.background = element_blank(),
        panel.grid = element_blank(),
        axis.text.x = element_text(size=11),
        axis.text.y = element_text(size=11),
        axis.ticks = element_blank())

Plot:

enter image description here

  • Related