我发现我需要使用一个函数来控制散点图中使用的字符,而不是只传入一个分类变量。这是为什么?
这是我尝试过的。
这是输入文件的样子
~/data $ head avgpm25.csv
"pm25","fips","region","longitude","latitude"
9.77118522614686,"01003","east",-87.74826,30.592781
9.99381725284814,"01027","east",-85.842858,33.26581
10.6886181012839,"01033","east",-87.72596,34.73148
11.3374236874237,"01049","east",-85.798919,34.459133
12.1197644686119,"01055","east",-86.032125,34.018597
10.8278048723489,"01069","east",-85.350387,31.189731
11.5839280137523,"01073","east",-86.82805,33.527872
11.2619958748527,"01089","east",-86.588226,34.73079
9.41442269955754,"01097","east",-88.139667,30.722256
这是R会话的成绩单
> pollution <- fread("~/Downloads/data//avgpm25.csv")
> str(pollution)
Classes ‘data.table’ and 'data.frame': 576 obs. of 5 variables:
$ pm25 : num 9.77 9.99 10.69 11.34 12.12 ...
$ fips : chr "01003" "01027" "01033" "01049" ...
$ region : chr "east" "east" "east" "east" ...
$ longitude: num -87.7 -85.8 -87.7 -85.8 -86 ...
$ latitude : num 30.6 33.3 34.7 34.5 34 ...
- attr(*, ".internal.selfref")=<externalptr>
请注意pollution$region
是chr
> with(pollution, plot(pm25)) # this works, but all points are black
> with(pollution, plot(pm25, col = region)) # this fails
Error in plot.xy(xy, type, ...) : invalid color name 'east'
> with(pollution, plot(pm25, col = as.factor(region))) # this works, points of two region get painted in two different colors
这就是我提问的原因。为什么这不起作用
> with(pollution, plot(pm25, col = as.factor(region), pch = as.factor(region)))
Error in plot.xy(xy, type, ...) : invalid plotting symbol
但定义一个函数,然后将其传递给它!
> pch_func <- function(x) { ifelse (x == "east", 1, 2)}
> with(pollution, plot(pm25, col = as.factor(region), pch = pch_func(region)))
这也不起作用
> pollution <- mutate(pollution, region = as.factor(region))
> with(pollution, plot(pm25, col = region, pch = region))
Error in plot.xy(xy, type, ...) : invalid plotting symbol
这是为什么?任何帮助将不胜感激。
答案 0 :(得分:1)
来自?par
pch 指定符号或单个字符的整数 用作绘图点的默认值。查看可能值的点 和他们的解释
因此,您应该转换yourfactorto integer
:
with(pollution, plot(pm25, col = as.factor(region),
pch = as.integer(region)))
dx<- read.table(text='pm25,fips,region,longitude,latitude
9.77118522614686,01003,east,-87.74826,30.592781
9.99381725284814,01027,east,-85.842858,33.26581
10.6886181012839,01033,east,-87.72596,34.73148
11.3374236874237,01049,east,-85.798919,34.459133
12.1197644686119,01055,aaa,-86.032125,34.018597
10.8278048723489,01069,east,-85.350387,31.189731
11.5839280137523,01073,aaaa,-86.82805,33.527872
11.2619958748527,01089,east,-86.588226,34.73079
9.41442269955754,01097,aaaa,-88.139667,30.722256',header=TRUE,sep=',')
with(dx, plot(pm25, col = as.factor(region), pch = as.integer(region)))