R ggplot2渐变贴图的离散调色板
在SO用户的帮助下,我现在能够使用R ggplot2渐变贴图的离散调色板,r,plot,ggplot2,color-palette,R,Plot,Ggplot2,Color Palette,在SO用户的帮助下,我现在能够使用geom\u tile和scale\u fill\u gradient绘制渐变贴图。现在我需要指定一个固定的调色板。我的第一次尝试是使用scale\u fill\u manual,但我收到以下错误消息: 错误:提供给离散刻度的连续值 这是原始绘图的代码 ggplot() + geom_tile(data = idw.output, alpha = 0.8, aes(x = longitud, y = latitud, fill = RGlobal)) +
geom\u tile
和scale\u fill\u gradient
绘制渐变贴图。现在我需要指定一个固定的调色板。我的第一次尝试是使用scale\u fill\u manual
,但我收到以下错误消息:
错误:提供给离散刻度的连续值
这是原始绘图的代码
ggplot() +
geom_tile(data = idw.output, alpha = 0.8, aes(x = longitud, y = latitud, fill = RGlobal)) +
scale_fill_gradient(low = "cyan", high = "orange",name = "UVI") +
coord_map(xlim = c(-1.7, -1),ylim = c(37.7,38)) +
ggtitle("Previsión UVI") + xlab(" ") + ylab(" ")
所需的调色板是
paleta <- c("#4eb400","#a0ce00","#f7e400","#f8b600","#f88700","#f85900","#e82c0e","#d8001d","#ff0099","#b54cff","#998cff")
提前感谢您的帮助选项1:缩放填充梯度n
选项2:scale\u fill\u manual
首先转换为系数:
idw.output$RGlobal_f <- cut(idw.output$RGlobal, breaks = length(paleta))
ggplot() + geom_tile(data = idw.output, alpha = 0.8, aes(x = longitud, y = latitud, fill = RGlobal_f)) +
scale_fill_manual(values = paleta) +
coord_map(xlim = c(-1.7, -1),ylim = c(37.7,38)) +
ggtitle("Previsión UVI") + xlab(" ") + ylab(" ")
idw.output$RGlobal\u f将我的评论转换为答案:
# create a factor variable
idw.output$RGlobal2 <- cut(idw.output$RGlobal, 11)
# plot
ggplot(idw.output, aes(x = factor(longitud), y = factor(latitud), fill = RGlobal2)) +
geom_tile() +
scale_x_discrete(expand = c(0,0)) +
scale_y_discrete(expand = c(0,0)) +
scale_fill_manual(values = paleta) +
ggtitle("Previsión UVI") + xlab(" ") + ylab(" ")
其中:
另一种选择是使用scale\u fill\u gradientn
,正如@Axeman在他的回答中所做的那样,但是通过将x轴和y轴转换为因子变量来更好地定位轴标签:
ggplot(idw.output, aes(x = factor(round(longitud,1)), y = factor(latitud), fill = RGlobal)) +
geom_tile() +
scale_x_discrete(expand = c(0,0)) +
scale_y_discrete(expand = c(0,0)) +
scale_fill_gradientn(colours = paleta) +
labs(title = "Previsión UVI") +
theme(axis.title = element_blank())
其中:
由于longitud
和latitud
是连续变量,因此不能对其应用离散尺度。您可以使用连续比例或将变量转换为因子,例如:aes(x=factor(longitud),y=factor(latitud),fill=RGlobal)
Hi@Jaap如果我将变量转换为因子,那么我是否应该使用scale\u fill\u manual
如果您想使用问题中提到的指定调色板,然后是的,您必须使用比例填充\u手册,但这也意味着您必须将填充变量转换为离散(=因子/字符)变量。对于您试图绘制的绘图类型,我建议使用比例\填充\梯度(或其同级)。我也认为缩放填充梯度最适合贴图,但关键是我必须使用离散调色板。在这种情况下,您可以使用cut
从RGlobal
创建一个因子变量,然后将其用于fill
。这是一个非常好的答案,唯一的一点是瓷砖尺寸更大。但它对我有用。更新得好。您可以在scale\u x/y\u discrete
调用中添加limits
。创建沿轴的所有步骤都表示的因子时要小心。@Axeman没问题;但是如果你觉得你偷了支票,你一定会找到一个方法来减轻我的痛苦;-)这会有很大的不同吗?如何修复它?@pacomet IMHO:是的,因为使用因子变量可以更好地显示瓷砖。现在,y轴末端的瓷砖比中间的瓷砖小。有关修复方法,请参阅我的答案。@Axeman我可以这样做,但这几乎意味着在我自己的答案中进行编辑;-)。因此,在我的答案中添加了一个例子,并将其归因于您的答案。
# create a factor variable
idw.output$RGlobal2 <- cut(idw.output$RGlobal, 11)
# plot
ggplot(idw.output, aes(x = factor(longitud), y = factor(latitud), fill = RGlobal2)) +
geom_tile() +
scale_x_discrete(expand = c(0,0)) +
scale_y_discrete(expand = c(0,0)) +
scale_fill_manual(values = paleta) +
ggtitle("Previsión UVI") + xlab(" ") + ylab(" ")
# cut 'RGlobal' in equal lengths from the minimum value to the maximum value
# by making use of the 'length.out' parameter of the 'cut' function
idw.output$Rfactor <- cut(idw.output$RGlobal, breaks = seq(2.4, 4.1, length.out = 11))
niveles <- levels(idw.output$Rfactor)
# create the plot
ggplot(idw.output, aes(x = factor(round(longitud,1)), y = factor(latitud), fill = Rfactor)) +
geom_tile() +
scale_x_discrete(expand = c(0,0)) +
scale_y_discrete(expand = c(0,0)) +
scale_fill_manual(name = "UVI", values = paleta, breaks = niveles) +
labs(title = "Previsión UVI", x = " ", y = " ")
# instead of 'labs(title = "Previsión UVI", x = " ", y = " ")'
# you can also use: 'labs(title = "Previsión UVI") + theme(axis.title = element_blank())'
ggplot(idw.output, aes(x = factor(round(longitud,1)), y = factor(latitud), fill = RGlobal)) +
geom_tile() +
scale_x_discrete(expand = c(0,0)) +
scale_y_discrete(expand = c(0,0)) +
scale_fill_gradientn(colours = paleta) +
labs(title = "Previsión UVI") +
theme(axis.title = element_blank())