1导入数据
代码语言:javascript复制library(reshape2)
> p<-file.choose()
> df<-read.csv(p,sep=",",na.strings="NA",stringsAsFactors=FALSE)
> df
City Female Male
1 Acapulco 2565.51 2595.80
2 Bellingham 453.36 539.27
3 Beverly Hills 5050.46 5269.71
4 Bremerton 5269.89 5705.00
5 Camacho 3643.30 2154.15
6 Guadalajara 290.99 232.33
7 Hidalgo 7361.04 3951.73
8 Los Angeles 6014.64 6281.53
9 Merida 4770.14 3970.31
10 Mexico City 1255.17 1233.14
11 Orizaba 3531.85 2713.37
12 Portland 5612.43 6314.17
13 Salem 9416.67 8795.17
14 San Andres 3458.54 4747.51
15 San Diego 5800.65 5569.12
16 San Francisco 392.54 351.76
17 Seattle 6561.90 5590.49
18 Spokane 5835.68 5398.36
19 Tacoma 8467.80 8363.35
20 Vancouver 3973.95 4691.05
21 Victoria 1151.91 1250.12
22 Walla Walla 488.42 676.18
23 Yakima 2158.89 2911.08
2对数据求和
代码语言:javascript复制 df$sum<-rowSums(df[,2:3])
代码语言:javascript复制 City Female Male sum
1 Acapulco 2565.51 2595.80 5161.31
2 Bellingham 453.36 539.27 992.63
3 Beverly Hills 5050.46 5269.71 10320.17
4 Bremerton 5269.89 5705.00 10974.89
5 Camacho 3643.30 2154.15 5797.45
6 Guadalajara 290.99 232.33 523.32
7 Hidalgo 7361.04 3951.73 11312.77
8 Los Angeles 6014.64 6281.53 12296.17
9 Merida 4770.14 3970.31 8740.45
10 Mexico City 1255.17 1233.14 2488.31
11 Orizaba 3531.85 2713.37 6245.22
12 Portland 5612.43 6314.17 11926.60
13 Salem 9416.67 8795.17 18211.84
14 San Andres 3458.54 4747.51 8206.05
15 San Diego 5800.65 5569.12 11369.77
16 San Francisco 392.54 351.76 744.30
17 Seattle 6561.90 5590.49 12152.39
18 Spokane 5835.68 5398.36 11234.04
19 Tacoma 8467.80 8363.35 16831.15
20 Vancouver 3973.95 4691.05 8665.00
21 Victoria 1151.91 1250.12 2402.03
22 Walla Walla 488.42 676.18 1164.60
23 Yakima 2158.89 2911.08 5069.97
3对数据根据sum进行排序
根据sum排序,必须用factor()中的levels指定City的次序,不然默认就是字母顺序
代码语言:javascript复制df2<-dplyr::arrange(df,sum)
代码语言:javascript复制 df2$City<-factor(df2$City,levels=df2$City)
代码语言:javascript复制> df2
City Female Male sum
1 Guadalajara 290.99 232.33 523.32
2 San Francisco 392.54 351.76 744.30
3 Bellingham 453.36 539.27 992.63
4 Walla Walla 488.42 676.18 1164.60
5 Victoria 1151.91 1250.12 2402.03
6 Mexico City 1255.17 1233.14 2488.31
7 Yakima 2158.89 2911.08 5069.97
8 Acapulco 2565.51 2595.80 5161.31
9 Camacho 3643.30 2154.15 5797.45
10 Orizaba 3531.85 2713.37 6245.22
11 San Andres 3458.54 4747.51 8206.05
12 Vancouver 3973.95 4691.05 8665.00
13 Merida 4770.14 3970.31 8740.45
14 Beverly Hills 5050.46 5269.71 10320.17
15 Bremerton 5269.89 5705.00 10974.89
16 Spokane 5835.68 5398.36 11234.04
17 Hidalgo 7361.04 3951.73 11312.77
18 San Diego 5800.65 5569.12 11369.77
19 Portland 5612.43 6314.17 11926.60
20 Seattle 6561.90 5590.49 12152.39
21 Los Angeles 6014.64 6281.53 12296.17
22 Tacoma 8467.80 8363.35 16831.15
23 Salem 9416.67 8795.17 18211.84
4绘制散点图
代码语言:javascript复制> ggplot(df2,aes(City,sum))
geom_point(shape=21,size=3,colour="black",fill="#FC4E07")
geom_segment(aes(x=City,xend=City,y=0,yend=sum))
coord_flip()
> theme(axis.title=element_text(size=13,face="plain",color="black"),
axis.text = element_text(size=10,face="plain",color="black"),
legend.title=element_text(size=14,face="plain",color="black"))