R语言 如何在ggplot中使用x轴上的线和间距文本记号连接点?

hiz5n14c  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(186)

我有以下关于牛仔裤的数据,我希望它以最容易理解的方式绘制,重点是可用的尺寸。

jeans <-  tribble(~ category, ~ brand, ~size_value,
"Jeans","BE GANESHA","38",
"Jeans","BE GANESHA","40",
"Jeans","BE GANESHA","42",
"Jeans","BE GANESHA","44",
"Jeans","BE GANESHA","34",
"Jeans","BE GANESHA","36",
"Jeans","Bendita Be","M",
"Jeans","Bendita Be","XL",
"Jeans","By Florencia","0",
"Jeans","By Florencia","1",
"Jeans","By Florencia","2",
"Jeans","By Florencia","3",
"Jeans","CLARA AGUAYO","U",
"Jeans","Caro Criado","L",
"Jeans","Caro Criado","M",
"Jeans","Caro Criado","XS",
"Jeans","Caro Criado","S",
"Jeans","Caro Criado","XL",
"Jeans","Joséphine","32",
"Jeans","Joséphine","34",
"Jeans","Joséphine","36",
"Jeans","Joséphine","38",
"Jeans","Joséphine","40",
"Jeans","LAS MARCAS","L",
"Jeans","LAS MARCAS","M",
"Jeans","LAS MARCAS","S",
"Jeans","LAS MARCAS","XL",
"Jeans","LAS MARCAS","XS",
"Jeans","LAS MARCAS","XXL",
"Jeans","LIMITE","1",
"Jeans","LIMITE","2",
"Jeans","LIMITE","3",
"Jeans","LIMITE","4",
"Jeans","LIMITE","5",
"Jeans","LIMITE","6",
"Jeans","LIMITE","0",
"Jeans","MARIMBA MULTIMARCAS","31",
"Jeans","MUTMA","26",
"Jeans","MUTMA","27",
"Jeans","MUTMA","28",
"Jeans","MUTMA","29",
"Jeans","MUTMA","30",
"Jeans","MUTMA","31",
"Jeans","Margara Shaw","L",
"Jeans","Margara Shaw","M",
"Jeans","Margara Shaw","S",
"Jeans","Margara Shaw","XL",
"Jeans","Margara Shaw","XS",
"Jeans","Margara Shaw","XXL",
"Jeans","Margara Shaw","4XL")

jeans_pants %>% 
    mutate(size_value = case_when(
              size_value == "S/M" ~ "S/M ", 
              size_value == "M/L" ~ "M/L ", 
              size_value == "L/XL" ~ "L/XL ",
              size_value == "S/M" ~ "S/M ", 
              size_value == "M/L" ~ "M/L ", 
              size_value == "L/XL" ~ "L/XL ",
              size_value == "XL" ~ "XL    ",
              size_value == "XXL" ~ "XXL    ",
              size_value == "XXXL" ~ "XXXL    ",
              .default = as.character(size_value)),
           size_value = factor(size_value, levels = c("U", "0", "1", "2", "3", "4", "5", 
                                                      "6",
                                                      "S/M ", "M/L ", "L/XL ",
                                                      "XXS", "XS", "S", "M", "L", "XL   ", 
                                                      "XXL    ", "XXXL    ", "4XL",
                                                      "22", "23", "24", "25", "26", "27", 
                                                      "28", "29", "30", "31", "32", "33", 
                                                      "34", "36", "38", "40", "42", 
                                                      "44"))) %>% 
    arrange(brand, desc(size_value)) %>% 
    ggplot(aes(size_value, brand)) +
    geom_point(aes(color = size_value), alpha = .6, size = 3) +
    scale_color_manual(values = rep(c("#8E6151", "#988880"), 38)) + 
    theme(legend.position = "none",
          panel.border = element_blank(),
          panel.grid.minor.x = element_blank(),
          axis.text.x = element_text(margin = margin(r = 22))))

首先,我想在相同比例的点之间画一条线(我将展示一张所需服装的图片)。然后,我尝试手动在轴XL,XXL和XXXL中进行空间设置,但我做不到。

我真的很感激帮助和任何进一步的建议可视化。
在这里我留下了一个品牌的例子,有多种尺寸的规模:

data <- tribble(~brand,~size_value,
"BE GANESHA","38",
"BE GANESHA","40",
"BE GANESHA","42",
"BE GANESHA","44",
"BE GANESHA","34",
"BE GANESHA","36",
"Margo Baridon","24",
"Margo Baridon","26",
"Margo Baridon","28",
"Margo Baridon","30",
"Margo Baridon","32",
"Margo Baridon","34",
"BE GANESHA","M",
"BE GANESHA","L",
"BE GANESHA","S",
"BE GANESHA","XL",
"BE GANESHA","XS",
"BE GANESHA","XXL",
"Margo Baridon","L",
"Margo Baridon","M",
"Margo Baridon","S",
"Margo Baridon","XS",
"Margo Baridon","24",
"Margo Baridon","26",
"Margo Baridon","28",
"Margo Baridon","30",
"Margo Baridon","32",
"Margo Baridon","34",
"Margo Baridon","XL")
ldxq2e6h

ldxq2e6h1#

我认为ggplot2不支持离散轴上的任意间距(我很想找到这样的解决方案!在此期间,让我们转移到连续的x轴,基于期望的宽度预定义对应的x位置。(这样就不需要添加空格。)

sizes <- tibble(size_value = c("U", "0", "1", "2", "3", "4", "5", "6",
                               "S/M", "M/L", "L/XL",
                               "XXS", "XS", "S", "M", "L", "XL", 
                               "XXL", "XXXL", "4XL",
                               "22", "23", "24", "25", "26", "27", 
                               "28", "29", "30", "31", "32", "33", 
                               "34", "36", "38", "40", "42", "44"),
                x = lag(cumsum(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 5L, 3L, 2L, 1L, 1L, 
                             1L, 6L, 7L, 8L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                             2L, 2L, 2L, 2L, 2L, 2L, 2L)), default=0)
)

(The顺序和宽度是从前面的代码中推断出来的。)
我们会把这个left_join放到你的jeans上。请注意,我并没有防范任何在sizes$size_value中找不到的jeans$size_value;如果发生这种情况,x=将被指定为NA,并且可能不会绘制。
由于您希望在按brand分组的点之间添加线,因此我也将添加线(在点下面),尽管这是“仅仅因为”。我还添加了theme_bw()来简化主题,更类似于你在问题中的演示。

jeans %>%
  left_join(sizes, by = "size_value") %>%
  mutate(size_value = factor(size_value, levels = sizes$size_value)) %>%
  arrange(brand, desc(size_value)) %>%
  ggplot(aes(x, brand)) +
  geom_line(aes(group = brand)) +
  geom_point(aes(color = size_value), alpha = .6, size = 3) +
  scale_color_manual(values = rep(c("#8E6151", "#988880"), nrow(sizes))) +
  scale_x_continuous(breaks = sizes$x, labels = sizes$size_value) +
  theme_bw() +
  theme(legend.position = "none",
        panel.border = element_blank(),
        panel.grid.minor.x = element_blank(),
        axis.text.x = element_text(margin = margin(r = 22)))

相关问题