我想使用ggplot2
生成的直方圖的背景着色。我想要的背景爲look like the one in the answer here。使用帶geom_histogram的geom_rect
這是我的代碼:
dates <- seq(from = as.Date("2015/1/1"), to = as.Date("2015/12/31"), "day")
library(lubridate)
day <- yday(dates)
month <- month(dates)
df <- data.frame(day, month)
library(dplyr)
df %>%
sample_n(50) ->
df
library(ggplot2)
ggplot(df, aes(day)) + geom_histogram() +
scale_x_continuous(breaks = seq(0, 365, 10), limits = c(0, 365)) +
theme_bw()
將會產生這樣的情節:
而這正是我想,這不工作:
ggplot(df, aes(day)) + geom_histogram() +
geom_rect(xmin = day, xmax = day, ymin = -Inf, ymax = Inf, fill = month) +
scale_x_continuous(breaks = seq(0, 365, 10), limits = c(0, 365)) +
theme_bw()
答案是很清楚的,你需要做什麼。它的哪一部分很難理解? –
你看過你的問題的其他答案嗎? http://stackoverflow.com/questions/31510796/shade-background-of-ggplot-according-to-month/31511456#31511456 – Heroka
@RomanLuštrik如果你看鏈接答案中的'rects','xstart'和' geom_rect的xend參數都有自己的變量。但是在我創建的'df'中,我只有一個變量'day',它被'geom_histogram'轉換爲bin,所以不清楚'xmin'和'xmax'應該是什麼值。 – luciano