当x是离散变量而不改变条形底部时更改图形y轴的下限 [英] Changing lower limit of graph y-axis when x is a discrete variable without cutting off bottom of bars
问题描述
我正在尝试调整此图的y轴,使其从1而不是0开始.做到这一点的最佳方法是什么?
该解决方案提供了
代码:
groups%>%ungroup()%&%;%mutate(消息= fct_relevel(消息,个人",一般")),enviroattitudeshalf = fct_relevel(enviroattitudeshalf,低环境态度",高环境态度"))%>%ggplot(aes(x =消息,y =平均值))+geom_col(宽度= 0.5,填充="003900")+geom_text(aes(label = round(mean,digits = 1),vjust = -2))+geom_errorbar(aes(ymin =平均值-se,ymax =平均值+ se),宽度= 0.2,位置= position_dodge(.9))+实验室(标题=环境:个人和一般说服力的评估",y =评分",x =个人评估或一般评估")+ylim(0,8)+facet_wrap(〜enviroattitudeshalf)
数据:
结构(列表(enviroattitudeshalf = c(低环境态度",低环境态度",高环境态度",高环境态度"),消息= c(一般",个人",一般",个人"),均值= c(3.89473684210526,3.37894736842105,4.43636363636364、5.10909090909091),se = c(0.145460372156746,0.19522803582675、0.160549137262631、0.171509247396541)),row.names = c(NA,-4L),组=结构(list(enviroattitudeshalf = c(高环境态度",低环境态度"),. rows =结构体(list(3:4,1:2),ptype =整数(0),class = c("vctrs_list_of","vctrs_vctr","list")))),row.names = 1:2,class = c("tbl_df","tbl","data.frame",.drop = TRUE),class = c("grouped_df",("tbl_df","tbl","data.frame"))
作为重新标记y轴的替代方法,您可以通过设置 oob = scales :: oob_squish
.超出范围挤压功能会将超出范围的值设置为最接近的限制.这样可以保留条形图的上部,并给出相同的解释,而重新标记将表明第一个条形图的值超过4,而不会.
groups%>%ungroup()%&%;%mutate(消息= fct_relevel(消息,个人",一般")),enviroattitudeshalf = fct_relevel(enviroattitudeshalf,低环境态度",高环境态度"))%>%ggplot(aes(x =消息,y =平均值))+geom_col(宽度= 0.5,填充="003900")+geom_text(aes(label = round(mean,digits = 1),vjust = -2))+geom_errorbar(aes(ymin =平均值-se,ymax =平均值+ se),宽度= 0.2,位置= position_dodge(.9))+实验室(标题=环境:个人和一般说服力的评估",y =评分",x =个人评估或一般评估")+facet_wrap(〜enviroattitudeshalf)+scale_y_continuous(limits = c(1,8),oob = scales :: oob_squish)
I am trying to adjust the y-axis of this graph so that it starts at 1 instead of 0. What's the best way to do this?
The solution offered here cuts off the bottom of the graph. I would like for the bars to look pretty much identical to the graph below, but with the lower y-limit at 1 and each bar moved down 1 unit to match. I would like to preserve the small amount of gray space below each bar.
Code:
groups %>%
ungroup() %>%
mutate(message = fct_relevel(message, "Personal", "General"),
enviroattitudeshalf = fct_relevel(enviroattitudeshalf, "Low Environmental Attitudes", "High Environmental Attitudes")) %>%
ggplot(aes(x = message, y = mean)) +
geom_col(width = 0.5, fill = "003900") +
geom_text(aes(label = round(mean, digits = 1), vjust = -2)) +
geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = .2, position = position_dodge(.9)) +
labs(title = "Environment: Evaluations of Personal and General Convincingness",
y = "Rating",
x = "Personal evaluation or general evaluation") +
ylim(0, 8) +
facet_wrap(~enviroattitudeshalf)
Data:
structure(list(enviroattitudeshalf = c("Low Environmental Attitudes",
"Low Environmental Attitudes", "High Environmental Attitudes",
"High Environmental Attitudes"), message = c("General", "Personal",
"General", "Personal"), mean = c(3.89473684210526, 3.37894736842105,
4.43636363636364, 5.10909090909091), se = c(0.145460372156746,
0.19522803582675, 0.160549137262631, 0.171509247396541)), row.names = c(NA,
-4L), groups = structure(list(enviroattitudeshalf = c("High Environmental Attitudes",
"Low Environmental Attitudes"), .rows = structure(list(3:4, 1:2), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
As an alternative to re-labeling the y-axis, you can cut it off at 1 by setting oob = scales::oob_squish
. The out of bounds squish function sets the values that are out of bounds to the nearest limit. This preserves the upper part of the bar, giving the same interpretation, whereas relabeling would suggest the first bar exceeds the value 4, and it doesn't.
groups %>%
ungroup() %>%
mutate(message = fct_relevel(message, "Personal", "General"),
enviroattitudeshalf = fct_relevel(enviroattitudeshalf, "Low Environmental Attitudes", "High Environmental Attitudes")) %>%
ggplot(aes(x = message, y = mean)) +
geom_col(width = 0.5, fill = "003900") +
geom_text(aes(label = round(mean, digits = 1), vjust = -2)) +
geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = .2, position = position_dodge(.9)) +
labs(title = "Environment: Evaluations of Personal and General Convincingness",
y = "Rating",
x = "Personal evaluation or general evaluation") +
facet_wrap(~enviroattitudeshalf) +
scale_y_continuous(limits = c(1, 8), oob = scales::oob_squish)
这篇关于当x是离散变量而不改变条形底部时更改图形y轴的下限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!