在 ggplot2 中重新排列 x 轴的类别只能按降序操作。

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

Reordering x categories in ggplot2 only works with descending order

问题

我使用字符变量作为x轴,数字变量作为y轴,字符变量作为填充创建了一个条形图。我想按填充变量对x轴的类别进行排序,但只能使用降序重新排序。

我已经进行了广泛的搜索,发现在任何地方都提到reorder(x, z)应该按照z的升序对x的类别进行排序,而reorder(x, -z)或reorder(x, desc(z))应该按照z的降序对x的类别进行排序。
在我的情况下,只有使用desc(z)才能按z的顺序对类别进行排序;reorder(x, -z)返回错误(! invalid argument to unary operator),而使用z只会按照x的顺序对它们进行排序。

以下代码几乎实现了我想要的效果:

df <- data.frame(x = c("loc9", "loc2", "loc3", "loc7", "loc5", "loc6", "loc4", "loc1", "loc8"),
                 y = c(1, 2, 5, 3, 5, 6, 7, 9, 5),
                 z = c("A", "A", "B", "B", "B", "B", "C", "C", "C"),
                 se = c(0.1, 0.14, 0.2, 1, 0.25, 0.3, 0.21, 0.23, 0.2),
                 n = c(2, 2, 3, 2, 1, 1, 3, 5, 4))

plot <- df %>%
  ggplot(aes(x = reorder(x, desc(z)), y = y, fill = z)) +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(reorder(x, desc(z)), ymin = y - se, ymax = y + se), width = 0.3, colour = "blue3", alpha = 0.9, size = 0.7) +
  geom_text(aes(x = reorder(x, desc(z)), y = 0.5, label = n)) +
  theme(legend.position = "bottom", plot.title = element_text(size = 12),
        text = element_text(size = 14),
        axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) +
  ggtitle("这里是图的标题") +
  labs(x = "", ylab = "y的标签", fill = NULL)

以下代码会产生36个警告(In mean.default(X[[i]], ...) : argument is not numeric or logical: returning NA),并生成如下图:

plot <- df %>%
  ggplot(aes(x = reorder(x, desc(z)), y = y, fill = z)) +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(reorder(x, desc(z)), ymin = y - se, ymax = y + se), width = 0.3, colour = "blue3", alpha = 0.9, size = 0.7) +
  geom_text(aes(x = reorder(x, desc(z)), y = 0.5, label = n)) +
  theme(legend.position = "bottom", plot.title = element_text(size = 12),
        text = element_text(size = 14),
        axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) +
  ggtitle("这里是图的标题") +
  labs(x = "", ylab = "y的标签", fill = NULL)
plot

这段代码会产生36个警告(In mean.default(X[[i]], ...) : argument is not numeric or logical: returning NA),并生成如下图:

英文:

I am making a bar plot with a character variable as x, a numeric variable as y and a character variable as fill. I want to order the x categories by the fill variable, but it only works with descending reorder.

I've been extensively searching around and I find everywhere that reorder(x, z) should order the x categories according to the ascending order of z, while reorder(x,-z) or reorder(x, desc(z)) should order the x categories according to the descending order of z.
In my case, I only get the categories ordered by z when using desc(z); reorder(x, -z) returns an error (! invalid argument to unary operator), and using z simply orders them by x.

This code gives almost what I want:

df&lt;- data.frame(x=c(&quot;loc9&quot;,&quot;loc2&quot;, &quot;loc3&quot;,&quot;loc7&quot;,&quot;loc5&quot;,&quot;loc6&quot;,&quot;loc4&quot;,&quot;loc1&quot;,&quot;loc8&quot;), y=c(1,2,5,3,5,6,7,9,5), z=c(&quot;A&quot;,&quot;A&quot;, &quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;C&quot;,&quot;C&quot;,&quot;C&quot;), se=c(0.1, 0.14, 0.2, 1,0.25,0.3,0.21,0.23,0.2), n=c(2,2,3,2,1,1,3,5,4))
plot&lt;- df %&gt;%
  ggplot(aes(x=reorder(x, desc(z)), y=y, fill=z))+
  geom_bar(stat=&quot;identity&quot;)+
  geom_errorbar(aes(reorder(x, desc(z)), ymin=y-se, ymax=y+se), width=0.3, colour=&quot;blue3&quot;, alpha =0.9, size=0.7)+
  geom_text(aes(x=reorder(x, desc(z)), y=0.5, label=n))+
  theme(legend.position=&quot;bottom&quot;,plot.title = element_text(size=12),
        text = element_text(size=14),
        axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))+
  ggtitle(&quot;Here the title of the plot&quot;) +
  labs(x=&quot;&quot;, ylab=&quot;Lab of y&quot;, fill=NULL)

