基于截止值的geom_point颜色

时间:2018-07-05 05:43:28

标签: r ggplot2

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

谢谢

2 个答案:

答案 0 :(得分:1)

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

enter image description here

答案 1 :(得分:0)

这就是你的追求吗?

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

enter image description here