浏览器和操作系统份额

浏览器和操作系统份额

本文是学习zonination/w3data的笔记,对浏览器和操作系统的市场份额的时间序列数据进行可视化。

绘图所需的数据集为:
browser.csv
os.csv

绘图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
setwd("~/Desktop/浏览器和操作系统份额")
library(readr)
library(reshape2)
library(ggplot2)
library(RColorBrewer)
library(scales)

browser <- read_csv("browser.csv")
# 添加日期变量
# 因为我的电脑系统语言是中文的(这个时候%B格式的月份对应的是中文的月份),所以需要把英文月份换成数字
browser$month[which(browser$month == "January")] = 1
browser$month[which(browser$month == "December")] = 12
browser$month[which(browser$month == "November")] = 11
browser$month[which(browser$month == "October")] = 10
browser$month[which(browser$month == "September")] = 9
browser$month[which(browser$month == "August")] = 8
browser$month[which(browser$month == "July")] = 7
browser$month[which(browser$month == "June")] = 6
browser$month[which(browser$month == "May")] = 5
browser$month[which(browser$month == "April")] = 4
browser$month[which(browser$month == "March")] = 3
browser$month[which(browser$month == "February")] = 2
browser$date <- as.Date(paste(formatC(browser$month, width = 2, flag = "0"), browser$year, "01"), "%b %Y %e")
# 宽变长
browser <- melt(browser, id = c("month", "year", "date"))
browser <- browser[,-1:-2]

# 因子排序
browser$variable <- factor(
browser$variable,
levels = c(
"IE", "AOL", "Netscape", "Mozilla",
"Firefox", "Opera", "Safari", "Chrome"
)
)

# 绘图
p <- ggplot(browser, aes(date, value)) +
geom_area(position = 'fill',
aes(fill = variable),
colour = "black",
alpha = 0.75) +
scale_fill_manual("Browser", values = c(
# rev()函数可以将元素反序排列
rev(brewer.pal(3, "Blues")[2:3]),
brewer.pal(3, "Greens"),
brewer.pal(9, "Set1")[c(1, 5, 6)])
) +
scale_y_continuous(labels = scales::percent_format()) +
labs(title = "浏览器市场份额",
subtitle = "数据来源:w3schools.com",
x = "",
y = "市场份额") +
theme_bw(base_size = 15, base_family = "STSong") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.subtitle = element_text(hjust = 0.5))
print(p)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 操作系统市场
os <- read_csv("os.csv")
os$month[which(os$month == "January")] = 1
os$month[which(os$month == "December")] = 12
os$month[which(os$month == "November")] = 11
os$month[which(os$month == "October")] = 10
os$month[which(os$month == "September")] = 9
os$month[which(os$month == "August")] = 8
os$month[which(os$month == "July")] = 7
os$month[which(os$month == "June")] = 6
os$month[which(os$month == "May")] = 5
os$month[which(os$month == "April")] = 4
os$month[which(os$month == "March")] = 3
os$month[which(os$month == "February")] = 2
os$date <- as.Date(paste(formatC(os$month, width = 2, flag = "0"), os$year, "01"), "%b %Y %e")

os <- melt(os, id = c("month", "year", "date"))
os <- os[,-1:-2]
os$variable <- factor(os$variable, levels =
c(
"Win10", "Win8", "Win7", "Vista",
"WinXP", "W2000", "Win98", "Win95",
"W2003", "NT", "Mac", "Linux",
"Chrome OS", "Mobile"
))

ggplot(os, aes(date, value)) +
geom_area(position = "fill",
aes(fill = variable),
colour = "black",
alpha = 0.75) +
scale_fill_manual("操作\n系统",
values = c(
rev(brewer.pal(8, "Blues")),
rev(brewer.pal(3, "Greens")[2:3]),
brewer.pal(3, "Purples")[3],
rev(brewer.pal(3, "Oranges")[2:3]),
brewer.pal(3, "Greys")[2]
),
labels = c(
"Windows 10", "Windows 8", "Windows 7",
"Windows Vista", "Windows XP", "Windows 2000",
"Windows 98", "Windows 95", "Windows 2003",
"Windows NT*", "Macintosh", "Linux",
"Chrome OS", "Mobile"
)) +
scale_y_continuous(labels = scales::parse_format()) +
labs(title = "操作系统市场份额",
subtitle = "数据来源:w3schools.com",
x = "",
y = "市场份额",
caption = "注:2012年之后,\"Windows NT\"指所有的Windows操作系统") +
theme_bw(base_size = 15, base_family = "STSong") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.subtitle = element_text(hjust = 0.5))

# R

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×