如何在 ggplot2 中指向历史记录?
How to point a hist in ggplot2?
我的鼻息肉有些问题。我用 base R 做了一个 hist,但现在我想用 ggplot2 做同样的事情。我想在我的历史中指出一些国家。我将把我的数据做一个可重现的例子。这里是:
library(xml2)
library(rvest)
library(dplyr)
library(ggplot2)
url <- "https://www.worldometers.info/coronavirus/"
web <- xml2::read_html(url)
dat <- rvest::html_table(web)[[1]]
names(dat)[1]<-"record"
dat<-dat[-c(1:8,224:231),-c(17:19)]
names(dat)<-c("record","Country","TotalCases","NewCases","TotalDeaths","NewDeaths",
"TotalRecovered","NewRecovered","ActiveCases","SeriosuCritical",
"Cases_M","Deaths_M","TotalTests","Tests_M","Population","Continent")
dat[dat==""]<-NA
dat[-c(1,2,16)]<-apply(dat[-c(1,2,16)],2,function(x)gsub(",","",x)) # cut commas
dat[-c(1,2,16)]<-apply(dat[-c(1,2,16)],2,function(x)as.numeric(gsub("+","",x))) # cut (+)
Lethality <- dat$TotalDeaths / dat$TotalCases * 100 # add lethality
dat <- cbind(dat,Lethality)
head(dat)
我有这个历史图表,我想用 ggplot 做同样的事情。在图表上指出这些国家。
hist(dat$Lethality,col="green",xlab="Lehtality",main="COVID19 lethality across countries")
points(c(dat$Lethality[dat$Country=="Mexico"],dat$Lethality[dat$Country=="Qatar"],
dat$Lethality[dat$Country=="Venezuela"], dat$Lethality[dat$Country=="Yemen"]),
c(0,0,0, 0),pch=17,col=c("red","blue","brown", "black"),cex=2)
legend("topright",c("Mexico","Qatar","Venezuela","Yemen"),fill=c("red","blue","brown","black"))
这是ggplot2的历史,我不知道如何指出国家和添加图例。请帮我解决这个问题并理解。
ggplot(dat,aes(Lethality,))+
geom_histogram(color="purple", fill="violet")+
theme_bw() +
labs(x = "Lethality",y="Frecuency",
title="Estimated Lethality of Covid-19",
subtitle="Source:https://www.worldometers.info/coronavirus/")
提前致谢。
使用 ggplot2
试试这个。关键是添加 geom_point()
并启用 fill
、color
和 shape
的选项。这里的代码:
library(ggplot2)
#Code
ggplot(dat,aes(Lethality))+
geom_histogram(color="purple", fill="violet")+
geom_point(data=subset(dat,Country %in% c("Mexico","Qatar","Venezuela","Yemen")),
aes(x=Lethality,y=0,color=Country,
shape=Country,fill=Country))+
scale_color_manual(values = c('red','blue','brown','black'))+
scale_fill_manual(values = c('red','blue','brown','black'))+
scale_shape_manual(values = rep(22,4))+
theme_bw() +
labs(x = "Lethality",y="Frecuency",
title="Estimated Lethality of Covid-19",
subtitle="Source:https://www.worldometers.info/coronavirus/")
输出:
如果需要三角形:
#Code 2
ggplot(dat,aes(Lethality))+
geom_histogram(color="purple", fill="violet")+
geom_point(data=subset(dat,Country %in% c("Mexico","Qatar","Venezuela","Yemen")),
aes(x=Lethality,y=0,color=Country,
shape=Country,fill=Country))+
scale_color_manual(values = c('red','blue','brown','black'))+
scale_fill_manual(values = c('red','blue','brown','black'))+
scale_shape_manual(values = rep(24,4))+
theme_bw() +
labs(x = "Lethality",y="Frecuency",
title="Estimated Lethality of Covid-19",
subtitle="Source:https://www.worldometers.info/coronavirus/")
输出:
我的鼻息肉有些问题。我用 base R 做了一个 hist,但现在我想用 ggplot2 做同样的事情。我想在我的历史中指出一些国家。我将把我的数据做一个可重现的例子。这里是:
library(xml2)
library(rvest)
library(dplyr)
library(ggplot2)
url <- "https://www.worldometers.info/coronavirus/"
web <- xml2::read_html(url)
dat <- rvest::html_table(web)[[1]]
names(dat)[1]<-"record"
dat<-dat[-c(1:8,224:231),-c(17:19)]
names(dat)<-c("record","Country","TotalCases","NewCases","TotalDeaths","NewDeaths",
"TotalRecovered","NewRecovered","ActiveCases","SeriosuCritical",
"Cases_M","Deaths_M","TotalTests","Tests_M","Population","Continent")
dat[dat==""]<-NA
dat[-c(1,2,16)]<-apply(dat[-c(1,2,16)],2,function(x)gsub(",","",x)) # cut commas
dat[-c(1,2,16)]<-apply(dat[-c(1,2,16)],2,function(x)as.numeric(gsub("+","",x))) # cut (+)
Lethality <- dat$TotalDeaths / dat$TotalCases * 100 # add lethality
dat <- cbind(dat,Lethality)
head(dat)
我有这个历史图表,我想用 ggplot 做同样的事情。在图表上指出这些国家。
hist(dat$Lethality,col="green",xlab="Lehtality",main="COVID19 lethality across countries")
points(c(dat$Lethality[dat$Country=="Mexico"],dat$Lethality[dat$Country=="Qatar"],
dat$Lethality[dat$Country=="Venezuela"], dat$Lethality[dat$Country=="Yemen"]),
c(0,0,0, 0),pch=17,col=c("red","blue","brown", "black"),cex=2)
legend("topright",c("Mexico","Qatar","Venezuela","Yemen"),fill=c("red","blue","brown","black"))
这是ggplot2的历史,我不知道如何指出国家和添加图例。请帮我解决这个问题并理解。
ggplot(dat,aes(Lethality,))+
geom_histogram(color="purple", fill="violet")+
theme_bw() +
labs(x = "Lethality",y="Frecuency",
title="Estimated Lethality of Covid-19",
subtitle="Source:https://www.worldometers.info/coronavirus/")
提前致谢。
使用 ggplot2
试试这个。关键是添加 geom_point()
并启用 fill
、color
和 shape
的选项。这里的代码:
library(ggplot2)
#Code
ggplot(dat,aes(Lethality))+
geom_histogram(color="purple", fill="violet")+
geom_point(data=subset(dat,Country %in% c("Mexico","Qatar","Venezuela","Yemen")),
aes(x=Lethality,y=0,color=Country,
shape=Country,fill=Country))+
scale_color_manual(values = c('red','blue','brown','black'))+
scale_fill_manual(values = c('red','blue','brown','black'))+
scale_shape_manual(values = rep(22,4))+
theme_bw() +
labs(x = "Lethality",y="Frecuency",
title="Estimated Lethality of Covid-19",
subtitle="Source:https://www.worldometers.info/coronavirus/")
输出:
如果需要三角形:
#Code 2
ggplot(dat,aes(Lethality))+
geom_histogram(color="purple", fill="violet")+
geom_point(data=subset(dat,Country %in% c("Mexico","Qatar","Venezuela","Yemen")),
aes(x=Lethality,y=0,color=Country,
shape=Country,fill=Country))+
scale_color_manual(values = c('red','blue','brown','black'))+
scale_fill_manual(values = c('red','blue','brown','black'))+
scale_shape_manual(values = rep(24,4))+
theme_bw() +
labs(x = "Lethality",y="Frecuency",
title="Estimated Lethality of Covid-19",
subtitle="Source:https://www.worldometers.info/coronavirus/")
输出: