R 将ggplot中的x轴显示从单个天数更改为月份
我试图从ggplot中的数据集中,以日期为x轴,用三个不同的y列可视化日冕感染率的时间序列。 以下是我的数据集的摘录:R 将ggplot中的x轴显示从单个天数更改为月份,r,ggplot2,R,Ggplot2,我试图从ggplot中的数据集中,以日期为x轴,用三个不同的y列可视化日冕感染率的时间序列。 以下是我的数据集的摘录: Nr. Country.Region cases recov. active deaths 69 Germany 2020-03-30 66885 13500 53385 645 70 Germany 2020-03-31 71808 16100 55708 775 71 Germany 2020-04-01 77872
Nr. Country.Region cases recov. active deaths
69 Germany 2020-03-30 66885 13500 53385 645
70 Germany 2020-03-31 71808 16100 55708 775
71 Germany 2020-04-01 77872 18700 59172 920
72 Germany 2020-04-02 84794 22440 62354 1107
73 Germany 2020-04-03 91159 24575 66584 1275
74 Germany 2020-04-04 96092 26400 69692 1444
75 Germany 2020-04-05 100123 28700 71423 1584
76 Germany 2020-04-06 103374 28700 74674 1810
77 Germany 2020-04-07 107663 36081 71582 2016
78 Germany 2020-04-08 113296 46300 66996 2349
79 Germany 2020-04-09 118181 52407 65774 2607
80 Germany 2020-04-10 122171 53913 68258 2767
81 Germany 2020-04-11 124908 57400 67508 2736
82 Germany 2020-04-12 127854 60300 67554 3022
83 Germany 2020-04-13 130072 64300 65772 3194
84 Germany 2020-04-14 131359 68200 63159 3294
85 Germany 2020-04-15 134753 72600 62153 3804
86 Germany 2020-04-16 137698 77000 60698 4052
87 Germany 2020-04-17 141397 83114 58283 4352
我正在分析的数据跨越几个月,所以当我在ggplot中可视化数据时
ggplot(cases.Germany) + geom_point(aes(x = date, y = active.cases, col = "active cases")) + labs(title = "Corona cases in Germany", x = "date", y = "No. of incidents") + geom_point(aes(x = date, y = confirmed.cases, col= "confirmed cases")) + geom_point(aes(x = date, y = recovered, col = "recovered"))
,我当然会得到如下结果:
现在,如果可能的话,我想把每一天分成几个月,可以用来为我的ggplot创建一个x轴,但是我对如何做到这一点感到非常困惑。
感谢您的帮助和指导 由于问题中提供的数据范围较小,图有点有限,但它应该可以做到这一点;我假设当你说将天分组为月份时,你的意思是标签应该显示月份 如果您的意思是将所有数据分组为每月数据,即4月份的病例总数等,则需要采用不同的方法 为了简化对ggplot的调用,我将数据转换为长格式 图书馆GGPLOT2 图书馆弹琴 图书馆三年 df1% mutatedate=as.Datedate%>% pivot\u长期国家、日期 ggplotdf1+ geom_pointaesx=日期,y=值,col=名称+ labstitle=德国电晕案例,x=日期,y=事件数量+ 比例x日期间隔=1个月,日期标签=%B 数据
由v0.3.0于2020年5月20日创建尝试添加+比例x日期间隔=1个月。注:x轴应仅为日期。将各个日期分组为月份是什么意思:x轴上的每月文本或每月数据,即每个月的案例总数?@Neel谢谢!但我有一个问题,它不接受代码,因为我的日期栏似乎不是日期格式。你知道我将如何转换该列吗?@Peter我想显示每一天的值,但希望x轴只显示每个月的文本,因此答案是正确的,它还包括调用dplyr::mutate将字符日期转换为日期格式。嘿,我突然发现第4、5、6、7、8列出现错误,还有7个必须命名。当尝试执行df1%mutatedate=as.Datedate%>%pivot\u longer-ccountry时,date这是新R 4.0版本的问题还是我可能弄乱了其他东西?您是否使用问题中的示例数据?这些数据在我的机器上运行良好,我刚刚从问题中复制了数据和代码。您是否在代码中引用了另一个数据帧?好吧,我显然是个白痴,对不起。您的代码运行得很好,在运行代码之前,我根本没有加载Reforme2,我在另一个项目的早期会话中这样做了,并且显然已经完成了,我一行一行地检查了一遍,发现了我的错误。对不起,再次感谢您的帮助!:不用担心,我发现70%或更多的错误是由于打字错误或遗漏了一些明显的东西!
df <- data.frame( country = c("Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany",
"Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany", "Germany"),
date = c("2020-03-30","2020-03-31", "2020-04-01", "2020-04-02", "2020-04-03", "2020-04-04",
"2020-04-05", "2020-04-06", "2020-04-07", "2020-04-08", "2020-04-09",
"2020-04-10", "2020-04-11", "2020-04-12", "2020-04-13", "2020-04-14",
"2020-04-15", "2020-04-16", "2020-04-17"),
cases = c(66885L, 71808L, 77872L, 84794L, 91159L, 96092L, 100123L, 103374L, 107663L, 113296L, 118181L, 122171L, 124908L, 127854L, 130072L, 131359L, 134753L, 137698L, 141397L),
recov = c(13500L, 16100L, 18700L, 22440L, 24575L, 26400L, 28700L, 28700L, 36081L, 46300L, 52407L, 53913L, 57400L, 60300L, 64300L, 68200L, 72600L, 77000L, 83114L),
active = c(53385L, 55708L, 59172L, 62354L, 66584L, 69692L, 71423L, 74674L, 71582L, 66996L, 65774L, 68258L, 67508L, 67554L, 65772L, 63159L, 62153L, 60698L, 58283L),
deaths = c(645L, 775L, 920L, 1107L, 1275L, 1444L, 1584L, 1810L, 2016L, 2349L, 2607L, 2767L, 2736L, 3022L, 3194L, 3294L, 3804L, 4052L, 4352L))