簡體   English   中英

R並排箱線圖

[英]R Side-by-Side Boxplot

我敢肯定,對於大多數人來說,這是一個非常簡單的問題,但是我是新手,無法解決。 如何創建按時間分組的並排箱圖? 例如,我有24個月的數據。 我想在前12個月中繪制一個箱形圖,然后在接下來的12個月中繪制另一個箱形圖。 我的數據如下所示。

Month,Revenue
1,94000
2,81000
3,117000
4,105000
5,117000
6,89000
7,101000
8,118000
9,105000
10,123000
11,109000
12,89000
13,106000
14,159000
15,121000
16,135000
17,116000
18,133000
19,144000
20,130000
21,142000
22,124000
23,140000
24,104000

由於您的數據具有時間順序,因此按年度分別按月繪制折線圖可能很有啟發。 這是折線圖和箱線圖的代碼。 我只是在下面的代碼中組成了年值,但是您可以根據需要進行設置:

library(ggplot2)

# Assuming your data frame is called "dat"
dat$Month.abb = month.abb[rep(1:12,2)]
dat$Month.abb = factor(dat$Month.abb, levels=month.abb)
dat$Year = rep(2014:2015, each=12)

ggplot(dat, aes(Month.abb, Revenue, colour=factor(Year))) +
  geom_line(aes(group=Year)) + geom_point() +
  scale_y_continuous(limits=c(0,max(dat$Revenue))) +
  theme_bw() +
  labs(colour="Year", x="Month")

ggplot(dat, aes(factor(Year), Revenue)) +
  geom_boxplot() +
  scale_y_continuous(limits=c(0,max(dat$Revenue))) +
  theme_bw() +
  labs(x="Year")

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM