我有一个按月划分的数据集。然而,当我绘制每个子集时,图例的颜色比例和范围都是不同的。我如何显式地定义它,以便它在所有子集上都是一致的?
因此,在下面的示例中,我希望未过滤的数据集和过滤的数据集的图例的色标和范围是相同的。
#original dataset. I want to construct the legend with this
weather <- read.csv("weather2_rmse.csv") %>%
mutate(date_of_forecast = as.Date(date_of_forecast))
#filtered. I want to construct the points with this
data <- filter(weather, date_of_forecast == "2015-02-01")
weather_map <- function(data) {
# change default color scale title
m <- list(colorbar = list(title = ""))
# geo styling
g <- list(
scope = 'north america',
showland = TRUE,
landcolor = toRGB("grey83"),
subunitcolor = toRGB("white"),
countrycolor = toRGB("white"),
showlakes = TRUE,
lakecolor = toRGB("white"),
showsubunits = TRUE,
showcountries = TRUE,
resolution = 50,
projection = list(
type = 'conic conformal',
rotation = list(lon = -100)
),
lonaxis = list(
showgrid = TRUE,
gridwidth = 0.5,
range = c(-140, -55),
dtick = 5
),
lataxis = list(
showgrid = TRUE,
gridwidth = 0.5,
range = c(20, 60),
dtick = 5
)
)
#the plotly part
p <- plot_geo(data, lat = ~latitude, lon = ~longitude, color = ~rmse) %>%
add_markers(
text = ~paste(data$rmse, "RMSE"), hoverinfo = "text"
) %>%
layout(title = 'Average RMSE of MaxTemp Forecasts by Month<br>xxxxxxxxxx', geo = g)
p
}发布于 2018-02-11 02:25:18
例如,这样定义一个比例,例如:
scl = list(c(0, "rgb(0, 0, 255)"), list(1, "rgb(0, 255, 0)"))(您还可以添加更多值,这是基于分位数AFAIK的。所以像这样设置它
scl = list(c(0, "rgb(0, 0, 255)"), list(0.1, "rgb(0, 0, 255)"),
c(0.1, "rgb(0, 255, 0)"), list(1, "rgb(0, 255, 0)"))将使您的值的前10%为蓝色,其余为绿色。
然后,修改您的plot调用,例如:
p <- plot_geo(data) %>%
add_markers(y = ~latitude, x = ~longitude,
mode = "markers", type = "scatter",
marker = list(color = ~rmse,
colorscale = scl,
cauto = FALSE,
colorbar = list(title=""),
cmax = 15,
cmin = 0)) %>%
layout(title = 'Average RMSE of MaxTemp Forecasts by Month<br>xxxxxxxxxx', geo = g)
p通过使用cmax和cmin设置精确的限制,您应该在各子图中具有相同的比例
https://stackoverflow.com/questions/48723071
复制相似问题