日期Vs. Ggplot中的时间条形图

问题描述 投票:0回答:1

我是RShiny的新手,并试图在R上绘制针对日期图的计数(数据集包括就诊的患者)。下面是我使用的代码。

timeCounts <- data.frame(table(mydata[,1])) #the date column
colnames(timeCounts) <- c("pDate", "pCount")
ggplot(timeCounts, aes(x=pDate, y= pCount, fill=pDate)) + geom_bar(stat="identity")

这会生成意外的图形,如下所示:plot1但是我的目的是获得连接值的线形图或条形图(高度与每天计数成正比),请您帮我解决这个问题吗?

将pDate转换为日期格式后,生成了下图:plot2

样本数据如下:dataset

我想生成的是这张图:

pDate     pCount
1/1/2020    36
1/10/2020   60
1/12/2020   63
1/13/2020   59
1/14/2020   80
r ggplot2 shiny geom-bar
1个回答
0
投票

您可以使用此代码创建条形图或折线图:

library(ggplot2)

timeCounts %>%
  mutate(pDate = as.Date(pDate, format="%m/%d/%Y")) %>%
  ggplot(aes(x=pDate, y=pCount)) + 
  geom_bar(stat="identity")

timeCounts %>%
  mutate(pDate = as.Date(pDate, format="%m/%d/%Y")) %>%
  ggplot(aes(x=pDate, y=pCount)) + 
  geom_line()

数据

timeCounts <- structure(list(pDate = c("1/1/2020", "1/10/2020", "1/12/2020", 
"1/13/2020", "1/14/2020"), pCount = c(36L, 60L, 63L, 59L, 80L
)), class = "data.frame", row.names = c(NA, -5L))
© www.soinside.com 2019 - 2024. All rights reserved.