This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(ggplot2)
# サンプルデータを作成
data <- data.frame(
Date = as.Date(c("2024-10-01", "2024-10-02", "2024-10-03", "2024-10-04")),
Value = c(3, 12, 5, 8)
)
# 日付の最大値とその次の日付を取得
max_date <- max(data$Date)
next_date <- max_date + diff(range(data$Date)) / (nrow(data) - 1)
# Dの値を取得
d_value <- data$Value[data$Date == max_date] + 0.2
# 棒グラフを作成し、Dの棒を赤枠で囲む
ggplot(data, aes(x = Date, y = Value)) +
geom_bar(stat = "identity") +
geom_rect(aes(
xmin = max_date - diff(range(data$Date)) / (nrow(data) - 1) / 2,
xmax = next_date - diff(range(data$Date)) / (nrow(data) - 1) / 2,
ymin = 0, ymax = d_value),
color = "red", fill = NA, size = 1) +
labs(title = "サンプル棒グラフ", x = "日付", y = "値")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## Warning: Use of `data$Date` is discouraged.
## ℹ Use `Date` instead.
## Use of `data$Date` is discouraged.
## ℹ Use `Date` instead.