R:ggplot ylim不起作用 [英] R: ggplot ylim doesn't work

查看:275
本文介绍了R:ggplot ylim不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图在一个情节中改变它。但是,一旦我改变它,情节就消失了。我无法弄清楚原因。有人可以帮我解决问题吗?

 > df< -data.frame(Category = c(A,B,C D,B,A,D,C),Setup = c(X,Y),Gain = c(1.4,1.45,1.43,1.48,1.33,1.37,1.04 ,1.09))
> p< -ggplot(df,aes(Setup,Gain))+ ylim(0.9,1.25)+ geom_bar(stat =identity)+ facet_wrap(Category,ncol = 2 )

当我删除ylim()时,上面的代码工作,但是使用 ylim 它不工作。任何想法是什么问题?



谢谢。

解决方案

code> ylim(0.9,1.25) with

  coord_cartesian(ylim = c(0.9 ,1.25))

函数 ylim 影响哪些数据点用于绘图。相反, coord_cartesian 不会更改基础数据。



完整的命令:

  ggplot(df,aes(Setup,Gain))+ 
coord_cartesian(ylim = c(0.9,1.25))+
geom_bar (stat =identity)+
facet_wrap(Category,ncol = 2)



ylim 方法将产生以下数据:

  df [df $ Gain> = 0.9& df $ Gain< = 1.25,] 

类别设置增益
7 DX 1.04
8 CY 1.09

此外,它将从 0 开始删除所有栏。


I am trying to change the ylim in a plot. But as soon as I change it, the plot vanishes. I am not able to figure out the reason. Can someone help me to root cause the issue?

>df<-data.frame(Category=c("A", "B", "C", "D", "B", "A", "D", "C"), Setup=c("X", "Y"), Gain=c(1.4, 1.45, 1.43, 1.48, 1.33, 1.37, 1.04, 1.09))
>p<-ggplot( df, aes(Setup, Gain)) + ylim(0.9, 1.25) + geom_bar(stat="identity") + facet_wrap( "Category", ncol=2 )

When I remove the ylim(), the above code works but with ylim it doesn't work. Any idea what's the issue?

Thanks.

解决方案

Replace ylim(0.9, 1.25) with

coord_cartesian(ylim = c(0.9, 1.25))

The function ylim also influences which data points are used for plotting. In contrast, coord_cartesian will not change the underlying data.

The complete command:

ggplot(df, aes(Setup, Gain)) + 
  coord_cartesian(ylim = c(0.9, 1.25)) +
  geom_bar(stat = "identity") + 
  facet_wrap("Category", ncol = 2)

The ylim approach will result in the following data:

df[df$Gain >= 0.9 & df$Gain <= 1.25, ]

  Category Setup Gain
7        D     X 1.04
8        C     Y 1.09

Furthermore, it will remove all bars since they start at 0.

这篇关于R:ggplot ylim不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