基于颜色箱的R中图形绘制
我有三列:纬度、经度和价格。 我想画一个x轴为lat,y轴为long的图。点的颜色由价格值决定。(0-50$-红色,50-100$蓝色等)。我尝试使用ggplots,但没有找到任何可以根据提供的范围对点进行着色的内容基于颜色箱的R中图形绘制,r,R,我有三列:纬度、经度和价格。 我想画一个x轴为lat,y轴为long的图。点的颜色由价格值决定。(0-50$-红色,50-100$蓝色等)。我尝试使用ggplots,但没有找到任何可以根据提供的范围对点进行着色的内容 latitude longitude price 40.8520537789 -73.7886796346 100 40.8411441603 -73.7830522867 100.4 40.8500238142 -73.7893275949 102
latitude longitude price
40.8520537789 -73.7886796346 100
40.8411441603 -73.7830522867 100.4
40.8500238142 -73.7893275949 102
您可以使用plot()函数进行此操作
这是data.table(或者也可以使用data.frame)
您可以通过以下代码找到每行的颜色
color_val <- ifelse(dt[['price']] >= 0 & dt[['price']] <= 50,"red",ifelse(dt[['price']] > 50 & dt[['price']] <= 100,"blue","black"))
现在,您可以通过以下命令绘制数据:
plot(x = dt[['latitude']],y = dt[['longitude']],col = color_val)
注意:这只是在x-y坐标上的正常绘图。首先在
price
列上使用cut()
或findInterval()
来获取组。我这样做了。现在,我在价格列中的值为0,1,…9(10个箱子)。这将如何解决呢?所有的ifelse
编码都可以简化为cut(dt$price,c(0,50100,Inf),labels=c(“红色”、“蓝色”、“黑色”)
cut(dt$price, c(0,50,100,Inf), labels=c("red","blue","black"))
plot(x = dt[['latitude']],y = dt[['longitude']],col = color_val)