使用R和ggplot将y轴调整为2x2 ANOVA条形图的问题 [英] Problems adapting the y-axis to 2x2 ANOVA bargraph using R and ggplot

查看:65
本文介绍了使用R和ggplot将y轴调整为2x2 ANOVA条形图的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是Pro R用户,但我已经尝试了多种方法,但找不到解决问题的方法.

I am not a Pro R user but I already tried multiple things and can't find a solution to the problem.

我基于以下网站为2x2 ANOVA创建了条形图,包括误差线,APA主题和自定义颜色: https://sakaluk.wordpress.com/2015/08/27/6-make-it-pretty-plotting-与ggplot2/进行2次交互它可以很好地工作,但是y轴从0开始,尽管我的比例范围仅为1-7.我试图调整该轴,但出现了奇怪的错误.

I created a bar graph for 2x2 ANOVA including error bars, APA theme and custom colors based on this website: https://sakaluk.wordpress.com/2015/08/27/6-make-it-pretty-plotting-2-way-interactions-with-ggplot2/ It works nicely but the y-axis starts at 0 although my scale only ranges from 1 - 7. I am trying to adapt the axis but I get strange errors.

这就是我所做的:

# see https://sakaluk.wordpress.com/2015/08/27/6-make-it-pretty-plotting-2-way-interactions-with-ggplot2/

interactionMeans(anova.2)
plot(interactionMeans(anova.2))

#using ggplot
install.packages("ggplot2")
library(ggplot2)

# create factors with value 

GIFTSTUDY1DATA$PRICE <- ifelse (Scenario == 3 | Scenario == 4, 1, -1 )
table(GIFTSTUDY1DATA$PRICE)
GIFTSTUDY1DATA$PRICE <- factor(GIFTSTUDY1DATA$PRICE, levels = c(-1, +1),
                                  labels = c("2 expensive", "1 inexpensive"))

GIFTSTUDY1DATA$AFFECT <- ifelse (Scenario == 1 | Scenario == 3, -1, +1 )
table(GIFTSTUDY1DATA$AFFECT)
GIFTSTUDY1DATA$AFFECT <- factor(GIFTSTUDY1DATA$AFFECT,
                                 levels = c(-1,1),
                                 labels = c("poor", "rich"))
# get descriptives

dat2 <- describeBy(EVALUATION,list(GIFTSTUDY1DATA$PRICE,GIFTSTUDY1DATA$AFFECT), 
                  mat=TRUE,digits=2)
dat2

names(dat2)[names(dat2) == 'group1'] = 'Price'
names(dat2)[names(dat2) == 'group2'] = 'Affect'

dat2$se = dat2$sd/sqrt(dat2$n)
# error bars +/- 1 SE
limits = aes(ymax = mean + se, ymin=mean - se)
dodge = position_dodge(width=0.9)

# set layout

apatheme=theme_light()+
  theme(panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        panel.border=element_blank(),
        axis.line=element_line(),
        text=element_text(family='Arial'))

#plot

p=ggplot(dat2, aes(x = Affect, y = mean, fill = Price))+
  geom_bar(stat='identity', position=dodge)+
  geom_errorbar(limits, position=dodge, width=0.15)+
  apatheme+
  ylab('mean gift evaluatoin')+
  scale_fill_manual(values=c("yellowgreen","skyblue4"))
p

哪个给我这个数字:

https://i.stack.imgur.com/MwdVo.png

现在,如果我尝试使用ylim或scale_y_continous更改y轴

Now, if I try to change the y-axis using ylim or scale_y_continous

p + ylim(1,7)
p + scale_y_continuous(limits = c(1,7))

我得到了一个带有y轴的图形,但是没有条形,并且显示了一条错误消息

I get a graph with the y-axis as wanted but no bars and an error message stating

删除了包含缺失值(geom_bar)的4行.

Removed 4 rows containing missing values (geom_bar).

https://i.stack.imgur.com/p66H8.png

使用

p + expand_limits(y=c(1,7))
p 

更改y轴的上端,但仍包括零!

changes the upper end of the y-axis but still includes the zero!

我做错了什么?我是否必须不使用geom_bar从头开始?预先感谢.

What am I doing wrong? Do I have to start all over without using geom_bar? Thanks in advance.

推荐答案

我遇到了类似的问题,该问题通过替换来解决

I have encountered a similar problem which was solved by replacing

scale_y_continuous(limits = c() coord_cartesian(ylim = c())

我认为这可能对您有用.

I think this might work for you.

这里是一个例子:

library(tidyverse)

ggplot(mtcars,aes(factor(am),hp)) + 
   geom_bar(stat = "identity") + 
   coord_cartesian(ylim = c(1000,3000))

另请参阅链接: Google R讨论

这篇关于使用R和ggplot将y轴调整为2x2 ANOVA条形图的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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