R-ggplot。如何仅绘制变量>;0?
我正在绘制我省城镇中的covid19 PCR数量。问题是许多城镇的PCR检测都没有阳性结果。我需要一种方法来绘制至少有1+PCR的城镇 这是我的代码:R-ggplot。如何仅绘制变量>;0?,r,ggplot2,R,Ggplot2,我正在绘制我省城镇中的covid19 PCR数量。问题是许多城镇的PCR检测都没有阳性结果。我需要一种方法来绘制至少有1+PCR的城镇 这是我的代码: library(tidyverse) library('data.table') dfcsv1 <- read.csv("https://dadesobertes.gva.es/datastore/dump/ee17a346-a596-4866-a2ac-a530eb811737?bom=True",
library(tidyverse)
library('data.table')
dfcsv1 <- read.csv("https://dadesobertes.gva.es/datastore/dump/ee17a346-a596-4866-a2ac-a530eb811737?bom=True",
encoding = "UTF-8", header = TRUE, sep = ",")
colnames(dfcsv1) <- c("code","code2","Municipio", "PCR", "TasaPCR", "PCR14",
"TasaPCR14", "Muertos", "TasaMuertos")
dfcsv1$TasaMuertos = as.numeric(gsub(",","\\.",dfcsv1$TasaMuertos))
dfcsv1$TasaPCR = as.numeric(gsub(",","\\.",dfcsv1$TasaPCR))
dfcsv1$TasaPCR14 = as.numeric(gsub(",","\\.",dfcsv1$TasaPCR14))
dfcsv1 %>%
mutate(Municipio = fct_reorder(Municipio, PCR14)) %>%
ggplot(aes(x=Municipio, y=PCR14, fill =TasaPCR14)) +
geom_bar(stat="identity", width=0.6) +
coord_flip() +
geom_text(data=dfcsv1, aes(y=PCR14,label=PCR14),vjust=1)+
scale_fill_gradient(low="steelblue", high="red")
库(tidyverse)
库('data.table')
dfcsv1%
ggplot(aes(x=市政,y=PCR14,fill=TasaPCR14))+
几何图形条(stat=“identity”,宽度=0.6)+
coord_flip()+
geom_text(数据=dfcsv1,aes(y=PCR14,标签=PCR14),vjust=1)+
比例填充梯度(低=“钢蓝”,高=“红色”)
正如其他人在评论中所说,在重新排序因子级别之前,您需要过滤掉大于0的PCR14
。但是,您还需要从geom_text
中删除数据
参数,否则所有这些因子级别都会返回,您将陷入一片混乱。它已经有点拥挤了,零级被移除了
我认为您还应该将vjust
更改为hjust
,以便将文本放置在更好的位置,因为您已经翻转了坐标,并且(翻转的)y轴范围会进行补偿增加以适应它:
dfcsv1 %>%
filter(PCR14 > 0) %>%
mutate(Municipio = fct_reorder(Municipio, PCR14)) %>%
ggplot(aes(x = Municipio, y = PCR14, fill = TasaPCR14)) +
geom_bar(stat = "identity", width = 0.6) +
coord_flip() +
geom_text(aes(y = PCR14,label = PCR14), hjust= -0.5) +
scale_fill_gradient(low = "steelblue", high = "red") +
ylim(c(0, 45))
顺便说一句,去掉那些看起来也好多了:
dfcsv1 %>%
filter(PCR14 > 1) %>%
mutate(Municipio = fct_reorder(Municipio, PCR14)) %>%
ggplot(aes(x=Municipio, y=PCR14, fill =TasaPCR14)) +
geom_bar(stat="identity", width=0.6) + coord_flip() +
geom_text(aes(y=PCR14,label=PCR14),hjust=-0.5)+
scale_fill_gradient(low="steelblue", high="red") +
ylim(c(0, 45))
正如其他人在评论中所说,在重新排序因子级别之前,您需要过滤掉大于0的
PCR14
。但是,您还需要从geom_text
中删除数据
参数,否则所有这些因子级别都会返回,您将陷入一片混乱。它已经有点拥挤了,零级被移除了
我认为您还应该将vjust
更改为hjust
,以便将文本放置在更好的位置,因为您已经翻转了坐标,并且(翻转的)y轴范围会进行补偿增加以适应它:
dfcsv1 %>%
filter(PCR14 > 0) %>%
mutate(Municipio = fct_reorder(Municipio, PCR14)) %>%
ggplot(aes(x = Municipio, y = PCR14, fill = TasaPCR14)) +
geom_bar(stat = "identity", width = 0.6) +
coord_flip() +
geom_text(aes(y = PCR14,label = PCR14), hjust= -0.5) +
scale_fill_gradient(low = "steelblue", high = "red") +
ylim(c(0, 45))
顺便说一句,去掉那些看起来也好多了:
dfcsv1 %>%
filter(PCR14 > 1) %>%
mutate(Municipio = fct_reorder(Municipio, PCR14)) %>%
ggplot(aes(x=Municipio, y=PCR14, fill =TasaPCR14)) +
geom_bar(stat="identity", width=0.6) + coord_flip() +
geom_text(aes(y=PCR14,label=PCR14),hjust=-0.5)+
scale_fill_gradient(low="steelblue", high="red") +
ylim(c(0, 45))
作为一般规则,无论是绘图类型还是使用的是
ggplot
、lattice
还是基本plot
函数,都应首先进行子集设置
plot(x[y>0] , y[y>0])
剩下的是美学。作为一般规则,无论您使用的是绘图类型,还是使用的是
ggplot
、lattice
还是基本plot
函数,都应首先进行子集设置
plot(x[y>0] , y[y>0])
剩下的就是美学。在
mutate()之前可以尝试dfcsv1%>%filter(PCR14>0)%>%
在mutate
之后和ggplot
之前添加一个过滤器
?