我尝试使用 ggplot 创建带有数据点的线图。我想根据基于四分位数的截止值分配点的颜色。geom_point 没有从汇总统计中绘制任何点并发出警告消息“删除了包含缺失值的行”。显式包含数值的相同代码(不是来自汇总统计计算),绘图做得很好。感谢您是否可以建议解决问题的方法。以下是可重现的代码:
patient <- cbind.data.frame(seq(1:14),matrix(sample(1:100, 84), ncol=6))
colnames(patient) <- c('DAYS', 'PHYSICAL_ACTIVITY', 'SMOKING', 'ALCOHOL_INTAKE', 'HYDRATION', 'SLEEP', 'Total_score')
ggplot(data=patient, aes(x=DAYS,y=SLEEP)) +
geom_line(colour='black', size=1) +
geom_point(size=3,aes(colour=cut(SLEEP, c(-Inf,summary(SLEEP)[[2]],summary(SLEEP)[[5]],Inf))), show.legend=F) +
scale_color_manual(values = c("(-Inf,summary(SLEEP)[[2]]]" = "green", "(summary(SLEEP)[[2]],summary(SLEEP)[[5]]]" = "orange", "(summary(SLEEP)[[5]], Inf]" = "red")) +
theme(axis.title.y=element_blank()) +
theme(axis.title.x=element_blank(), axis.text.x=element_blank(),axis.ticks.x=element_blank()) +
ggtitle("SLEEP (hrs)")+ theme(panel.background = element_blank()) +
guides(fill=FALSE)+ theme(plot.title = element_text(size = 8, face = "bold"))
谢谢
查看 cut(...) 的输出:
> cut(patient$SLEEP, c(-Inf, summary(patient$SLEEP)[[2]], summary(patient$SLEEP)[[5]], Inf))
[1] (62.5, Inf] (22.8,62.5] (62.5, Inf] (62.5, Inf] (-Inf,22.8] (22.8,62.5]
[7] (-Inf,22.8] (22.8,62.5] (-Inf,22.8] (22.8,62.5] (62.5, Inf] (-Inf,22.8]
[13] (22.8,62.5] (22.8,62.5]
Levels: (-Inf,22.8] (22.8,62.5] (62.5, Inf]
所以 ggplot 期望 scale_color 中的这些值:
values = c('(-Inf,22.8]' = 'green', '(22.8,62.5]' = 'orange', '(62.5, Inf]' = 'red')
但是您不需要通过级别,只需按照对应顺序传递颜色:
values = c('green', 'orange', 'red')
您也不需要所有这些重复的theme
行:
ggplot(patient, aes(DAYS, SLEEP)) +
geom_line() +
geom_point(
aes(colour = cut(SLEEP, c(-Inf, summary(SLEEP)[[2]], summary(SLEEP)[[5]], Inf))),
size = 3, show.legend = FALSE ) +
scale_color_manual(values = c('green', 'orange', 'red')) +
labs(title = 'SLEEP (hrs)', x = NULL, y = NULL) +
theme_minimal() +
theme(
plot.title = element_text(size = 8, face = 'bold'),
panel.grid = element_blank() )
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句