sb = as.data.frame(Seatbelts)
sb$law = as.factor(sb$law)##THIS CODE WORKS, BUT NO LABELS
RearLaw=subset(sb,law==1)
RearNoLaw=subset(sb,law==0)
ggplot(sb,aes(x=law,y=rear))+geom_boxplot()+labs(x="Seatbelt Laws (1=TRUE)",
y="# of Rear Deaths & Serious Injuries",title="Rear Deaths With & Without Seatbelt Laws")##THIS CODE DOESN'T WORK
ggplot(sb,aes(x=law,y=rear))+geom_boxplot()+labs(x="Seatbelt Laws (1=TRUE)",
y="# of Rear Deaths & Serious Injuries",title="Rear Deaths With & Without Seatbelt Laws")+
geom_text(x=boxplot.stats(RearLaw$rear),label=fivenum(RearLaw$rear))我试图创建一个并排的方框,并将RearLaw和RearNoLaw的统计信息显示在它们各自的框旁,但我一直收到这样的信息:“错误:美学必须与数据(192):x和label相同。”我认为这是因为fivenum生成了5个输出,而我的df有192个条目,但我只想显示这5个输出。上面的代码包括标记RearLaw框(即law=1)的尝试,但是如果可能的话,我也想给RearNoLaw框(law=0)贴上标签。我是一个全新的R,从来没有在这里张贴,所以请告诉我,如果有任何其他信息提供。
发布于 2022-01-31 17:11:29
cyl_fivenum <- sb %>%
group_by(law) %>%
summarise(five = list(fivenum(rear))) %>%
tidyr::unnest()
ggplot(sb, aes(x = law, y = rear)) +
geom_boxplot()+
labs(x="Seatbelt Laws (1=TRUE)",
y="# of Rear Deaths & Serious Injuries",title="Rear Deaths With & Without Seatbelt Laws") +
geom_text(data = cyl_fivenum,
aes(x = factor(law), y = five, label = five),
nudge_x = .5)

https://stackoverflow.com/questions/70929840
复制相似问题