缩放ggplot2中的Y轴错误条 [英] Scaling Y axis errorbars in ggplot2

查看:174
本文介绍了缩放ggplot2中的Y轴错误条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用Rstudio的以下数据框:

  X NSUM MEAN LOWMEAN UPMEAN 
2非货币奖励800 4.86 4.58 5.15
3 $ 0(不提​​)822 5.06 4.78 5.35
4 $ 25 830 6.35 6.06 6.65
5 $ 50 815 6.84 6.54 7.14
6 $ 75 864 7.00 6.70 7.29

所以我使用下面的命令创建了这个漂亮的图:


$ (原始数据,aes(x = rawdata $ x,y = rawdata $ MEAN))+
geom_point(color =red)b $ b

  plot1 < -  ggplot )+ 
geom_errorbar(aes(ymin = rawdata $ LOWMEAN,ymax = rawdata $ UPMEAN,width = 0),color =black)+
coord_flip()

绘制手段和图表来显示上限和下限。我想要做的就是改变y轴,所以蜱不会出现,但不管我做什么,ylim()或scale_y_continuous()我得到的错误:

应用于连续变量的离散值?

解决方案

所以有几件事。首先, aes(...)会在默认数据集的上下文中评估它的参数 / em>的。这意味着 aes(x = X,...)将在(任意)列中查找(第一个)列 X 您在 data = ... 参数( rawdata )中使用的数据框。您应该不要使用,例如 rawdata $ X aes(...)内。这可能会导致不可预知的结果,并且通常很不明显。

c $ c>和 limits = ... scale_y_continuous(...)中。前者将设置标记,但可能不会显示所有标记,因为限制是自动设置的。后者将覆盖默认限制。所以下面是你的数据的两个例子:

  library(ggplot2)
ggplot(rawdata,aes(x = X ,y = MEAN))+
geom_point(color =red)+
geom_errorbar(aes(ymin = LOWMEAN,ymax = UPMEAN,width = 0))+
coord_flip()+
scale_y_continuous(breaks = c(4,5,6,7,8))



请注意我们如何设置休息时间(4,5,6,7,8),但我们没有看到4和8.这是因为 ggplot 会自动设置限制,其中不包括4你可以强制这个如下:

$ $ p $ $ $ $ $ $ ggplot(rawdata,aes(x = X,y = MEAN))+
geom_point(color =red)+
geom_errorbar(aes(ymin = LOWMEAN,ymax = UPMEAN,width = 0))+
coord_flip()+
scale_y_continuous = c(4,5,6,7,8),limits = c(4,8))



最后,如果哟你需要使用主题(...)

$ b来清除
之间的微弱网格线
$ b

  ggplot(rawdata,aes(x = X,y = MEAN))+ 
geom_point(color =red)+
geom_errorbar(aes(ymin = LOWMEAN,ymax = UPMEAN,width = 0))+
coord_flip()+
scale_y_continuous(break = c(4,5,6,7,8),limits = c(4,8))+
主题(panel.grid.minor = element_blank())




HI all I'm using Rstudio with the following dataframe:

                      X NSUM MEAN LOWMEAN UPMEAN
2 Nonmonetary Incentive  800 4.86    4.58   5.15
3       $0 (no mention)  822 5.06    4.78   5.35
4                  $25   830 6.35    6.06   6.65
5                  $50   815 6.84    6.54   7.14
6                  $75   864 7.00    6.70   7.29

So I've created this nice plot using the following command:

plot1 <- ggplot(rawdata, aes(x = rawdata$X, y = rawdata$MEAN)) + 
  geom_point(colour = "red") + 
  geom_errorbar(aes(ymin = rawdata$LOWMEAN, ymax = rawdata$UPMEAN, width =0), colour = "black") + 
  coord_flip()

Which plots the means and a bar to show the upper and lower bounds. What I want to do is change the y axis so the ticks don't appear as often but no matter what I do, ylim() or scale_y_continuous() I get the error:

Discrete values applied to continuous variable?

解决方案

So there are a couple of things.

First, aes(...) evaluates it's argument in the context of the default dataset. This means that aes(x=X,...) will look (first) for a column X in whatever data frame you use in the data=... argument (rawdata, in your case). You should never use, e.g. rawdata$X inside aes(...). This can cause unpredictable and often very obscure results.

Second, to control the number of axis ticks, use breaks=... and limits=... in scale_y_continuous(...). The former will set the ticks but might not display all of them because limits are set automatically. The latter will override the default limits. So here are two example with your data:

library(ggplot2)
ggplot(rawdata, aes(x = X, y = MEAN)) + 
  geom_point(colour = "red") + 
  geom_errorbar(aes(ymin = LOWMEAN, ymax = UPMEAN, width =0)) + 
  coord_flip()+
  scale_y_continuous(breaks=c(4,5,6,7,8))

Notice how we've set the breaks to (4,5,6,7,8), but we don't see 4 and 8. This is because ggplot is setting limits automatically, which don't include 4 and 8. You can force this as follows:

ggplot(rawdata, aes(x = X, y = MEAN)) + 
  geom_point(colour = "red") + 
  geom_errorbar(aes(ymin = LOWMEAN, ymax = UPMEAN, width =0)) + 
  coord_flip()+
  scale_y_continuous(breaks=c(4,5,6,7,8), limits=c(4,8))

Finally, if you want toget rid of the faint grid lines between the major ticks, you need to use theme(...)

ggplot(rawdata, aes(x = X, y = MEAN)) + 
  geom_point(colour = "red") + 
  geom_errorbar(aes(ymin = LOWMEAN, ymax = UPMEAN, width =0)) + 
  coord_flip()+
  scale_y_continuous(breaks=c(4,5,6,7,8), limits=c(4,8))+
  theme(panel.grid.minor=element_blank())

这篇关于缩放ggplot2中的Y轴错误条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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