简体   繁体   English

在 R 中为 plot_usmap 定义图例的比例

[英]Defining the scale of the legend for a plot_usmap in R

I am plotting the state-level market-shares of a firm, "SB", in the US using usmap::plot_usmap() .我正在使用usmap::plot_usmap()美国“SB”公司的州级市场份额。 I would like to obtain two comparable plots, one for year 2005 and one for 2013, but I also need them to be comparable in terms of color scales.我想获得两个可比较的图,一个是 2005 年,一个是 2013 年,但我也需要它们在色阶方面具有可比性。 For this purpose, I would like to FIX the scale of the legend from 0 to 60%, since market shares in 2005 range from 0 to 40% while they range from 0 to 60% in 2013. If I plot the two graphs without fixing the scale, the colors don't match ie in 2005 the darkest blue is 40-50% while in 2013 the darkest blue is for 50-60%.为此,我想将图例的比例从 0% 固定到 60%,因为 2005 年的市场份额范围从 0% 到 40%,而 2013 年的范围从 0% 到 60%。比例,颜色不匹配,即在 2005 年,最深的蓝色是 40-50%,而在 2013 年,最深的蓝色是 50-60%。

My dataframe, data, has 3 variables: state, SB2005, and SB2013.我的数据框 data 有 3 个变量:状态、SB2005 和 SB2013。

> data
   state   SB2013   SB2005
1     AK 13.73301 39.07751
2     AL 31.07569 27.79722
3     AR 17.32783 18.86964
4     AZ 50.41637 43.68238
5     CA 41.97910 36.44163
6     CO 44.54290 37.15720
7     CT 28.86247 30.40817
8     DC 31.19301 23.21915
9     DE 36.13980 30.46963
10    FL 33.09666 33.82679
11    GA 32.23701 35.41160
12    HI 34.97479 28.96052
13    IA 17.84128 19.53318
14    ID 33.69578 24.82588
15    IL 31.11345 31.15282
16    IN 30.11551 26.12669
17    KS 20.05335 22.27834
18    KY 25.73649 22.78522
19    LA 36.49632 25.91918
20    MA 31.95588 36.32401
21    MD 34.82404 32.72958
22    ME 33.77195 37.01851
23    MI 31.92783 34.58157
24    MN 25.97937 30.64853
25    MO 27.83736 25.74990
26    MS 17.41351 24.45305
27    MT 18.94255 20.83375
28    NC 28.51608 26.42641
29    ND 12.51515 12.01312
30    NE 14.94143 17.01305
31    NH 41.07999 39.42883
32    NJ 34.27007 35.41661
33    NM 37.09724 30.60311
34    NV 53.29869 46.74818
35    NY 23.93017 30.71950
36    OH 22.25994 26.24390
37    OK 27.27723 26.68940
38    OR 43.86115 34.61210
39    PA 30.73276 27.73622
40    PR 23.70947  8.94793
41    RI 29.47681 36.57546
42    SC 27.97239 27.07268
43    SD 13.16030 11.69766
44    TN 32.06779 27.59431
45    TX 40.74324 38.22124
46    UT 55.91908 41.99760
47    VA 34.95502 29.42225
48    VT 24.06665 33.79450
49    WA 35.32285 28.29210
50    WI 18.13374 19.69282
51    WV 19.93967 16.25546
52    WY 26.10650 30.10866

I was able to get two maps but with different scales, one for 2005 using variable SB2005, and one for 2013 using variable SB2013.我能够得到两张不同比例的地图,一张是 2005 年使用变量 SB2005,另一张是 2013 年使用变量 SB2013。

I tried adding: + legend.scale(c(0,60)) but without success.我尝试添加: + legend.scale(c(0,60))但没有成功。

library(usmap)
library(ggplot2) 

plot_usmap(regions = "state", data=data, values = "SB2005")+
labs(title = "SB Market-Share by State (2005)") + 
theme(panel.background = element_rect(colour = "black"))+
scale_fill_continuous(low = "white", high ="cornflowerblue", 
name = "Market-Share (%)",label = scales::comma) + 
theme(legend.position = "right")

I want to get two maps with the SAME SCALE of colors.我想获得两张颜色相同的地图。

Consider using the limits parameter in scale_fill_continuous().考虑在 scale_fill_continuous() 中使用限制参数。

##Map for 2005
map_1 <- plot_usmap(regions = "state", data=data, values = "SB2005")+
    labs(title = "SB Market-Share by State (2005)") + 
    theme(panel.background = element_rect(colour = "black"))+
    scale_fill_continuous(low = "white", high ="darkblue", 
                          name = "Market-Share (%)",label = scales::comma,
                          limits = c(0,60)) + 
    theme(legend.position = "right")

##Map for 2013
map_2 <- plot_usmap(regions = "state", data=data, values = "SB2013")+
    labs(title = "SB Market-Share by State (2013)") + 
    theme(panel.background = element_rect(colour = "black"))+
    scale_fill_continuous(low = "white", high ="darkblue", 
                          name = "Market-Share (%)",label = scales::comma,
                          limits = c(0,60)) + 
    theme(legend.position = "right")

Here is my output:这是我的输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM