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
R:fct_reorder()和绘制绘图网格-不兼容?_R_Ggplot2_Cowplot - Fatal编程技术网

R:fct_reorder()和绘制绘图网格-不兼容?

R:fct_reorder()和绘制绘图网格-不兼容?,r,ggplot2,cowplot,R,Ggplot2,Cowplot,我正在使用fct\u reorder()对ggplot中的因子级别进行排序。这对于单独的情节来说效果很好。但是当我使用cowplot中的plot\u grid()时,出现了一些问题。作为对比,在左边,我使用了一个具有固定因子水平的绘图,而不是使用fct\u reorder 编辑: 以下是我正在使用的实际代码: #make the base myplot <-filter(summary_by_intensity_reshaped, str_detect(feature, "binary

我正在使用
fct\u reorder()
对ggplot中的因子级别进行排序。这对于单独的情节来说效果很好。但是当我使用
cowplot
中的
plot\u grid()
时,出现了一些问题。作为对比,在左边,我使用了一个具有固定因子水平的绘图,而不是使用
fct\u reorder

编辑: 以下是我正在使用的实际代码:

#make the base 
myplot <-filter(summary_by_intensity_reshaped, str_detect(feature, "binary"), Frequency == "2Hz") %>%
ggplot(., aes(fct_reorder(feature, mean),mean,fill=Intensity, ymax=mean+sem, ymin=mean-sem)) 

#add the layers
myplot  + geom_bar(stat="identity", position=position_dodge()) + 
  geom_errorbar(aes(width=0.2),position=position_dodge(0.9)) + 
  labs(x="Behavior",y="Percent of Trials (%)") +
  scale_x_discrete(breaks=c("binary_flutter", "binary_hold", "binary_lift", "binary_jump","binary_rear", "binary_lick", "binary_guard", "binary_vocalize"), labels=c("Flutter", "Holding", "Lifting", "Jumping", "Rearing", "Licking", "Guarding", "Vocalizing"))+
  facet_grid(~Frequency)+
  theme(axis.text.x=element_text(angle=-90))
#打基础
myplot%
ggplot(,aes(fct_重新排序(特征,平均值),平均值,填充=强度,ymax=平均值+扫描电镜,ymin=平均扫描电镜))
#添加图层
myplot+geom_bar(stat=“identity”,position=position_dodge())+
几何误差条(aes(宽度=0.2),位置=位置减淡(0.9))+
实验室(x=“行为”,y=“试验百分比(%)”)+
缩放x离散(断裂=c(“二元颤振”、“二元保持”、“二元提升”、“二元跳跃”、“二元后方”、“二元舔”、“二元保护”、“二元发声”),标签=c(“颤振”、“保持”、“提升”、“跳跃”、“提升”、“舔”、“保护”、“发声”))+
平面网格(~频率)+
主题(axis.text.x=元素\文本(角度=-90))
输出如下所示:


当我尝试在
plot\u grid()
中使用“myplot”时,问题就出现了。此时,它会呈现出奇怪的效果,如下面的示例所示

我怀疑您使用的
fct\u reorder()
不正确
plot_grid()
只需将绘制的任何绘图放入网格即可

library(ggplot2)
library(cowplot)
library(forcats)

p1 <- ggplot(mpg, aes(class, displ, color = factor(cyl))) + geom_point()    
p2 <- ggplot(mpg, aes(fct_reorder(class, displ, mean), displ, color = factor(cyl))) + 
        geom_point()
plot_grid(p1, p2)
库(ggplot2)
图书馆(cowplot)
图书馆(供猫用)

p1你能提供一个可重复的例子吗?嗨。谢谢你的回复。澄清一下,在我的例子中,“mean”不是mean函数。平均值实际上是我的data.frame中因子的名称。我计算平均数,并将其放入一列。如果我单独绘图,绘图看起来很好。当我尝试使用plot_grid时,它呈现为那样。好吧,在任何情况下,您都必须提供一个完整的、可复制的示例,以便我们提供更有用的反馈。请参阅此问题和答案以获取指导。