问题描述
我可以在R中写些什么来创建线形图可视化(使用ggplot2)来表示每天的总病例数(本质上是每天标出星号的点)?
city | date | cases | cumulative cases (by date)
----------|-------|--------|------------------------
London | Day 1 | 4 | 4
Paris | Day 1 | 5 | 9
Madrid | Day 1 | 3 | 12 *
London | Day 2 | 6 | 6
Paris | Day 2 | 3 | 9
Madrid | Day 2 | 8 | 17 *
London | Day 3 | 9 | 9
Paris | Day 3 | 7 | 16
Madrid | Day 3 | 5 | 21 *
解决方法
尝试按Sub Test()
Dim htmlTable As Object
Dim collTD As Object
Dim oNode As Object
Dim IE As Object
Dim RowCount As Long
Dim currentColumn As Long
RowCount = 1
currentColumn = 1
Set IE = CreateObject("InternetExplorer.application")
With IE
.Visible = True
.navigate "https://ukonlinestores.co.uk/amazon-uk-sellers/"
' Wait for the page to fully load; you can't do anything if the page is not fully loaded
Do While .readyState <> 4: DoEvents: Loop
Application.Wait (Now + TimeSerial(0,2))
Set htmlTable = .document.getElementById("table_1")
Set collTD = htmlTable.getElementsByTagName("td")
For Each oNode In collTD
If currentColumn Mod 11 = 0 Then
RowCount = RowCount + 1
currentColumn = 1
End If
Cells(RowCount,currentColumn) = oNode.innertext
currentColumn = currentColumn + 1
Next oNode
End With
End Sub
分组的这种方法以获得最大值,然后绘制。下面是使用date
函数的代码:
tidyverse
输出:
或添加限制:
library(dplyr)
library(ggplot2)
#Code
df %>% group_by(date) %>%
filter(`cumulative cases`==max(`cumulative cases`,na.rm=T)) %>%
ggplot(aes(x=date,y=`cumulative cases`,group=1))+
geom_point(color='purple')+geom_line(color='purple')
输出:
使用了一些数据:
#Code 2
df %>% group_by(date) %>%
filter(`cumulative cases`==max(`cumulative cases`,group=1))+
geom_point(color='purple')+geom_line(color='purple')+
scale_y_continuous(limits = c(0,21))