我使用此代码绘制北美温度变化地图:
ggplot(maps, aes(y=Latitude, x=Longitude, z=variable)) +
ggtitle(title)+
scale_fill_brewer(palette = "Spectral")+
geom_contour_filled(breaks = c(-Inf,-2., -1.5, -1., -0.5, 0, 0.5, 1, 1.5, 2, 3, 4, 5, 7, 9, 11,Inf))+
xlim(-146,-44)+
ylim(35,90)+
theme(plot.title = element_text(hjust = 0.5))
Run Code Online (Sandbox Code Playgroud)
我想更改调色板以获得更好的可视化效果。目前,它写道 scale_fill_brewer(palette = "Spectral"),我将把它更改为自定义调色板
colors <- c(rgb(10,40,100,max=255),rgb(51,102,217,max=255),rgb(105,138,236,max=255),rgb(151,180,250,max=255),rgb(204,217,255,max=255),rgb(255,245,204,max=255),rgb(255,224,153,max=255),rgb(255,203,102,max=255),rgb(255,180,51,max=255),rgb(255,140,51,max=255),rgb(255,85,0,max=255),rgb(230,40,30,max=255),rgb(191,0,0,max=255),rgb(140,0,0,max=255),rgb(108,0,0,max=255),rgb(110,0,70,max=255))
Run Code Online (Sandbox Code Playgroud)
使用scale_fill_brewer(palette = colors)会报错,我也试过了palette(colors),也是不行。
如何自定义参数可以识别的调色板?
我根据这样的一段代码绘制了一张地图:
ggplot(faithfuld, aes(y=eruptions, x=waiting, z=100*density)) +
geom_contour_filled(breaks = c(-Inf,-2., -1.5, -1., -0.5, 0, 0.5, 1, 1.5, 2, 3, 4, 5, 7, 9, 11,Inf))+
theme(plot.title = element_text(size = 10,hjust = 0.5))
Run Code Online (Sandbox Code Playgroud)
这是我的情节目前看起来像:
但是我的老板让我把这个传奇变成这样:
或者像这样:
此链接 ( https://ggplot2.tidyverse.org/reference/theme.html ) 中的参数只是为图例提供了细微的更改。而且我找不到任何可以实现这一点的论据,ggplot 是否可行?或者我必须使用其他绘图包?
创建具有不同间隔宽度且图例级别之间没有间距的离散颜色条这个问题(答案第 4 号)提供了一种方法,可以创建像我的老板要求的颜色条,但是,我正在使用geom_contour_filled(breaks = c(-Inf,-2., -1.5, -1., -0.5, 0, 0.5, 1, 1.5, 2, 3, 4, 5, 7, 9, 11,Inf))此参数,因此图例始终与大量文字:
有什么解决办法吗?