微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用每个变量的最大值创建ggplot2图的R函数是什么?

如何解决使用每个变量的最大值创建ggplot2图的R函数是什么?

我可以在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

输出:

Relation found here

或添加限制:

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

输出:

enter image description here

使用了一些数据:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。