简体   繁体   English

pretty_breaks函数引发错误

[英]pretty_breaks function trows an error

The Goal I am trying to achieve 我正在努力实现的目标

I have two dataframes that I want to plot them overlayed/superimposed for the group variables. 我有两个数据框,我想将它们重叠/叠加在组变量上。 The variables are grouped gender and age band and I have Count in each dataframe. 变量按性别和年龄段分组,每个数据帧中都有计数。 I am working only with tidyverse and ggplot2 . 我只使用tidyverseggplot2

ae_att_df <- structure(list(Gender = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                 1L, 1L, 1L), .Label = 
                      c("Female", "Male", "Not Specified"), class = "factor"), 
             AgeBand = structure(1:10, .Label = c("0 yrs", "1-4 yrs", "10-14 
                                       yrs", "15- 19 yrs", "20-24 yrs", "25- 
                            29 yrs", "30-34 yrs", "35-39 yrs", "40-44 yrs", 
                            "45-49 yrs", "5-9 yrs", "50-54 yrs", "55-59 yrs", 
                          "60-64 yrs", "65-69 yrs", "70-74 yrs", "75-79 yrs", 
                          "80-84 yrs", "85+ yrs"), class = "factor"), N = 
                     c(4708L, 7065L, 1914L, 2292L, 4612L, 5968L, 5620L, 
                       4007L, 2802L, 2429L)), row.names = c(NA, -10L), class 
                  = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = 
       "Gender", drop = TRUE, indices = list(0:9), group_sizes = 10L, 
                                                           biggest_group_size 
                                             = 10L, labels = structure(list(
        Gender = structure(1L, .Label = c("Female", "Male", "Not Specified"), 
             class = "factor")), row.names = c(NA, -1L), class = 
             "data.frame", vars = "Gender", drop = TRUE))

