将连续刻度从十进制更改为百分比 [英] Changing a continuous scale from decimal to percents

查看:118
本文介绍了将连续刻度从十进制更改为百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

渗透率的等级以小数点表示(.5及以下),但将其更改为百分比时遇到问题.

The scale for penetration is listed as a decimal (.5 and down), but I am having a problem changing it to a percent.

我尝试使用此代码将其格式化为百分比格式的数据

I tried to format it in my data as a percentage using this code

penetration_levels$Penetration<-sprintf("%.1f %%", 100*penetration_levels$Penetration)

从格式的角度来看是可行的,但是当我尝试绘制曲线图时,出现了一个错误,说渗透被用作离散的而不是连续的比例.

which worked from a format sense, but when I tried to graph the plot I got an error saying penetration was used as a discrete, not continuous scale.

要解决此问题,请使用此代码将其格式化为数字变量

To fix that, used this code to format it as a numeric variable

penetration_levels$Penetration<-as.numeric(as.character(penetration_levels$Penetration))

哪个返回了一堆NA.有人知道我如何将其更改为百分比的任何其他方法吗?

Which returned a bunch of NAs. Does anyone know any other method of how I can change it to a percent?

这是我用来映射的代码

ggplot代码:

map <- ggplot(penetration_levels,aes(long,lat,group=region,fill=Penetration),) + geom_polygon() + coord _equal() + scale_fill_gradient2(low="red",mid="white",high="green",midpoint=.25)
map <- map + geom_point(data=mydata, aes(x=long, y=lat,group=1,fill=0, size=Annualized.Opportunity), color="gray6") + scale_size(name="Total Annual Opportunity-Millions",range=c(2,4))  
map <- map + theme(plot.title = element_text(size = 12,face="bold"))
map

mydata和渗透的负责人

Head of mydata and penetration

head(mydata)
Sold.To.Customer            City State Annualized.Opportunity           location          lat      long
21          10000110        NEW YORK    NY              12.142579        NEW YORK,NY     40.71435 -74.00597
262         10016487 FORT LAUDERDALE    FL              12.087310 FORT LAUDERDALE,FL 26.12244 -80.13732
349         11001422      ALLEN PARK    MI              10.910575      ALLEN PARK,MI 42.25754 -83.21104
19          10000096           ALTON    IL              10.040067           ALTON,IL 38.89060 -90.18428
477         11067228        BAY CITY    TX              10.030829        BAY CITY,TX 28.98276 -95.96940
230         10014909        BETHPAGE    NY               9.320271        BETHPAGE,NY 40.74427 -73.48207

head(penetration_levels)
State  region      long      lat group order subregion state       To     From    Total    Penetration
17    AL alabama -87.46201 30.38968     1     1      <NA>    AL 10794947 12537359 23332307    0.462661
18    AL alabama -87.48493 30.37249     1     2      <NA>    AL 10794947 12537359 23332307    0.462661
22    AL alabama -87.52503 30.37249     1     3      <NA>    AL 10794947 12537359 23332307    0.462661
36    AL alabama -87.53076 30.33239     1     4      <NA>    AL 10794947 12537359 23332307    0.462661
37    AL alabama -87.57087 30.32665     1     5      <NA>    AL 10794947 12537359 23332307    0.462661
65    AL alabama -87.58806 30.32665     1     6      <NA>    AL 10794947 12537359 23332307    0.462661

我还刚刚注意到,有一条白色的条带,类似于华盛顿缺少的一个多边形……您碰巧知道为什么吗?我试图重新合并数据并再次对其进行排序,但结果仍然相同.

I also just noticed that there was a white strip, similar to a polygon that is missing in Washington… do you happen to know why that is? I tried to re-merge my data and order it again, but still the same result.

任何见识将不胜感激.

Any insight would be greatly appreciated.

我还注意到华盛顿缺少一个白色的多边形吗?有谁知道为什么会这样吗?

Also, I noticed that Washington has a white polygon missing? Does anyone know why this happens?

推荐答案

您可以加载scales软件包并使用scale_fill_continuous(labels = percent).在帮助文本的参数部分中,百分比参数的记录不是很好,但是可以在示例部分scales包中的其他方便格式. /docs.ggplot2.org/current/scale_continuous.html"rel =" nofollow noreferrer>"> 此处 .

You may load the scales package and use scale_fill_continuous(labels = percent). The percent argument is not very well documented in the argument section of the help text, but an example of this function, and other convenient formats from the scales package, can be found in the example section here.

一个小例子:

library(scales)

df <- data.frame(long = 1:10, lat = 1:10,
                 penetration = seq(from = 0.1, to = 1, by = 0.1))

ggplot(data = df, aes(x = long, y = lat, fill = penetration)) +
  geom_point(shape = 21, size = 6) +
  scale_fill_continuous(labels = percent)

这篇关于将连续刻度从十进制更改为百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