英文:
How to order legend entries chronologically and not by value?
问题
以下是您要翻译的内容:
"The plot (with my minimal reproducible code) shows a quantity that is consumed and to be replenished from time to time.
The data is first grouped by month.
Then, if the quantity is replenished within a month, the data points within that month become a separate group.
The legend entry is the slope of the line for each group.
This results in the legend entries to be in increasing order.
I need the legend entries from top to bottom to follow the calendar order from left to right.
Thank you"
mydata <- read.table(header = TRUE, text = "
data_date data_value
2023-04-20 17
2023-04-24 15
2023-04-30 12
2023-05-03 10
2023-05-08 7
2023-05-13 2
2023-05-14 16
2023-05-17 15
2023-05-24 13
")
# This groups the data by month and by an increase within a month
mydatatable <- data.table(mydata) %>%
mutate(
mnth = format(as.Date(data_date), format="%b"),
jumpup = as.character( cumsum( ifelse( c(-1, diff(mydata$data_value) ) > 0 , 1, 0 ) ) ) ,
month_jumpup = as.factor( paste(mnth, jumpup ) ) ,
data_date = as.POSIXct(data_date, format="%Y-%m-%d", tz="UTC") ) %>%
select(data_date, data_value, month_jumpup)
# Linear regression to extract slope of the line of each group
fm1 <- lmList(data_value ~ data_date | month_jumpup , mydatatable)
# tibble with daily rate/slope per group
ratetib <- tibble(month_jumpup = names(fm1),
rateperday = as.factor( round( -24*3600*summary(fm1)$coef[,1,2], 3 ) ) )
plottib <- tibble(mydatatable) %>%
left_join(ratetib, by = c("month_jumpup" = "month_jumpup"))
gm <- ggplot( plottib , aes( x=data_date, y=data_value, color=rateperday )) +
geom_point() +
geom_smooth( method="lm", se=FALSE)
gm
英文:
The plot (with my minimal reproducible code) shows a quantity that is consumed and to be replenished from time to time.
The data is first grouped by month.
Then, if the quantity is replenishment within a month the data points within that month become a separate group.
The legend entry is the slope of the line for each group.
This results in the legend entries to be in increasing order.
I need the legend entries from top to bottom to follow the calendar order from left to right.
Thank you
mydata <- read.table(header = TRUE, text = "
data_date data_value
2023-04-20 17
2023-04-24 15
2023-04-30 12
2023-05-03 10
2023-05-08 7
2023-05-13 2
2023-05-14 16
2023-05-17 15
2023-05-24 13
")
# This groups the data by month and by an increase within a month
mydatatable <- data.table(mydata) %>%
mutate(
mnth = format(as.Date(data_date), format="%b"),
jumpup = as.character( cumsum( ifelse( c(-1, diff(mydata$data_value) ) > 0 , 1, 0 ) ) ) ,
month_jumpup = as.factor( paste(mnth, jumpup ) ) ,
data_date = as.POSIXct(data_date, format="%Y-%m-%d", tz="UTC") ) %>%
select(data_date, data_value, month_jumpup)
# Linear regression to extract slope of the line of each group
fm1 <- lmList(data_value ~ data_date | month_jumpup , mydatatable)
# tibble with daily rate/slope per group
ratetib <- tibble(month_jumpup = names(fm1),
rateperday = as.factor( round( -24*3600*summary(fm1)$coef[,1,2], 3 ) ) )
plottib <- tibble(mydatatable) %>%
left_join(ratetib, by = c("month_jumpup" = "month_jumpup"))
gm <- ggplot( plottib , aes( x=data_date, y=data_value, color=rateperday )) +
geom_point() +
geom_smooth( method="lm", se=FALSE)
gm
答案1
得分: 2
你可以使用 fct_inorder
重新排序你的因子:
gm <- ggplot( plottib , aes( x=data_date, y=data_value, color=fct_inorder(rateperday) )) +
geom_point() +
geom_smooth( method="lm", se=FALSE)
gm
#> `geom_smooth()` 使用公式 = 'y ~ x'
<!— —>
英文:
You can reorder your factor using fct_inorder
:
gm <- ggplot( plottib , aes( x=data_date, y=data_value, color=fct_inorder(rateperday) )) +
geom_point() +
geom_smooth( method="lm", se=FALSE)
gm
#> `geom_smooth()` using formula = 'y ~ x'
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论