簡體   English   中英

列到 x 軸

[英]Columns to x-axis

請幫助我嘗試將所有列制作成 x 軸,然后按日期制作並排條

這是我的數據,我真的嘗試過但無濟於事

    dateVisited hh_visited hh_ind_confirmed new_in_mig out_mig deaths HOH_death Preg_Obs Preg_Outcome child_forms
102  2020-07-21        292             1170        131      86     18         7        3           14          79
103  2020-07-22        400             1553        115     100     25        10       11           18         107
104  2020-07-23        381             1458        103      67     21         9        5           23          87
105  2020-07-24        345             1379         90      98     12         4        3           20          89
106  2020-07-25        436             1585        131     119     13         2        7           20         117
107  2020-07-26          0                0          0       0      0         0        0    

    0           0

我想你正在尋找這樣的東西:

library(tidyr)
library(ggplot2)

df %>% 
  pivot_longer(cols = -1) %>%
  ggplot(aes(name, value)) +
  geom_col(aes(fill = dateVisited), width = 0.6, 
           position = position_dodge(width = 0.8)) +
  guides(x = guide_axis(angle = 45))

在此處輸入圖片說明


來自問題的可重復數據

df <- structure(list(dateVisited = structure(1:6, .Label = c("2020-07-21", 
"2020-07-22", "2020-07-23", "2020-07-24", "2020-07-25", "2020-07-26"
), class = "factor"), hh_visited = c(292L, 400L, 381L, 345L, 
436L, 0L), hh_ind_confirmed = c(1170L, 1553L, 1458L, 1379L, 1585L, 
0L), new_in_mig = c(131L, 115L, 103L, 90L, 131L, 0L), out_mig = c(86L, 
100L, 67L, 98L, 119L, 0L), deaths = c(18L, 25L, 21L, 12L, 13L, 
0L), HOH_death = c(7L, 10L, 9L, 4L, 2L, 0L), Preg_Obs = c(3L, 
11L, 5L, 3L, 7L, 0L), Preg_Outcome = c(14L, 18L, 23L, 20L, 20L, 
0L), child_forms = c(79L, 107L, 87L, 89L, 117L, 0L)), class = "data.frame",
row.names = c("102", "103", "104", "105", "106", "107"))

您的數據無法輕松使用,因為它需要時間將其格式化為可以被R攝取的內容。 這里有一些東西可以讓你開始。 我做了4列類似於您的數據,使用該功能的假設數據幀meltreshape2包格式化數據,它是可以理解的ggplot2包,並使用ggplot2包生成柱狀圖。

df <- data.frame(dateVisited = seq(as.Date('2019-01-01'), as.Date('2019-12-31'), 30),
                 hh_visited = runif(13, 0, 436),
                 hh_ind_confirmed = runif(13, 0, 1585),
                 new_in_mig = runif(13, 0, 131))

df <- reshape2::melt(df, id.vars = 'dateVisited')

ggplot(data = df, aes(x = dateVisited, y = value, fill = variable))+
  geom_col(position = 'dodge')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM