简体   繁体   English

气泡图中分类 x 轴和 y 轴的顺序颠倒了吗? 如何反推?

[英]Order of categorical x and y axis in bubble chart reversed? How to unreverse?

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:我的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:我希望 y 轴和 x 轴的顺序如下:

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!谢谢!

Again, I don't understand what this question is all about.同样,我不明白这个问题的全部内容。 I like the plot, it's simple to follow.我喜欢 plot,它很容易遵循。 In my opinion, the ggplot's x and y variables are incorrectly defined.在我看来, ggplot's xy变量定义不正确。 Also added scale_y_discrete(limits = rev) to change the order top-to-bottom.还添加了scale_y_discrete(limits = rev)以更改从上到下的顺序。

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: Plot:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM