读取数据
library(XML)
library(RCurl)
## Loading required package: bitops
web<-"https://www.bls.gov/opub/ted/2018/69-percent-of-private-industry-workers-had-access-to-medical-care-benefits-in-march-2018.htm"
webcode<- getURL(web,.encoding = "UTF-8")
#转换类型
webhtml<- htmlParse(webcode, asText = T)
tables<- readHTMLTable(webhtml, colClasses = c("character" , "numeric" ))
## Warning in asMethod(object): 强制改变过程中产生了NA
tables<- tables[[1]]
names(tables)<- c("characteristic", "percent")
tables
## characteristic percent
## 1 All private industry workers NA
## 2 Small establishments (1 to 99 workers) 55
## 3 Medium-size establishments (100 to 499 workers) 83
## 4 Large establishments (500 or more workers) 88
## 5 Goods-producing industries 85
## 6 Service-providing industries 66
## 7 Full-time workers 86
## 8 Part-time workers 21
## 9 Union workers 94
## 10 Nonunion workers 66
整理数据
tables<-tables[-1,]
names(tables)<- c("characteristic", "percent")
tables
## characteristic percent
## 2 Small establishments (1 to 99 workers) 55
## 3 Medium-size establishments (100 to 499 workers) 83
## 4 Large establishments (500 or more workers) 88
## 5 Goods-producing industries 85
## 6 Service-providing industries 66
## 7 Full-time workers 86
## 8 Part-time workers 21
## 9 Union workers 94
## 10 Nonunion workers 66
绘图
library(ggplot2)
p<- ggplot(tables, aes(x = reorder(characteristic,percent), y = percent))
p<- p+ geom_bar(stat = "identity", fill = "steelblue", width = 0.5)
p<- p+ coord_flip()
p<- p+ theme_bw()
p+ theme(panel.border = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank(),
axis.ticks = element_blank(),
axis.line.y = element_line(colour = "black")) +
xlab("") +
ylab("Percentage \n (%)") +
scale_y_continuous(expand = c(0,0))+
ggtitle("69 percent of private industry workers had access \nto medical care benefits in March 2018")
