在图表中排序列

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

Order columns in plot

问题

我想用geom_col()创建一个堆积图,使用以下数据集。

tmp.df <- data.frame(
  original = c(2.68, 1.05, 72.89, 11.81, 11.67, 2.85, 1.11, 72.74, 12.05, 11.95),
  sensitivity = c('Si', 'Si', 'Si', 'Si', 'Si', 'STi', 'STi', 'STi', 'STi', 'STi'),
  parameters = c('OMd', 'ED6_N', 'CP', 'dr_N', 'GE', 'OMd', 'ED6_N', 'CP', 'dr_N', 'GE')
)

因此,我像下面这样做了:

ord.eng <- c("CP", "GE", "OMd", "ED6_N", "dr_N")

library(ggplot2)
ggplot(tmp.df, aes(x = sensitivity, y = original)) +
    geom_col(aes(fill = parameters), width = 0.5, color = "black") +
    ggtitle("Nu/Total N excretion") +
    xlab("Sobol index") +
    ylab("Sobol indices (%)") +
    theme_classic() +
    theme(
        plot.title = element_text(face = "bold", hjust = 0.5, size = 18),
        axis.text.x = element_text(color = "black", size = 18),
        axis.text.y = element_text(color = "black", size = 18),
        axis.title = element_text(face = "bold", size = 18, color = "black"),
        legend.text = element_text(size = 15),
        legend.title = element_text(hjust = 0.5, size = 15)
    ) +
    scale_y_continuous(breaks = seq(0, 140, 20)) +
    scale_fill_manual(
        name = "Variables",
        limits = ord.eng,
        values = c(
            "CP" = "White",
            "GE" = "ivory",
            "OMd" = "ivory3",
            "ED6_N" = "gray",
            "dr_N" = "black"
        )
    )

然而,柱状图没有按照'ord.eng'的顺序堆叠。它只改变了图例的顺序。但我希望它与图例的顺序相同。您有什么好主意吗?

谢谢你的帮助。

结果柱状图

英文:

I'd like to make a stocked graph with geom_col() with below dataset.

tmp.df &lt;- data.frame(
  original = c(2.68, 1.05, 72.89, 11.81, 11.67, 2.85, 1.11, 72.74, 12.05, 11.95),
  sensitivity = c(&#39;Si&#39;, &#39;Si&#39;, &#39;Si&#39;, &#39;Si&#39;, &#39;Si&#39;, &#39;STi&#39;, &#39;STi&#39;, &#39;STi&#39;, &#39;STi&#39;, &#39;STi&#39;),
  parameters = c(&#39;OMd&#39;, &#39;ED6_N&#39;, &#39;CP&#39;, &#39;dr_N&#39;, &#39;GE&#39;, &#39;OMd&#39;, &#39;ED6_N&#39;, &#39;CP&#39;, &#39;dr_N&#39;, &#39;GE&#39;)
)

Thus I did like below:

ord.eng &lt;- c(&quot;CP&quot;, &quot;GE&quot;, &quot;OMd&quot;, &quot;ED6_N&quot;, &quot;dr_N&quot;)

library(ggplot2)
ggplot(tmp.df, aes(x = sensitivity, y = original))+
    geom_col(aes(fill = parameters), width = 0.5, color = &quot;black&quot;)+
    ggtitle(&quot;Nu/Total N excretion&quot;) +
    xlab(&quot;Sobol index&quot;)+
    ylab(&quot;Sobol indices (%)&quot;)+
    theme_classic()+
    theme(plot.title = element_text(face = &quot;bold&quot;, hjust = 0.5, size = 18),
          axis.text.x = element_text(color = &quot;black&quot;, size = 18),
          axis.text.y = element_text(color = &quot;black&quot;, size = 18),
          axis.title = element_text(face = &quot;bold&quot;, size = 18, color = &quot;black&quot;),
          legend.text = element_text(size = 15),
          legend.title = element_text(hjust = 0.5, size = 15))+
    scale_y_continuous(breaks = seq(0, 140, 20))+
        scale_fill_manual(name = &quot;Variables&quot;, limits = ord.eng,
                      values = c(&quot;CP&quot; = &quot;White&quot;, &quot;GE&quot; = &quot;ivory&quot;, &quot;OMd&quot; = &quot;ivory3&quot;, &quot;ED6_N&quot; = &quot;gray&quot;, &quot;dr_N&quot; = &quot;black&quot;))

However, the bar chart didn't stock 'ord.eng' order.
It changed just legend order.
But I'd like to stock it same order with legend.

Do you have any good ideas?

Thank you for your help.

Result bar plot

答案1

得分: 0

将要排序的列转换为因子是一种做法。levels参数告诉R变量可以具有不同的值,而ggplot在确定如何堆叠时使用levels的顺序:

tmp.df %>%
    mutate(parameters = factor(parameters, levels = ord.eng)) %>%
    ggplot(aes(x = sensitivity, y = original, fill = parameters)) +
    geom_col(aes(fill = parameters), width = 0.5, color = "black") +
    ggtitle(sprintf("Nu/Total N excretion")) +
    xlab("Sobol index") +
    ylab("Sobol indices (%)") +
    theme_classic() +
    theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 18),
          axis.text.x = element_text(color = "black", size = 18),
          axis.text.y = element_text(color = "black", size = 18),
          axis.title = element_text(face = "bold", size = 18, color = "black"),
          legend.text = element_text(size = 15),
          legend.title = element_text(hjust = 0.5, size = 15)) +
    scale_y_continuous(breaks = seq(0, 140, 20)) +
    scale_fill_manual(name = "Variables", limits = ord.eng,
                      values = c("CP" = "White", "GE" = "ivory", "OMd" = "ivory3", "ED6_N" = "gray", "dr_N" = "black"))

在图表中排序列

英文:

One way of doing this is to turn the column you want to order by into a factor. The levels argument tells R the different values the variable can have, and ggplot uses the order of levels when deciding how to stack:

tmp.df %&gt;%
    mutate(parameters = factor(parameters, levels = ord.eng)) %&gt;% ### changed line ###
    ggplot(aes(x = sensitivity, y = original, fill = parameters)) +
    geom_col(aes(fill = parameters), width = 0.5, color = &quot;black&quot;)+
    ggtitle(sprintf(&quot;Nu/Total N excretion&quot;))+
    xlab(&quot;Sobol index&quot;)+
    ylab(&quot;Sobol indices (%)&quot;)+
    theme_classic()+
    theme(plot.title = element_text(face = &quot;bold&quot;, hjust = 0.5, size = 18),
          axis.text.x = element_text(color = &quot;black&quot;, size = 18),
          axis.text.y = element_text(color = &quot;black&quot;, size = 18),
          axis.title = element_text(face = &quot;bold&quot;, size = 18, color = &quot;black&quot;),
          legend.text = element_text(size = 15),
          legend.title = element_text(hjust = 0.5, size = 15))+
    scale_y_continuous(breaks = seq(0, 140, 20))+
        scale_fill_manual(name = &quot;Variables&quot;, limits = ord.eng,
                      values = c(&quot;CP&quot; = &quot;White&quot;, &quot;GE&quot; = &quot;ivory&quot;, &quot;OMd&quot; = &quot;ivory3&quot;, &quot;ED6_N&quot; = &quot;gray&quot;, &quot;dr_N&quot; = &quot;black&quot;))

在图表中排序列

huangapple
  • 本文由 发表于 2023年7月13日 22:11:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680347.html
匿名

发表评论

匿名网友

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

确定