r - 如何让我的 y 轴标签到达绘图的顶部?
问题描述
我试图让 y 轴以 2.5% 的增量读出,即 0、2.5、5 等,但它既没有这样做,也没有达到 35 岁以下的条形图的顶部。
#Compare the overall net engagment between the two age groups
ad.analysis.age <- engagement %>%
group_by(age) %>%
summarise(ppos = weighted.mean(Attentive.engagement, Impressions),
pneg = pmax(0, weighted.mean(responses - Attentive.engagement, Impressions)),
Impressions = sum(Impressions)) %>%
mutate(netp = ppos - pneg,
marginoferror = 1.96 * (
(ppos * (1 - ppos)) / Impressions + (pneg * (1 - pneg)) / Impressions))
#Plot the visualization
ggplot(ad.analysis.age, aes(x = age, y = netp, ymin = (netp - marginoferror),
ymax = (netp + marginoferror))) +
theme(axis.text.x = element_text(size = 10), axis.title.y = element_text(size = 10)) +
geom_bar(stat = "identity", width = 0.6, fill = "#0A2240") + gpa_theme(10) +
scale_y_continuous(labels = scales::percent_format()) +
labs(x = NULL, y = "Net Attentive Engagement", title =
"Ad Engagement by Age") +
theme(aspect.ratio = 2.5/3) +
theme(axis.title.y = element_text(margin = unit(c(0, 3, 0, 0), "mm")))
解决方案
您应该breaks
在中添加参数scale_y_continuous
:
+ scale_y_continuous(breaks = seq(0,1,by = 0.025), labels = scales::percent_format())
这是使用iris
数据集的插图:
library(dplyr)
library(ggplot2)
iris %>% group_by(Species) %>% summarise(Mean = mean(Sepal.Length)/100) %>%
ggplot(aes(x = Species, y = Mean))+
geom_col( width = 0.6, fill = "#0A2240")+
scale_y_continuous(breaks = seq(0,1,by = 0.025), labels = scales::percent)
它回答了你的问题吗?
推荐阅读
- reactjs - 我应该使用
标签? - javascript - 循环遍历电影数组并调用另一个函数
- python - gcloud 崩溃(OSError):[Errno 2] 没有这样的文件或目录:'/workspace/env/bin/python3.7'
- solr - 多租户电子商务网站中的 Solr Schema 建议
- python - 如何添加动态等高线图
- angular - ngForOf 在嵌套在 turbo 表中的角度 9 中不起作用
- python - 如何减少 Harris Corner Detector 检测到的点数?
- postgresql - psql:sudo:未知用户“abhishek”
- css - CSS对齐旋转的文本
- python - 如何在不覆盖第一项的情况下在链接列表中添加第二项?