R 函数抛出一个错误

R 函数抛出一个错误,r,ggplot2,R,Ggplot2,我正在努力实现的目标 我有两个数据帧,我想为组变量绘制叠加/叠加的数据帧。变量分为性别和年龄段,我在每个数据框中都有计数。我只使用tidyverse和ggplot2 ae_att_df <- structure(list(Gender = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label =

我正在努力实现的目标

我有两个数据帧,我想为组变量绘制叠加/叠加的数据帧。变量分为性别和年龄段,我在每个数据框中都有计数。我只使用
tidyverse
ggplot2

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))
我的数据帧的可视化应该如下所示:

然而,我尝试了比例尺库,得到了一张空的图表


功能
漂亮的_breaks
位于
scales
包中。 所以你可以试试

scale_y_continuous(breaks = scales::pretty_breaks(15))
或者在开始时加载整个包,使用

library(scales)

函数
pretty\u breaks
位于
scales
包中。 所以你可以试试

scale_y_continuous(breaks = scales::pretty_breaks(15))
或者在开始时加载整个包,使用

library(scales)

首先,我们需要一些具有代表性的样本数据:

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

首先,我们需要一些具有代表性的样本数据:

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


library(scales)
请检查您提供的数据是否正确:如果我尝试运行
ae\u att\u df
,则会出现错误。此外,TIBLE似乎有38行,尽管您只提供了4行。是的,我实际上剪切了它并更改了值,因为我的数据是保密的。但是尝试了对原始数据的建议,在我的绘图中没有任何结果。好的,我明白了,但是请提供不包含34个空行的玩具数据。此外,似乎还有一个拼写错误“ae_att_df”,因为我在尝试运行它时出错。例如,您可以使用
head(my_df,10)
仅显示前10行。您的样本数据不具有代表性,因为在这两种情况下,在
Gender
中仅包含
女性。我将提供一些示例数据作为回答,并向您展示如何绘制所需的绘图。
library(scales)
请检查您提供的数据是否正确:如果我尝试运行
ae\u att\u df
,我会出错。此外,TIBLE似乎有38行,尽管您只提供了4行。是的,我实际上剪切了它并更改了值,因为我的数据是保密的。但是尝试了对原始数据的建议,在我的绘图中没有任何结果。好的,我明白了,但是请提供不包含34个空行的玩具数据。此外,似乎还有一个拼写错误“ae_att_df”,因为我在尝试运行它时出错。例如,您可以使用
head(my_df,10)
仅显示前10行。您的样本数据不具有代表性,因为在这两种情况下,在
Gender
中仅包含
女性。我将提供一些示例数据作为回答,并向您展示如何绘制所需的绘图。添加库(缩放)时,它不会绘制任何内容。如果使用
scale\u y\u continuous(breaks=scales::pretty\u breaks(15))
,会发生什么?添加库(缩放)时,它不会绘制任何内容。如果使用
scale\u y\u continuous会发生什么(休息=缩放::漂亮的休息(15))
?谢谢你,阿德拉。你建议的方式很好,但数据结构不同。无论如何,我会尝试在你提供的结构中获取我的数据。然而,我有两个不同的列,其中包括急诊室就诊人数。但是,入院人数会出现在另一个列中。因此,我需要操作3个不同的列来设计一。非常感谢!@Adele-在ggplot功能中有没有一种方法可以让我对复制的数据集进行同样的处理?为什么我坚持这样做?因为我需要计算所有急诊的就诊人数,这在我的叠加图中首先出现,然后我添加了急诊住院的患者。这就是为什么你的例子并没有抓住我太多的问题。话虽如此,我还是想看看是否有办法将ggplot应用于我的例子,而不是你的例子。首先,你的样本数据不具有代表性,因为它只包括
性别
变量中的
女性
,所以很难绘制所需的输出t、 如果我理解正确,你有一个数据集,然后你有一些子集。在这种情况下,你可以创建新的因子变量来区分住院和未住院的患者。这个新的因子变量可以用在
填充
参数中的第二个
geom_bar
中。你好,阿德拉,我已经解决了问题wi以上建议。非常感谢您的时间。谢谢您阿德拉。您的建议很好,但数据结构不同。无论如何,我会尝试按照您提供的结构获取我的数据。然而,我有两个不同的列,其中包括a&E就诊人数。但是,获准的列会出现在另一个列中。Thus、 我需要操纵3个不同的栏来设计一个。非常感谢!@Adele-在ggplot函数中有没有一种方法可以让我对复制的数据集进行同样的操作?为什么我坚持这样做?因为我需要计算所有a&E的就诊人数,这首先是在我的叠加图中,然后我添加那些被允许进入hosp的人我是从参加A&E的人那里得到的。这就是为什么你的例子没有抓住太多我的问题。话虽如此,我还是想看看是否有办法将ggplot应用于我的例子而不是你的例子。首先,你的样本数据不具代表性,因为它只包括
性别
中的
女性
变量,因此很难绘制所需的输出。如果我理解正确,您有一个数据集,然后有一些子集。在这种情况下,您可以创建新的因子变量来区分住院患者和未住院患者。此新因子变量可用于
填充
参数中的第二个
geom\u bar
中。您好洛杉矶,我已经解决了上述问题。非常感谢您抽出时间。