ae_adm_df <- structure(list(Gender = structure(c(1L, 1L, 1L, 1L, 1L, 
                                            1L, 1L, 1L, 1L, 1L), .Label = 
                                 c("Female", "Male", "Not Specified"), class 
                                   = "factor"), 
                     AgeBand = structure(1:10, .Label = c("0 yrs", "1-4 yrs", 
                      "10-14 yrs", "15-19 yrs", "20-24 yrs", "25-29 yrs", 
                       "30-34 yrs", "35-39 yrs", "40-44 yrs", "45-49 yrs", 
                       "5-9 yrs", "50-54 yrs", "55-59 yrs", "60-64 yrs", "65- 
                       69 yrs", "70-74 yrs", "75-79 yrs", "80-84 yrs", "85+ 
                      yrs"), class = "factor"), N = c(4352L, 6229L, 2145L, 
                       2328L, 3963L, 4769L, 4475L, 3342L, 2544L, 2141L)), 
                       row.names = c(NA, -10L), class = c("grouped_df", 
                                   "tbl_df", "tbl", "data.frame"), 
                 vars = "Gender", drop = TRUE, indices = list(0:9), 
                 group_sizes = 10L, biggest_group_size = 10L, labels = 
                 structure(list(Gender = structure(1L, .Label = c("Female", 
                                                "Male", "Not Specified"), 
                 class = "factor")), row.names = c(NA, -1L), class = 
                "data.frame", vars = "Gender", drop = TRUE))

The Problem I have 我有问题

When I am attempting to plot it with 'ggplot2', it trows an error for pretty_breaks . 当我试图与“GGPLOT2”绘制,它trows为pretty_breaks错误。

Error in pretty_breaks(15) : could not find function "pretty_breaks". pretty_breaks(15)中的错误:找不到函数“ pretty_breaks”。

This is the code I have tried: 这是我尝试过的代码:

ggplot(NULL, aes(x = AgeBand, y = N)) +
geom_bar(data = ae_att_df,  stat = 'identity', position = 'dodge', aes(fill = 
                                                                  Gender))+
geom_bar(data = ae_adm_df,  stat = 'identity',position =position_dodge(0.9), 
                            width = 0.6, aes(fill = Gender)) + xlab("Age 
                                                                    Group") +
ylab("ED Attendances and Admissions") + ggtitle(paste("Unscheduled ED 
                                                       Attendance and 
                                                        Admission")) +
theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
scale_y_continuous(breaks = pretty_breaks(15)) +
 scale_fill_manual("legend", values = c("Female Admissions" = "coral3", 
                                        "Female Attendances" = "lightcoral", 
                                        "Male Admissions" = "steelblue4", 
                                        "Male Attendances" = "lightblue2"))

The vizualization of my dataframe should look like this: 我的数据框的Vizualization应该如下所示:

在此处输入图片说明

Yet, I have tried the scale library and I get an empty chart. 但是,我尝试了比例尺库,但得到了一个空图表。

在此处输入图片说明

the function pretty_breaks is inside the scales package. 函数pretty_breaksscales包中。 So you could try 所以你可以尝试

scale_y_continuous(breaks = scales::pretty_breaks(15))

Or load the whole package at the beginning, using 或在开始时使用加载整个程序包

library(scales)

First we need some representative sample data: 首先,我们需要一些代表性的样本数据:

set.seed(123)
ae_adm_df <- data.frame("Gender" = rep(c("Female Admitted", "Male Admitted"), each = 4),
                        "AgeBand" = rep(c("0 yrs", "1-4yrs", "10-14yrs", "15-19yrs"), 2),
                        "N" = sample(2000:8000, 8))

#   Gender           AgeBand    N
# 1 Female Admitted    0 yrs 3725
# 2 Female Admitted   1-4yrs 6729
# 3 Female Admitted 10-14yrs 4453
# 4 Female Admitted 15-19yrs 7296
# 5   Male Admitted    0 yrs 7639
# 6   Male Admitted   1-4yrs 2273
# 7   Male Admitted 10-14yrs 5165
# 8   Male Admitted 15-19yrs 7349

ae_att_df <- data.frame("Gender" = rep(c("Female Not Admitted", "Male Not Admitted"), each = 4),
                        "AgeBand" = rep(c("0yrs", "1-4yrs", "10-14yrs", "15-19yrs"), 2),
                        "N" = sample(1000:3000, 8))
#                Gender  AgeBand    N
# 1 Female Not Admitted     0yrs 2103
# 2 Female Not Admitted   1-4yrs 1913
# 3 Female Not Admitted 10-14yrs 2912
# 4 Female Not Admitted 15-19yrs 1905
# 5   Male Not Admitted     0yrs 2353
# 6   Male Not Admitted   1-4yrs 2142
# 7   Male Not Admitted 10-14yrs 1205
# 8   Male Not Admitted 15-19yrs 2794

Noticed that levels of Gender in data ae_adm_df differ from levels of Gender in ae_att_df . 注意到水平的Gender数据ae_adm_df从水平不同Genderae_att_df This is important. 这个很重要。

Now we can plot. 现在我们可以绘图。 Do not include aes() in ggplot() , as there is no data provided. 不要在ggplot()包含aes() ,因为没有提供data You need to provide aes(x = AgeBand, fill = Gender, y = N) for both geom_bar() instead. 您需要同时为两个geom_bar()提供aes(x = AgeBand, fill = Gender, y = N)

library(scales)
library(ggplot2)

ggplot() +
  geom_bar(data = ae_adm_df, aes(x = AgeBand, fill = Gender, y = N), 
           stat = 'identity', position = 'dodge') +
  geom_bar(data = ae_att_df, aes(x = AgeBand, y = N, fill = Gender), 
           stat = 'identity', position = position_dodge(0.9), width = 0.6) + 
  xlab("Age Group") +
  ylab("ED Attendances and Admissions") + 
  ggtitle("Unscheduled ED Attendance and Admission") +
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
  scale_y_continuous(breaks = scales::pretty_breaks(15)) +
  scale_fill_manual("", values = c("Female Admitted" = "coral3", 
                                   "Female Not Admitted" = "lightcoral", 
                                   "Male Admitted" = "steelblue4", 
                                   "Male Not Admitted" = "lightblue2"))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM