如何删除usmap plot中的边框
How to remove the borders in usmap plot
我正在尝试根据软件包 'usmap' 制作美国大选地图。问题是这个包似乎只能做白色背景。如果我更改背景颜色,它会显示边框。我试图通过设置 panel.border = element_blank()
来删除边框,但它似乎没有用。有什么办法可以解决这个问题吗?
library(usmap)
poll <- read_csv('poll.csv')
poll$fips <-fips(poll$State)
poll$Attitude <- factor(poll$Attitude, levels = attitude)
plot2 <- plot_usmap(data = poll, values = "Attitude", color = 'white', labels=FALSE) + scale_fill_manual(values=c("#1055b6", "#67b5e2", "#cccccc","#ffaca3","#ed4748")) + guides(fill = FALSE)
plot2+ theme(plot.background = element_rect(fill = "#fdf1e5"),
panel.background = element_rect(fill = "#fdf1e5"),
panel.border = element_blank())
这是我的数据。
State Abbrev Code n_votes Attitude fips
1 Alabama Ala. AL 9 Solid Republican 01
2 Alaska Alaska AK 3 Toss-up 02
3 Arizona Ariz. AZ 11 Toss-up 04
4 Arkansas Ark. AR 6 Solid Republican 05
5 California Calif. CA 55 Solid Democrat 06
6 Colorado Colo. CO 9 Leaning to Democrat 08
7 Connecticut Conn. CT 7 Solid Democrat 09
8 Delaware Del. DE 3 Solid Democrat 10
9 District of Columbia D.C. DC 3 Solid Democrat 11
10 Florida Fla. FL 29 Leaning to Democrat 12
11 Georgia Ga. GA 16 Toss-up 13
12 Hawaii Hawaii HI 4 Solid Democrat 15
13 Idaho Idaho ID 4 Solid Republican 16
14 Illinois Ill. IL 20 Solid Democrat 17
15 Indiana Ind. IN 11 Leaning to Republican 18
16 Iowa Iowa IA 6 Leaning to Republican 19
17 Kansas Kans. KS 6 Leaning to Republican 20
18 Kentucky Ky. KY 8 Solid Republican 21
19 Louisiana La. LA 8 Solid Republican 22
20 Maine Maine ME 2 Solid Democrat 23
21 Maryland Md. MD 10 Solid Democrat 24
22 Massachusetts Mass. MA 11 Solid Democrat 25
23 Michigan Mich. MI 16 Leaning to Democrat 26
24 Minnesota Minn. MN 10 Toss-up 27
25 Mississippi Miss. MS 6 Solid Republican 28
26 Missouri Mo. MO 10 Leaning to Republican 29
27 Montana Mont. MT 3 Solid Republican 30
28 Nebraska Nebr. NE 2 Solid Republican 31
29 Nevada Nev. NV 6 Leaning to Democrat 32
30 New Hampshire N.H. NH 4 Leaning to Democrat 33
31 New Jersey N.J. NJ 14 Solid Democrat 34
32 New Mexico N.M. NM 5 Solid Democrat 35
33 New York N.Y. NY 29 Solid Democrat 36
34 North Carolina N.C. NC 15 Toss-up 37
35 North Dakota N.D. ND 3 Solid Republican 38
36 Ohio Ohio OH 18 Toss-up 39
37 Oklahoma Okla. OK 7 Solid Republican 40
38 Oregon Ore. OR 7 Solid Democrat 41
39 Pennsylvania Pa. PA 20 Leaning to Democrat 42
40 Rhode Island R.I. RI 4 Solid Democrat 44
41 South Carolina S.C. SC 9 Toss-up 45
42 South Dakota S.D. SD 3 Solid Republican 46
43 Tennessee Tenn. TN 11 Solid Republican 47
44 Texas Tex. TX 38 Toss-up 48
45 Utah Utah UT 6 Leaning to Republican 49
46 Vermont Vt. VT 3 Solid Democrat 50
47 Virginia Va. VA 13 Leaning to Democrat 51
48 Washington Wash. WA 12 Solid Democrat 53
49 West Virginia W.Va. WV 5 Solid Republican 54
50 Wisconsin Wis. WI 10 Leaning to Democrat 55
51 Wyoming Wyo. WY 3 Solid Republican 56
谢谢
段
主题元素 panel.border
继承自 panel.background
矩形对象。在您的代码中,它被 panel.background
的矩形对象覆盖。您的 panel.background
元素仅指定 fill=
,因此 color=
(面板周围边框的颜色)默认设置为 "black"
。如果您想为 panel.background
指定边框的颜色,您应该在该对象的 element_rect
内进行。例如,这将删除您的面板边框(但保留绘图边框):
plot2+ theme(plot.background = element_rect(fill = "#fdf1e5"),
panel.background = element_rect(color=NA, fill = "#fdf1e5"))
此外,plot.background
是整个图,包括绘图区域和周围的东西,如轴标签、文本等。panel.background
是图本身的绘图区域:它是所有的点都画在图上。毫无疑问,您已经了解内框是 panel.background
,“外”框是 plot.background
。如果只指定plot.background
,则不需要指定panel.background
.
例如,下面给出相同的结果(完全删除 panel.background
对象):
plot2+ theme(plot.background = element_rect(fill = "#fdf1e5"))
我无法让你的数据框正常工作(没有做大量的导入工作),所以你可以使用 usmap
包中的内置人口数据观察效果:
plot_usmap(data=statepop, values="pop_2015", color='white', labels=FALSE) + guides(fill=FALSE) +
theme(plot.background = element_rect(fill = "#fdf1e5"))
哦,如果你想删除所有边框(不仅仅是内部边框),你应该以相同的方式在plot.background
元素中指定它:
theme(plot.background = element_rect(color = NA, fill = "#fdf1e5"))
我正在尝试根据软件包 'usmap' 制作美国大选地图。问题是这个包似乎只能做白色背景。如果我更改背景颜色,它会显示边框。我试图通过设置 panel.border = element_blank()
来删除边框,但它似乎没有用。有什么办法可以解决这个问题吗?
library(usmap)
poll <- read_csv('poll.csv')
poll$fips <-fips(poll$State)
poll$Attitude <- factor(poll$Attitude, levels = attitude)
plot2 <- plot_usmap(data = poll, values = "Attitude", color = 'white', labels=FALSE) + scale_fill_manual(values=c("#1055b6", "#67b5e2", "#cccccc","#ffaca3","#ed4748")) + guides(fill = FALSE)
plot2+ theme(plot.background = element_rect(fill = "#fdf1e5"),
panel.background = element_rect(fill = "#fdf1e5"),
panel.border = element_blank())
这是我的数据。
State Abbrev Code n_votes Attitude fips
1 Alabama Ala. AL 9 Solid Republican 01
2 Alaska Alaska AK 3 Toss-up 02
3 Arizona Ariz. AZ 11 Toss-up 04
4 Arkansas Ark. AR 6 Solid Republican 05
5 California Calif. CA 55 Solid Democrat 06
6 Colorado Colo. CO 9 Leaning to Democrat 08
7 Connecticut Conn. CT 7 Solid Democrat 09
8 Delaware Del. DE 3 Solid Democrat 10
9 District of Columbia D.C. DC 3 Solid Democrat 11
10 Florida Fla. FL 29 Leaning to Democrat 12
11 Georgia Ga. GA 16 Toss-up 13
12 Hawaii Hawaii HI 4 Solid Democrat 15
13 Idaho Idaho ID 4 Solid Republican 16
14 Illinois Ill. IL 20 Solid Democrat 17
15 Indiana Ind. IN 11 Leaning to Republican 18
16 Iowa Iowa IA 6 Leaning to Republican 19
17 Kansas Kans. KS 6 Leaning to Republican 20
18 Kentucky Ky. KY 8 Solid Republican 21
19 Louisiana La. LA 8 Solid Republican 22
20 Maine Maine ME 2 Solid Democrat 23
21 Maryland Md. MD 10 Solid Democrat 24
22 Massachusetts Mass. MA 11 Solid Democrat 25
23 Michigan Mich. MI 16 Leaning to Democrat 26
24 Minnesota Minn. MN 10 Toss-up 27
25 Mississippi Miss. MS 6 Solid Republican 28
26 Missouri Mo. MO 10 Leaning to Republican 29
27 Montana Mont. MT 3 Solid Republican 30
28 Nebraska Nebr. NE 2 Solid Republican 31
29 Nevada Nev. NV 6 Leaning to Democrat 32
30 New Hampshire N.H. NH 4 Leaning to Democrat 33
31 New Jersey N.J. NJ 14 Solid Democrat 34
32 New Mexico N.M. NM 5 Solid Democrat 35
33 New York N.Y. NY 29 Solid Democrat 36
34 North Carolina N.C. NC 15 Toss-up 37
35 North Dakota N.D. ND 3 Solid Republican 38
36 Ohio Ohio OH 18 Toss-up 39
37 Oklahoma Okla. OK 7 Solid Republican 40
38 Oregon Ore. OR 7 Solid Democrat 41
39 Pennsylvania Pa. PA 20 Leaning to Democrat 42
40 Rhode Island R.I. RI 4 Solid Democrat 44
41 South Carolina S.C. SC 9 Toss-up 45
42 South Dakota S.D. SD 3 Solid Republican 46
43 Tennessee Tenn. TN 11 Solid Republican 47
44 Texas Tex. TX 38 Toss-up 48
45 Utah Utah UT 6 Leaning to Republican 49
46 Vermont Vt. VT 3 Solid Democrat 50
47 Virginia Va. VA 13 Leaning to Democrat 51
48 Washington Wash. WA 12 Solid Democrat 53
49 West Virginia W.Va. WV 5 Solid Republican 54
50 Wisconsin Wis. WI 10 Leaning to Democrat 55
51 Wyoming Wyo. WY 3 Solid Republican 56
谢谢
段
主题元素 panel.border
继承自 panel.background
矩形对象。在您的代码中,它被 panel.background
的矩形对象覆盖。您的 panel.background
元素仅指定 fill=
,因此 color=
(面板周围边框的颜色)默认设置为 "black"
。如果您想为 panel.background
指定边框的颜色,您应该在该对象的 element_rect
内进行。例如,这将删除您的面板边框(但保留绘图边框):
plot2+ theme(plot.background = element_rect(fill = "#fdf1e5"),
panel.background = element_rect(color=NA, fill = "#fdf1e5"))
此外,plot.background
是整个图,包括绘图区域和周围的东西,如轴标签、文本等。panel.background
是图本身的绘图区域:它是所有的点都画在图上。毫无疑问,您已经了解内框是 panel.background
,“外”框是 plot.background
。如果只指定plot.background
,则不需要指定panel.background
.
例如,下面给出相同的结果(完全删除 panel.background
对象):
plot2+ theme(plot.background = element_rect(fill = "#fdf1e5"))
我无法让你的数据框正常工作(没有做大量的导入工作),所以你可以使用 usmap
包中的内置人口数据观察效果:
plot_usmap(data=statepop, values="pop_2015", color='white', labels=FALSE) + guides(fill=FALSE) +
theme(plot.background = element_rect(fill = "#fdf1e5"))
哦,如果你想删除所有边框(不仅仅是内部边框),你应该以相同的方式在plot.background
元素中指定它:
theme(plot.background = element_rect(color = NA, fill = "#fdf1e5"))