Как мне получить метки оси Y, чтобы они достигли вершины моего графика? - PullRequest
1 голос
/ 01 апреля 2020

Я пытаюсь заставить ось y считывать с шагом 2,5%, т. Е. 0, 2,5, 5 и т. Д. c., Но это ни делает, ни даже не достигает вершины гистограммы для Under 35.

The y-axis for this bar chart does not reach the top of the plot.

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

1 Ответ

1 голос
/ 01 апреля 2020

Вы должны добавить 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)

enter image description here

Это отвечает на ваш вопрос?

...