在几何图形栏上添加一条线,并在RStudio中添加tidyverse

在几何图形栏上添加一条线,并在RStudio中添加tidyverse,r,ggplot2,tidyverse,R,Ggplot2,Tidyverse,飞越者 我想在geom_酒吧上加上平均每小时到达geom_的人数。这是我的一个合成数据示例,一个名为df_avg_occ的表 structure(list(hour = c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20

飞越者

我想在geom_酒吧上加上平均每小时到达geom_的人数。这是我的一个合成数据示例,一个名为df_avg_occ的表

structure(list(hour = c("0", "1", "2", "3", "4", "5", "6", "7", 
                        "8", "9", "10", "11", "12", "13", "14", "15", "16", 
                        "17", "18", "19", "20", "21", "22", "23"), 
               average = c(2.61835748792271, 2.11352657004831, 
                           1.71497584541063, 1.40338164251208, 
                           1.15700483091787, 1.86376811594203, 
                           1.83574879227053, 1.83478260869565, 
                           1.1256038647343,  1.7512077294686, 
                           2.4951690821256, 2.08695652173913, 
                           3.52898550724638,3.85990338164251, 
                           3.96376811594203, 4.00968523002421, 
                           3.9225181598063, 3.96610169491525, 
                           3.89588377723971, 3.95883777239709, 
                           3.81884057971014, 3.71497584541063, 4.5, 
                           3.08454106280193), 
              avg_arrivals = c(2.71428571428571,  1.91666666666667, 
                               1.30612244897959, 1.38, 1.85106382978723, 
                               1.79583333333333, 1.14285714285714, 
                               2.93877551020408, 3.33333333333333, 
                               4.82456140350877, 6.03448275862069, 
                               6.47368421052632, 6.53448275862069, 
                               6.48275862068965, 5.77586206896552, 
                               6.49122807017544, 6.37931034482759, 
                               5.89655172413793, 5.70689655172414, 
                               6.17241379310345, 5.77586206896552, 
                               4.27586206896552, 4.1551724137931, 
                               2.7719298245614)), 
              class = c("tbl_df", "tbl", "data.frame"), 
              row.names = c(NA, -24L))
注意:这不是真实的数据

这是我用tidyverse-ggplot2绘制的

plt_occupancy <- ggplot(tbl_avg_occ, aes(x = hour, average, group = hour))
plt_occupancy + geom_bar(stat = "identity", 
                         alpha=0.7, width = 0.50, fill= "yellow4") +
xlim("0", "1" , "2" , "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 
     "13", "14", "15", "16", "17", "18", "19", "20", "21", "22","23") +
scale_y_continuous(expand = c(0, 0), limits = c(0, 5.5)) +
theme_bw()+ 
labs(title = "Gabriel Burcea Hospital: Hourly ED Occupancy, 3rd of March to 
              27th of April 2016",
     subtitle = "Average Hourly ED Occupancy, n, by Hour of the day.
Note: results are intended for management information only",
     y = "Average occupancy, n", x = "Hour of the day", 
     caption = "Source: Gabriel Burcea ) +
theme(axis.title.y = element_text(margin = margin(t = 0, r = 21, b = 0, l = 
      0)),
    plot.title = element_text(size = 12, face = "bold"),
    plot.subtitle = element_text(size = 10)) 

plt\u您可以尝试一个闪避条形图

library(tidyverse)
d %>% 
  gather(k, v, -hour) %>% 
  ggplot(aes(hour, v, fill=k)) + 
   geom_col(position = position_dodge())

或者在不转换data.frame的情况下,您可以将
平均到达次数
添加为点

d %>% 
  ggplot(aes(x=as.numeric(hour))) + 
  scale_x_continuous(breaks = 0:24)+
  geom_col(aes(y=average, fill="my bars")) + 
  geom_point(aes(y=avg_arrivals)) + 
  geom_line(aes(y=avg_arrivals, group=1, color="my lines")) +
  scale_fill_manual("",values="yellow") + 
  scale_color_manual("",values = 1)


除了
geom_line
之外,您还可以使用
geom_路径(aes(y=avg_arrivals))

如果您更具体地了解您要做的事情,您将得到更多的答案。试着贴一张你所拥有的图片,以及一张你想要的图片(如果需要的话画)。你好,拉斯姆斯,说实话,我不知道如何在这里贴图片。尽管如此,我还是为我的情节添加了代码。你能帮个忙吗?你好,Jimbou,我想把这个作为geom_线加上-平均到达量作为一条线,平均到达量作为一个geom_线。你是一个明星,Jimbou!谢谢你,伙计!然而,对白天的时间顺序要小心一点。不过,它确实很好。@GabrielBurcea请查看我对《小时》杂志的编辑。是的,正如您所看到的,24被省略了,这意味着休息时间是从0:23开始的?是的,请随意更改为您想要的任何内容。