如何使用线连接数据点,并在 ggplot 中的 x 轴上添加间隔文本刻度?

huangapple go评论67阅读模式
英文:

How to join points using lines and spacing text ticks in the x axis in ggplot?

问题

我有以下关于牛仔裤的数据,我想以最易理解的方式绘制它,重点关注可用的尺码。

jeans <-  tribble(~ category, ~ brand, ~size_value,
"Jeans","BE GANESHA","38",
...(以下省略)

首先,我想在相同尺码的点之间绘制一条线(我将在上面展示所需外观的图片)。然后,我尝试手动调整 XL、XXL 和 XXXL 的轴间距,但无法成功。

如何使用线连接数据点,并在 ggplot 中的 x 轴上添加间隔文本刻度?

我会非常感谢帮助,并欢迎有关可视化的任何进一步建议。

这里我留下了一个具有多个尺码比例的品牌的示例:

data <- tribble(~brand,~size_value,
"BE GANESHA","38",
...(以下省略)
英文:

I have the following data on jeans and I want it to plot it in the most understandable way focusing on the sizes available.

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))))

In the first place, I want to draw a line between the points of the same scale (I'm going to show a picture above of the desired outfit). And then, I tried manually to space in the axis XL, XXL, and XXXL but I can't.

如何使用线连接数据点,并在 ggplot 中的 x 轴上添加间隔文本刻度?

I would really appreciate the help and any further suggestions on visualisation.

Here I left an example of a brand with multiple sizes' scales:

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")

答案1

得分: 2

我相信 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)
)

(顺序和宽度是根据您先前的代码推断出来的。)

我们将这个表格使用 left_join 连接到你的 jeans 数据集上。请注意,我没有防范任何 jeans$size_valuesizes$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)))

如何使用线连接数据点,并在 ggplot 中的 x 轴上添加间隔文本刻度?

英文:

I believe that ggplot2 does not support arbitrary spacing on a discrete axis (I'm curious to find solutions that do!). In the interim, let's shift to a continuous x-axis, predefining the corresponding x locations based on the desired widths. (This precludes the need to add spaces.)

sizes &lt;- tibble(size_value = c(&quot;U&quot;, &quot;0&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;,
                               &quot;S/M&quot;, &quot;M/L&quot;, &quot;L/XL&quot;,
                               &quot;XXS&quot;, &quot;XS&quot;, &quot;S&quot;, &quot;M&quot;, &quot;L&quot;, &quot;XL&quot;, 
                               &quot;XXL&quot;, &quot;XXXL&quot;, &quot;4XL&quot;,
                               &quot;22&quot;, &quot;23&quot;, &quot;24&quot;, &quot;25&quot;, &quot;26&quot;, &quot;27&quot;, 
                               &quot;28&quot;, &quot;29&quot;, &quot;30&quot;, &quot;31&quot;, &quot;32&quot;, &quot;33&quot;, 
                               &quot;34&quot;, &quot;36&quot;, &quot;38&quot;, &quot;40&quot;, &quot;42&quot;, &quot;44&quot;),
                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 order and widths were inferred from your previous code.)

We'll left_join this onto your jeans. Note that I am not guarding against any jeans$size_value not found in sizes$size_value; if that occurs, x= will be assigned NA and likely not plotted.

Since you wanted lines among points grouped by brand, I'll add that too (under the dots), though that's "just because". I also added theme_bw() to simplify the theme, more akin to what you had as a demo in the question.

jeans %&gt;%
  left_join(sizes, by = &quot;size_value&quot;) %&gt;%
  mutate(size_value = factor(size_value, levels = sizes$size_value)) %&gt;%
  arrange(brand, desc(size_value)) %&gt;%
  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(&quot;#8E6151&quot;, &quot;#988880&quot;), nrow(sizes))) +
  scale_x_continuous(breaks = sizes$x, labels = sizes$size_value) +
  theme_bw() +
  theme(legend.position = &quot;none&quot;,
        panel.border = element_blank(),
        panel.grid.minor.x = element_blank(),
        axis.text.x = element_text(margin = margin(r = 22)))

如何使用线连接数据点,并在 ggplot 中的 x 轴上添加间隔文本刻度?

huangapple
  • 本文由 发表于 2023年5月30日 01:57:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定