d <- data.frame(a = c(5, 8, 4, 9),
b = c(1, 3, 2, 5),
c = c(9, 8, 5, 1))
rownames(d) <- c("2023年5月",
"2023年6月",
"2023年7月",
"2023年8月")
colnames(d) <- c("カレーライス",
"ハヤシライス",
"タコライス")
library(kableExtra)
kable(d, caption = "売上金額[百万円]")
売上金額[百万円]
| 2023年5月 |
5 |
1 |
9 |
| 2023年6月 |
8 |
3 |
8 |
| 2023年7月 |
4 |
2 |
5 |
| 2023年8月 |
9 |
5 |
1 |
COL <- c("pink", "purple", "lightblue")
barplot(t(as.matrix(d)), col = COL,
main = "会計月別売上金額[百万円]",
xlab = "月",
ylab = "売上金額[百万円]")
# 格子線の追加
abline(h = seq(0, 20, 5), lty = 2, col = gray(0.5, 0.5))
# 凡例の追加
legend("topleft", fill = COL, legend = colnames(d))

library(ggplot2)
d_long <- data.frame(
Month = rep(rownames(d), times = ncol(d)),
Product = rep(colnames(d), each = nrow(d)),
Sales = as.vector(as.matrix(d))
)
d_long$Month <- factor(d_long$Month, levels = rownames(d))
p <- ggplot(d_long, aes(x = Month, y = Sales, group = Product, color = Product)) +
geom_line() +
geom_point() +
ggtitle("時系列グラフ") +
xlab("月") +
ylab("売上金額[百万円]") +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
legend.title = element_blank()
) +
scale_color_manual(values = c("pink", "purple", "lightblue"))
# グリッドの追加
p <- p + theme(panel.grid.major = element_line(color = "gray", linetype = "dashed"))
# グラフの表示
print(p)

hayashi <- d$ハヤシライス
taco <- d$タコライス
# 図枠の作成
plot(hayashi, taco, type = 'n',
main = 'ハヤシライスとタコライスの相関図',
xlab = 'ハヤシライスの売上[百万円]',
ylab = 'タコライスの売上[百万円]')
# 格子線の追加
abline(h = seq(0, max(taco) + 1, by = 1),
v = seq(0, max(hayashi) + 1, by = 1),
lty = 2, col = gray(0.5, 0.5))
# 作図
points(hayashi, taco, pch = 16, col = 'pink')
# 凡例の追加
legend('topright', pch = 16, col = 'pink', legend = 'ハヤシライス vs タコライス')
