英文:
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<- 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("Here the title of the plot") +
labs(x="", ylab="Lab of y", fill=NULL)
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<- 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("Here the title of the plot") +
labs(x="", ylab="Lab of y", 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.
答案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 <- 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
Update: Or use fct_reorder (forcats) to order alphabetically
plot<- df %>% ggplot(aes(fct_reorder(x,z), 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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论