R 基于截止值的几何点颜色

R 基于截止值的几何点颜色,r,ggplot2,R,Ggplot2,我尝试使用ggplot创建带有数据点的线图。我想根据基于四分位数的截止值分配点的颜色。geom_point没有从摘要统计数据中绘制任何点,并发出警告消息“删除了包含缺失值的行”。同样的代码明确包含数值(不是从摘要统计计算中),绘制工作做得很好。如果您能提出解决问题的方法,我将不胜感激。以下是可复制代码: patient <- cbind.data.frame(seq(1:14),matrix(sample(1:100, 84), ncol=6)) colnames(patient) <

我尝试使用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"))

患者这就是你想要的吗

library(tidyverse)
patient %>%
    mutate(Quartile = cut(
        SLEEP,
        c(-Inf, quantile(SLEEP)[2], quantile(SLEEP)[3], quantile(SLEEP)[4], Inf),
        labels = c("1st", "2nd", "3rd", "4th"))) %>%
    ggplot(aes(DAYS, SLEEP, colour = Quartile)) +
    geom_point(show.legend = F, size = 3) +
    geom_line(aes(colour = Quartile)) +
    scale_colour_manual(values = c(
        "1st" = "blue",
        "2nd" = "green",
        "3rd" = "orange",
        "4th" = "red")) +
    theme(
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        panel.background = element_blank(),
        plot.title = element_text(size = 8, face = "bold")) +
    guides(fill = FALSE) +
    ggtitle("SLEEP (hrs)")
查看切割(…)的输出:

因此,ggplot希望这些值以scale_color表示:

values = c('(-Inf,22.8]' = 'green', '(22.8,62.5]' = 'orange', '(62.5, Inf]' = 'red')
但是你不需要通过等级,只需要按照相应的顺序通过颜色:

values = c('green', 'orange', 'red')
您也不需要所有重复的
主题
行:

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() )

它的可能副本绝对有用。谢谢@Carlos Eduardoth这是一个有趣的情节设计方式。谢谢,毛里蒂斯
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() )