仅当在ggplot中将y轴的下限设置为0时,才会显示图形条 [英] Graph bars only appear when lower limit of y axis set to 0 in ggplot

查看:45
本文介绍了仅当在ggplot中将y轴的下限设置为0时,才会显示图形条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建条形图.当我将限制设置为(0,7)时,出现条形图.但是,我希望下限为1,而不是0.当我将下限设置为1时,条形图不会出现.我收到以下错误消息:

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

我如何设置限制并不重要.我已经使用了以下两个选项:

  ylim(1,7)scale_y_continuous(极限= c(1,7)) 

有人知道我该如何解决吗?

我想要一个看起来像这样的图,但是将1作为下y轴标签,这意味着所有条形将向下移动1.

这是该图的完整代码:

 完整%>%ggplot(aes(x =阶,y =均值))+geom_bar(stat ="identity",填充="003900",宽度= 0.5,position = position_dodge())+geom_errorbar(aes(ymin =平均值-se,ymax =平均值+ se),宽度= 0.2,位置= position_dodge(.9))+geom_text(aes(label = round(mean,digits = 1)),position = position_dodge(width = 1.0),vjust = -4.0,size = 3)+#facet_wrap(〜names)+实验室(标题=响应每条消息的行为意图")+#ylim(0,7)+scale_y_continuous(limits = c(1,7))+主题(axis.text = element_text(size = 7))+xlab(消息")+ylab(行为意图") 

以下是可复制的数据:

  structure(list(message = c("a","e"","h","m","convince_animals","convince_environment","convince_health","convince_money"),平均值= c(3.1038961038961、3.21052631578947、3.56、2.7972972972973,4.19512195121951、4.18536585365854、5.65365853658537、4.93658536585366),se = c(0.208814981196227,0.204609846510406,0.220760356801522,0.20542415978608、0.121188432228325、0.11075110910238、0.0896896391724367,0.120394657272105),类型= c(行为意图",行为意图",行为意图",行为意图",预期行为",在此,预期行为",预期行为",预期行为"被称为预期行为".),名称= c(动物",环境",健康",金钱",动物",环境",健康",金钱"),阶数= c(1,3、5、7、2、4、6、8)),row.names = c(NA,-8L),class = c("tbl_df",(tbl),"data.frame")) 

解决方案

看起来您正在通过设置限制来丢失数据,这使您的图变糟.您可以使用 coord_cartesian()而不是 ylim()来放大"数据;请参见

I am trying to create a bar graph. When I set the limits as (0,7), the bars appear. However, I would like the lower limit to be 1, not 0. When I set the lower limit to 1, the bars do not appear. I get the following error message:

Removed 8 rows containing missing values (geom_bar).

It doesn't matter how I set the limits. I have used both of the following options:

ylim(1, 7)

scale_y_continuous(limits = c(1, 7))

Does anyone know how I can fix this?

I'd like a graph that looks like this, but with 1 as the lower y-axis label, which would mean all the bars would be shifted down by 1.

Here's the full code for the graph:

full %>%
 ggplot(aes(x = order, y = mean)) + 
 geom_bar(stat = "identity", fill = "003900", width = 0.5, position = position_dodge()) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = .2, position = position_dodge(.9)) +
  geom_text(aes(label = round(mean, digits =1)), position = position_dodge(width=1.0), vjust = -4.0, size = 3) +
  #facet_wrap(~names) +
  labs(title = "Behavioral intentions in response to each message") +
 # ylim(0, 7) + 
  scale_y_continuous(limits = c(1, 7)) + 
  theme(axis.text = element_text(size = 7)) + 
  xlab("Message") + 
  ylab("Behavioral intentions")

Here's reproducible data:

structure(list(message = c("a", "e", "h", "m", "convince_animals", 
"convince_environment", "convince_health", "convince_money"), 
    mean = c(3.1038961038961, 3.21052631578947, 3.56, 2.7972972972973, 
    4.19512195121951, 4.18536585365854, 5.65365853658537, 4.93658536585366
    ), se = c(0.208814981196227, 0.204609846510406, 0.220760356801522, 
    0.20542415978608, 0.121188432228325, 0.11075110910238, 0.0896896391724367, 
    0.120394657272105), type = c("Behavioral Intentions", "Behavioral Intentions", 
    "Behavioral Intentions", "Behavioral Intentions", "Expected Behavior", 
    "Expected Behavior", "Expected Behavior", "Expected Behavior"
    ), names = c("Animals", "Environment", "Health", "Money", 
    "Animals", "Environment", "Health", "Money"), order = c(1, 
    3, 5, 7, 2, 4, 6, 8)), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame"))

解决方案

Looks like you're losing data by setting limits and it's screwing up your plot. You can use coord_cartesian() instead of ylim() to 'zoom in' on your data; see https://stackoverflow.com/a/25685952/12957340 and/or page 160 of the ggplot2 book for further info.

full %>%
  ggplot(aes(x = order, y = mean)) + 
  geom_bar(stat = "identity", fill = "003900", width = 0.5, position = position_dodge()) +
  geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = .2, position = position_dodge(.9)) +
  geom_text(aes(label = round(mean, digits =1)), position = position_dodge(width=1.0), vjust = -4.0, size = 3) +
  #facet_wrap(~names) +
  labs(title = "Behavioral intentions in response to each message") +
  coord_cartesian(ylim = c(1, 7)) + 
  theme(axis.text = element_text(size = 7)) + 
  xlab("Message") + 
  ylab("Behavioral intentions")

这篇关于仅当在ggplot中将y轴的下限设置为0时,才会显示图形条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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