ggplot2 用颜色非线性调整比例

Pau*_*aul 5 plot r ggplot2

我有两个问题:

  1. 虽然这些数据的范围是-1到5,但大多数的值都在-1到1之间。因此,我想知道是否可以将结果和颜色之间的关系调整为非线性(即更多变化) -1 和 1 之间,但 2 和 5 之间变化较小)。使用 GGplot2 可以实现这一点吗?

  2. 如何将比例尺移动到地图中,例如右下角位置?

这是我的代码:

library(ggplot2)
library(ggmap)
library(data.table)
map<-get_map(location='united states', zoom=4, maptype = "terrain",
         source='google',color='bw')
ggmap(map) + geom_point(
 aes(x=longitude, y=latitude, show_guide = TRUE, colour=V1), 
 data=plot.data, alpha=0.3, na.rm = T)  + 
 scale_color_gradient(low="red", high = "red4", name = "Level")
Run Code Online (Sandbox Code Playgroud)

bee*_*oot 3

您可以使用scale_colour_gradientn和定义values(来自?scale_colour_gradientn如果颜色不应沿渐变均匀定位,则此向量给出颜色向量中每种颜色的位置(0 和 1 之间)。),例如:

ggplot(data = iris, aes(x = Species, y = Sepal.Width, colour = Sepal.Length)) +
      geom_point() +
      scale_colour_gradientn(colours = c("blue", "red", "orange"),
                             values = c(0, 0.1, 1))
Run Code Online (Sandbox Code Playgroud)

要更改图例的位置,请查看?theme>legend.position和/或legend.justification