首页 > 解决方案 > 如何让我的 y 轴标签到达绘图的顶部?

问题描述

我试图让 y 轴以 2.5% 的增量读出,即 0、2.5、5 等,但它既没有这样做,也没有达到 35 岁以下的条形图的顶部。

此条形图的 y 轴未到达图的顶部。

#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")))

标签: rggplot2bar-chart

解决方案


您应该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)

在此处输入图像描述

它回答了你的问题吗?


推荐阅读