如何在 x 軸上顯示所有值?在我的示例中,我只能在 x 軸上看到 Jan2021、July 2021 和 Jan 2022。我想顯示“B 月”列中的所有月份。
Side question: What is the best method of handling column names with spaces in them?
Most of the time, using `Column Name` or as.name("Column Name") works, but sometimes I get an error when I use that.
library(tidyverse)
library(zoo) #for as.yearmon function
df3 <- structure(list(`Month B` = structure(c(2020.75, 2020.83333333333,
2020.91666666667, 2021, 2021.08333333333, 2021.16666666667, 2021.25,
2021.33333333333, 2021.41666666667, 2021.5, 2021.58333333333,
2021.66666666667, 2021.75, 2021.83333333333, 2021.91666666667,
2022, 2022.08333333333, 2022.16666666667), class = "yearmon"),
colA = c(777, 1111, 888, 999, 999, 1500, 1400, 1000, 1200,
999, 1200, 900, 1200, 1300, 777, 1200, 777, 900), colB = c(700,
800, 900, 700, 800, 900, 600, 500, 500, 600, 500, 500, 600,
111, 999, 999, 888, 777)), row.names = c(NA, -18L), class = c("tbl_df",
"tbl", "data.frame"))
df3$`Month B` <- as.yearmon(df3$`Month B`)
ggplot(df3, aes(`Month B`))
geom_line(aes(y = `colA`, colour = "colA"))
geom_line(aes(y = `colB`, colour = "colB"))
theme(axis.text.x = element_text(angle = 90)) ggtitle("MyTitle")
uj5u.com熱心網友回復:
這是一個建議:
而不是yearmon()我在這里使用dmy來自 lubridate 的函式并將其應用到 ggplot 中scale_x_date:
library(lubridate)
library(tidyverse)
df3 %>%
mutate(`Month B`=dmy(paste("01", as.character(`Month B`)))) %>%
ggplot(aes(`Month B`))
geom_line(aes(y = `colA`, colour = "colA"))
geom_line(aes(y = `colB`, colour = "colB"))
scale_x_date(date_labels="%b %y",date_breaks ="1 month")
theme(axis.text.x = element_text(angle = 90)) ggtitle("MyTitle")

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/460972.html
上一篇:R:擬合S形曲線?
下一篇:一幀中有多個小提琴情節可能嗎?
