我正在尝试用geom_segment()行绘制一个点图,以显示两种不同的COVID感染率:监狱工作人员和监狱居民。我对每个州都有观察,并希望每个州产生两个geom_segments和两个geom_point。从图中可以看到,我在努力处理两个不同的部分:1)我希望每个州有两条线(因此,俄亥俄州的两条线,居民的一条线和员工的一条线,彼此相邻)。有谁知道如何做到这一点?position_dodge似乎只移动geom_points,不会创建两行。2)我只想根据监狱居民感染计数来命令各州。当前,reorder(State,Count)按居民和工作人员感染的总数进行排序。
这是我当前正在运行的代码(rdt =数据集,Count =感染数,State =观察组,Type =人员/居民感染数-数据存储时间长):
ggplot(rdt,aes(x=Count,y=reorder(State,Count),
colour=Type))+
geom_segment(aes(yend=State),xend=0,colour="grey50",
position=position_dodge(width=2))+
geom_point(size=3,aes(colour=Type),position=position_dodge(width=2))+
scale_color_brewer(palette="Set1")+
theme_bw()+
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank())
感谢您提供的任何帮助或见解。
这是dput
我的数据集中前十行的一些示例输出
structure(list(State = structure(1:10, .Label = c("Alabama",
"Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut",
"Delaware", "District of Columbia", "Federal", "Florida", "Georgia",
"Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky",
"Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan",
"Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska",
"Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York",
"North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon",
"Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington",
"West Virginia", "Wisconsin", "Wyoming"), class = "factor"),
Date.R = structure(c(18403, 18403, 18403, 18403, 18403, 18403,
18403, 18403, 18403, 18403), class = "Date"), Type = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("General.Confirmed",
"Residents.Confirmed"), class = "factor"), Count = c(9, 4,
155, 1246, 959, 486, 689, 147, 187, 4724), Population = c(21154,
5143, 42282, 15593, 118431, 17621, 12218, 4053, 1790, 157340
), Ratio = c(0.000425451451262173, 0.000777756173439627,
0.00366586254198004, 0.0799076508689797, 0.00809754202869181,
0.0275807275410022, 0.0563922082173842, 0.0362694300518135,
0.104469273743017, 0.0300241515190034), Ratio100 = c(42.5451451262173,
77.7756173439627, 366.586254198004, 7990.76508689797, 809.754202869181,
2758.07275410022, 5639.22082173842, 3626.94300518135, 10446.9273743017,
3002.41515190034)), .Names = c("State", "Date.R", "Type",
"Count", "Population", "Ratio", "Ratio100"), row.names = c(NA,
10L), class = "data.frame")
编辑:这是最终代码(对变量进行了一些更改,对不起):
rdt$Ratio <- rdt$Count/rdt$Population
rdt$Ratio100 <- rdt$Ratio*100000
rdt$State<- factor(rdt$State,unique(rdt$State)) #Starts graphing process
# switch general and residents around to order by other group
rdt$Type <- factor(rdt$Type,c("General.Confirmed","Residents.Confirmed"))
ggplot(rdt,aes(y=Ratio100,x=reorder(State,Ratio100),colour=Type))+
geom_pointrange(aes(ymin =0,ymax=Ratio100),
position=position_dodge(width=.7),size=2,fatten=1)+
scale_color_brewer(palette="Set1",
name = "Unit of Observation", labels = c(
"US Population", "Prison Residents"))+
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank()) +
labs(title="COVID-19 Infection Rates for US General and Prison Population",
caption="Data from UCLA COVID-19 Behind Bars Data Project",
y="Infection Rate Per 100,000 People",
x="State")+
scale_y_continuous(sec.axis=dup_axis())+
theme_bw()+
coord_flip()
我最近遇到了一个类似的问题,经过大量的实验,我发现使用geom_pointrange和coord_flip似乎对我有用。
rdt$State<- factor(rdt$State,unique(rdt$State))
# switch staff and residents around to order by other group
rdt$Type <- factor(rdt$Type,c("Staff.Confirmed","Residents.Confirmed"))
ggplot(rdt,aes(y=Count,x=reorder(State,Count),colour=Type))+
geom_point(size=3,position=position_dodge(width=0.2))+
geom_pointrange(aes(ymin =0,ymax=Count),position=position_dodge(width=0.2))+
scale_color_brewer(palette="Set1")+
theme_bw()+
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank()) +
coord_flip()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句