简体   繁体   English

列到 x 轴

[英]Columns to x-axis

Please help i am trying to make all the columns into x-axis and the make side by side bars later by date请帮助我尝试将所有列制作成 x 轴,然后按日期制作并排条

this is my data i really tried but to no avail这是我的数据,我真的尝试过但无济于事

    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

I think you're looking for something like this:我想你正在寻找这样的东西:

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

在此处输入图片说明


Reproducible Data from question来自问题的可重复数据

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

Your data cannot be used easily since it requires time to format it into something that could ingested by R .您的数据无法轻松使用,因为它需要时间将其格式化为可以被R摄取的内容。 Here is something to get you started.这里有一些东西可以让你开始。 I made up a hypothetical dataframe of 4 columns that resemble your data, use the function melt from reshape2 package to format the data such that it is understandable by ggplot2 package, and use ggplot2 package to generate a bar plot.我做了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