在 ggplot2 中重新排列 x 轴的类别只能按降序操作。

The following code gives me 36 warnings (In mean.default(X[[i]], ...) : argument is not numeric or logical: returning NA) and produces the following figure.

plot&lt;- df %&gt;%
  ggplot(aes(x=reorder(x, desc(z)), y=y, fill=z))+
  geom_bar(stat=&quot;identity&quot;)+
  geom_errorbar(aes(reorder(x, desc(z)), ymin=y-se, ymax=y+se), width=0.3, colour=&quot;blue3&quot;, alpha =0.9, size=0.7)+
  geom_text(aes(x=reorder(x, desc(z)), y=0.5, label=n))+
  theme(legend.position=&quot;bottom&quot;,plot.title = element_text(size=12),
        text = element_text(size=14),
        axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))+
  ggtitle(&quot;Here the title of the plot&quot;) +
  labs(x=&quot;&quot;, ylab=&quot;Lab of y&quot;, fill=NULL)
plot

This code gives me 36 warnings (In mean.default(X[[i]], ...) : argument is not numeric or logical: returning NA) and produces the following figure.

在 ggplot2 中重新排列 x 轴的类别只能按降序操作。

答案1

得分: 1

认为这应该可以工作!

df <- data.frame(x = c("loc9", "loc2", "loc3", "loc7", "loc5", "loc6", "loc4", "loc1", "loc8"),
                 y = c(1, 2, 5, 3, 5, 6, 7, 9, 5),
                 z = c("A", "A", "B", "B", "B", "B", "C", "C", "C"),
                 se = c(0.1, 0.14, 0.2, 1, 0.25, 0.3, 0.21, 0.23, 0.2),
                 n = c(2, 2, 3, 2, 1, 1, 3, 5, 4))
df$z <- factor(df$z, levels = c("A", "B", "C"))
df = df[order(df$z, df$x), ]
df$x = factor(df$x, levels = unique(df$x))

plot <- df %>%
  ggplot(aes(x = x, y = y, fill = z)) +
  geom_bar(stat = "identity") +
  geom_errorbar(aes(x, ymin = y - se, ymax = y + se), width = 0.3, colour = "blue3", alpha = 0.9, size = 0.7) +
  geom_text(aes(x = x, y = 0.5, label = n))

plot
英文:

Think this should work!

df$z &lt;- factor(df$z, levels=c(&quot;A&quot;, &quot;B&quot;  , &quot;C&quot;))

df = df[order(df$z, df$x), ]

df$x = factor(df$x, levels = unique(df$x))

plot&lt;- df %&gt;%
  ggplot(aes(x=x, y=y, fill=z))+
  geom_bar(stat=&quot;identity&quot;)+ geom_errorbar(aes(x, ymin=y-se, ymax=y+se), width=0.3, colour=&quot;blue3&quot;, alpha =0.9, size=0.7)+ geom_text(aes(x=x, y=0.5, label=n))

plot

Update: Or use fct_reorder (forcats) to order alphabetically

plot&lt;- df %&gt;% ggplot(aes(fct_reorder(x,z), y=y, fill=z)) + geom_bar(stat=&quot;identity&quot;)+ geom_errorbar(aes(x, ymin=y-se, ymax=y+se), width=0.3, colour=&quot;blue3&quot;, alpha =0.9, size=0.7)+ geom_text(aes(x=x, y=0.5, label=n))

在 ggplot2 中重新排列 x 轴的类别只能按降序操作。

huangapple
  • 本文由 发表于 2023年2月19日 19:00:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499641.html
匿名

发表评论

匿名网友

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

确定