数据采集

library(RCurl)
## Loading required package: bitops
library(XML)
web<- "https://www.bls.gov/opub/ted/2018/nonfarm-payroll-employment-rose-213000-in-june-2018.htm"
webcode<- getURL(web)
webhtml<- htmlParse(webcode, asText = T)
tables<- readHTMLTable(webhtml , colClasses = c("character", "FormattedNumber"))
tables<- tables[[1]]

数据整理

tables<- tables[c(1,10,18,47,51,69,80,81,88,102,126,148,156,160),]
names(tables)<- c("Industry" , "change")
tables$type<- "down"
index<- tables$change>0
tables[index,]$type<- "up"
tables
##                               Industry change type
## 1                   Mining and logging   4000   up
## 10                        Construction  13000   up
## 18                       Manufacturing  36000   up
## 47                     Wholesale trade   2900   up
## 51                        Retail trade -21600 down
## 69      Transportation and warehousing  15400   up
## 80                           Utilities   -300 down
## 81                         Information      0 down
## 88                Financial activities   8000   up
## 102 Professional and business services  50000   up
## 126      Education and health services  54000   up
## 148            Leisure and hospitality  25000   up
## 156                     Other services  16000   up
## 160                         Government  11000   up

绘图

library(ggplot2)
p<- ggplot(tables, aes(x=Industry , y= change, fill = type))
p+ geom_bar(stat =  "identity") + 
  labs(x= "", y = "", title  = "Nonfarm payroll employment rose 213,000 in June 2018") + 
  theme_bw()+
  theme(legend.position = "top", 
        panel.border = element_blank(), 
        panel.grid.major.x= element_blank(), 
        panel.grid.minor.x = element_blank(),
        axis.line.y = element_line(colour = "black"))  +
  coord_flip()