Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据ggplot中的第三个因子变量保持变量的顺序? 此数据考虑 C > t >代码>作为因素, f>代码>频率为 c>代码> library(tidyverse) df <- tribble(~"c", ~"f", ~"t", "a", 49, "zoo", "b", 12, "foo", "p", 9, "zoo", "q", 29, "foo", "c", 15, "zoo") df %>% ggplot(aes(as.factor(c), f, fill = t)) + geom_bar(stat = "identity") + aes(x = fct_reorder(c, f, .desc = F)) + coord_flip() 库(tidyverse) df% ggplot(aes(作为系数(c),f,填充=t))+ 几何图形栏(stat=“identity”)+ aes(x=fct_重新排序(c,f,.desc=f))+ coord_flip()_R_Ggplot2_Tidyverse - Fatal编程技术网

如何根据ggplot中的第三个因子变量保持变量的顺序? 此数据考虑 C > t >代码>作为因素, f>代码>频率为 c>代码> library(tidyverse) df <- tribble(~"c", ~"f", ~"t", "a", 49, "zoo", "b", 12, "foo", "p", 9, "zoo", "q", 29, "foo", "c", 15, "zoo") df %>% ggplot(aes(as.factor(c), f, fill = t)) + geom_bar(stat = "identity") + aes(x = fct_reorder(c, f, .desc = F)) + coord_flip() 库(tidyverse) df% ggplot(aes(作为系数(c),f,填充=t))+ 几何图形栏(stat=“identity”)+ aes(x=fct_重新排序(c,f,.desc=f))+ coord_flip()

如何根据ggplot中的第三个因子变量保持变量的顺序? 此数据考虑 C > t >代码>作为因素, f>代码>频率为 c>代码> library(tidyverse) df <- tribble(~"c", ~"f", ~"t", "a", 49, "zoo", "b", 12, "foo", "p", 9, "zoo", "q", 29, "foo", "c", 15, "zoo") df %>% ggplot(aes(as.factor(c), f, fill = t)) + geom_bar(stat = "identity") + aes(x = fct_reorder(c, f, .desc = F)) + coord_flip() 库(tidyverse) df% ggplot(aes(作为系数(c),f,填充=t))+ 几何图形栏(stat=“identity”)+ aes(x=fct_重新排序(c,f,.desc=f))+ coord_flip(),r,ggplot2,tidyverse,R,Ggplot2,Tidyverse,以上代码生成此图 在这个图中,如何将相似的变量保持在一起,比如蓝色旁边是蓝色,红色旁边是红色 我们可以使用facet\u网格进行拆分 library(dplyr) library(ggplot2) df %>% ggplot(aes(c, f, fill = t)) + geom_bar(stat = 'identity') + aes(x = fct_reorder(c, f, .desc = F)) + co

以上代码生成此图


在这个图中,如何将相似的变量保持在一起,比如蓝色旁边是蓝色,红色旁边是红色

我们可以使用
facet\u网格进行拆分

library(dplyr)
library(ggplot2)
df %>%
     ggplot(aes(c, f, fill = t)) +
           geom_bar(stat = 'identity') +
           aes(x = fct_reorder(c, f, .desc = F)) +
           coord_flip() + 
           facet_grid(~ t)

除了@akrun的答案之外,您还可以将一些参数传递到
facet_grid
theme
函数中,以使刻面图看起来像一个单独的图:

df%>%
ggplot(aes(作为系数(c),f,填充=t))+
几何图形栏(stat=“identity”)+
aes(x=fct_重新排序(c,f,.desc=f))+
coord_flip()+
面网格(t~,scales=“free\u y”,space=“free\u y”)+
主题(strip.background.y=element_blank(),
strip.text.y=element\u blank(),
面板边距y=单位(-0.25,“线”))

如果我们对数据进行适当的安排,我们就不需要
facet\u grid

library(dplyr)
library(ggplot2)

df %>% 
  arrange(t, f) %>%
  mutate(c = factor(c, levels = unique(c))) %>% 
  ggplot(aes(c, f, fill = t)) +
  geom_bar(stat = "identity") +
  coord_flip()

谢谢,但由于绘图中的空行,结果并不理想。@AjayKumarKoli更容易理解绘图。但是,如果你按照你想要的方式去做,有更多的团体,就很难聚集起来insights@dc37你的回答非常适合我想要的情节。非常感谢。:)