gpt4 book ai didi

R 和 ggplot : shade the color of the categorical variable

转载 作者:行者123 更新时间:2023-12-05 03:56:46 26 4
gpt4 key购买 nike

我想制作一个图表来表示一周中每一天每小时发生事件的频率。如何更改星期几的颜色?我想从黑色切换到红色以接近周末

  weekday_hour_pickup <- my_data %>%
mutate(hour_pick = hour(new_pickup))%>%
group_by(hour_pick, weekday) %>%
count() %>%
ggplot(aes(hour_pick, n, color = weekday)) +
geom_line(size = 1.5) +
labs(x = "hour of the day", y = "count")

plot(weekday_hour_pickup)

enter image description here

最佳答案

这是一个很好的选择,可以让你的描述值看起来好像有一个渐变

# call libraries
library(dplyr)
library(ggplot2)

# define weekdays
weekdays <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

# define 7 colours from black to red
colours <- scales::seq_gradient_pal(low = "#000000", high = "#ff0000", space = "Lab")(1:7/7)

# create values for scale colour manual
values <- setNames(colours, weekdays)

# create fake data
data <- tibble(hour_pick = rep(1:24, 7),
n = rnorm(7*24) + rep(1:7, each = 24),

# weekday must be a factor if you want your legend to be sorted!
weekday = factor(rep(weekdays, each = 24), levels = weekdays, ordered = TRUE))

ggplot(data) +
geom_line(aes(x = hour_pick, y = n, colour = weekday)) +
theme_light() +
scale_colour_manual(values = values)

enter image description here

关于R 和 ggplot : shade the color of the categorical variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59090333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com