我已經使用日期 x 軸創建了一個 ggplot,但我想從另一個變數而不是日期顯示它們的值。
df
library(tidyverse)
library(lubridate)
df <- read_rds("https://github.com/johnsnow09/covid19-df_stack-code/blob/main/vaccine_milestones.rds?raw=true")
df
Updated.On cr_bin days_to_next_10cr_vacc
<date> <fct> <drtn>
1 2021-04-11 10 Cr 85 days
2 2021-05-27 20 Cr 46 days
3 2021-06-24 30 Cr 28 days
4 2021-07-18 40 Cr 24 days
5 2021-08-06 50 Cr 19 days
6 2021-08-25 60 Cr 19 days
7 2021-09-07 70 Cr 13 days
8 2021-09-18 80 Cr 11 days
9 2021-10-02 90 Cr 14 days
df %>%
ggplot(aes(x = Updated.On, y = days_to_next_10cr_vacc))
geom_col()
scale_x_date(aes(labels = cr_bin))
還試過: scale_x_date(aes(labels = c("10","20","30","40","50","60","70","80","90")))

在 x 軸上的圖中,我希望將cr_bin 中的值顯示為10 Cr, 20 cr, 30 Cr ... so on 90 Cr.
我已經嘗試過上面的代碼,但我不確定用什么來代替標簽來獲得想要的結果
uj5u.com熱心網友回復:
您需要為標簽設定中斷。我正在使用unique,以防萬一可能有重復的行。
還要注意轉換difftime為integer.
library(tidyverse)
library(lubridate)
df <- read_rds("https://github.com/johnsnow09/covid19-df_stack-code/blob/main/vaccine_milestones.rds?raw=true")
df %>%
ggplot(aes(x = Updated.On, y = as.integer(days_to_next_10cr_vacc)))
geom_col()
scale_x_date(breaks = unique(df$Updated.On), labels = unique(df$cr_bin))

由reprex 包(v2.0.1)于 2021 年 10 月 21 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/335794.html
上一篇:將垂直線段添加到動態條形圖ggplot-plotly
下一篇:位置文本條形圖堆疊ggplot2
