我想知道在ggplot中使用break还是之前创建它们是否更好。我的问题是试图用这个创建自定义颜色。我想用紫色刻度。我可以让它在没有休息时看起来很棒,但在这种情况下加利福尼亚和德克萨斯州占主导地位
由于我无法附加文件:这是statepop.csv
的statd内容> statd
Rank State Population X
1 1 California 38802500 NA
2 2 Texas 26956958 NA
3 3 Florida 19893297 NA
4 4 New York 19746227 NA
5 5 Illinois 12880580 NA
6 6 Pennsylvania 12787209 NA
7 7 Ohio 11594163 NA
8 8 Georgia 10097343 NA
9 9 North Carolina 9943964 NA
10 10 Michigan 9909877 NA
11 11 New Jersey 8938175 NA
12 12 Virginia 8326289 NA
13 13 Washington 7061530 NA
14 14 Massachusetts 6745408 NA
15 15 Arizona 6731484 NA
16 16 Indiana 6596855 NA
17 17 Tennessee 6549352 NA
18 18 Missouri 6063589 NA
19 19 Maryland 5976407 NA
20 20 Wisconsin 5757564 NA
21 21 Minnesota 5457173 NA
22 22 Colorado 5355856 NA
23 23 Alabama 4849377 NA
24 24 South Carolina 4832482 NA
25 25 Louisiana 4649676 NA
26 26 Kentucky 4413457 NA
27 27 Oregon 3970239 NA
28 28 Oklahoma 3878051 NA
29 29 Connecticut 3596677 NA
30 30 Iowa 3107126 NA
31 31 Arkansas 2994079 NA
32 32 Mississippi 2984926 NA
33 33 Utah 2942902 NA
34 34 Kansas 2904021 NA
35 35 Nevada 2839099 NA
36 36 New Mexico 2085572 NA
37 37 Nebraska 1881503 NA
38 38 West Virginia 1850326 NA
39 39 Idaho 1634464 NA
40 40 Hawaii 1419561 NA
41 41 Maine 1330089 NA
42 42 New Hampshire 1326813 NA
43 43 Rhode Island 1055173 NA
44 44 Montana 1023579 NA
45 45 Delaware 935614 NA
46 46 South Dakota 853175 NA
47 47 North Dakota 739482 NA
48 48 Alaska 737732 NA
49 49 Vermont 626011 NA
50 50 Wyoming 584153 NA
这是下面的代码
library(readxl)
library(rgeos)
library(maptools)
library(ggplot2) # devtools::install_github("hadley/ggplot2") only if you want subtitles/captions
library(ggalt)
library(ggthemes)
library(albersusa) # devtools::install_github("hrbrmstr/albersusa")
library(viridis)
library(scales)
statd<-read.csv('C:/statepop.csv')
us <- usa_composite()
us_map <- fortify(us, region="name")
# I may make a version of this that returns a fortified data.frame but
# for now, we just need to read the built-in saved shapefile and turn it
# into something ggplot2 can handle
statd$Population <- cut(statd$Population,breaks=6)
colors <- brewer.pal(6, "Purples") #set breaks for the 9
gg <- ggplot()
gg <- gg + geom_map(data=us_map, map=us_map,
aes(x=long, y=lat, map_id=id),
color="#2b2b2b", size=0.1, fill=NA)
gg <- gg + theme_map()
gg + coord_map()
gg +
geom_map(data=statd, map=us_map,
aes(fill=Population, map_id=State),
color="black", size=0.1) +
coord_proj(us_laea_proj) +
theme(legend.position="right") + labs(title="U.S. Population")