英文:
geom_raster with colors based on specific discrete values
问题
我想绘制一个基于特定分类的geom_raster,并根据颜色进行分类:
我尝试了以下代码(变量=SPI),但没有成功:
scale_fill_gradient(colours = c("棕色","灰褐色","浅黄褐色","白褐色","军校蓝2","蓝色"),
breaks=c(-2,-1.5,-1,1,1.5,2), labels = format(c("-2","-1.5","-1","1","1.5","2")))
示例:
year <- seq(1977,2021,1)
jan = runif(45, min=-4, max=4)
feb = runif(45, min=-4, max=4)
mar = runif(45, min=-4, max=4)
apr = runif(45, min=-4, max=4)
may = runif(45, min=-4, max=4)
jun = runif(45, min=-4, max=4)
jul = runif(45, min=-4, max=4)
aug = runif(45, min=-4, max=4)
sep = runif(45, min=-4, max=4)
oct = runif(45, min=-4, max=4)
nov = runif(45, min=-4, max=4)
dec = runif(45, min=-4, max=4)
df = data.frame(year,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec)
df <- reshape2::melt(df, id.vars = "year")
df$year <- factor(df$year, levels = (unique(df$year)))
df$variable <- factor(df$variable, levels = (unique(df$variable)))
library(ggplot2)
e1 <- ggplot(df, aes(x = variable, y = year, fill = value)) +
geom_raster()+
guides(fill=guide_legend(title="Bohicon"))+
scale_fill_gradient(colours = c("棕色","灰褐色","浅黄褐色","白褐色","军校蓝2","蓝色"),
breaks=c(-2,-1.5,-1,1,1.5,2), labels = format(c("-2","-1.5","-1","1","1.5","2")))+
theme(legend.position="bottom")
e1
非常感谢!
英文:
I'd like to plot a geom_raster with colors based on a specific classification:
I tried the following code (variable = SPI) but it didn't work:
scale_fill_gradient(colours = c( "brown","burlywood","bisque","aliceblue","cadetblue2","blue"),
breaks=c(-2,-1.5,-1,1,1.5,2), labels = format(c("-2","-1.5","-1","1","1.5","2")))
Example:
year <- seq(1977,2021,1)
jan = runif(45, min=-4, max=4)
feb = runif(45, min=-4, max=4)
mar = runif(45, min=-4, max=4)
apr = runif(45, min=-4, max=4)
may = runif(45, min=-4, max=4)
jun = runif(45, min=-4, max=4)
jul = runif(45, min=-4, max=4)
aug = runif(45, min=-4, max=4)
sep = runif(45, min=-4, max=4)
oct = runif(45, min=-4, max=4)
nov = runif(45, min=-4, max=4)
dec = runif(45, min=-4, max=4)
df = data.frame(year,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec)
df <- reshape2::melt(df, id.vars = "year")
df$year <- factor(df$year, levels = (unique(df$year)))
df$variable <- factor(df$variable, levels = (unique(df$variable)))
library(ggplot2)
e1 <- ggplot(df, aes(x = variable, y = year, fill = value)) +
geom_raster()+
guides(fill=guide_legend(title="Bohicon"))+
scale_fill_gradient(colours = c( "brown","burlywood","bisque","aliceblue","cadetblue2","blue"),
breaks=c(-2,-1.5,-1,1,1.5,2), labels = format(c("-2","-1.5","-1","1","1.5","2")))+
theme(legend.position="bottom")
e1
Many thanks and kind regards!
答案1
得分: 1
你可以指定一个列来将你的连续值分类到不同的类别中,然后使用它来手动设置你想要的颜色。
df$col <- as.factor(findInterval(df$value, c(-2,-1.5,-1,1,1.5,2), all.inside = T))
ggplot(df, aes(x = variable, y = year, fill = col)) +
geom_raster() +
guides(fill=guide_legend(title="Bohicon:")) +
scale_fill_manual(values = c("brown","burlywood","bisque","aliceblue","cadetblue2","blue"),
labels = format(c("-2","-1.5","-1","1","1.5","2"))) +
theme(legend.position="bottom")
绘图:
英文:
You can specify a column that classifies your continues values into different classes and then use it to manually set the colors you want.
df$col <- as.factor(findInterval(df$value, c(-2,-1.5,-1,1,1.5,2), all.inside = T))
ggplot(df, aes(x = variable, y = year, fill = col)) +
geom_raster() +
guides(fill=guide_legend(title="Bohicon:")) +
scale_fill_manual(values =c( "brown","burlywood","bisque","aliceblue","cadetblue2","blue"),
labels = format(c("-2","-1.5","-1","1","1.5","2"))) +
theme(legend.position="bottom")
Plot:
答案2
得分: 1
我可能漏掉了一些内容,但我认为您可能需要另一个断点来获得表格中显示的切割。我还在此帐户中的绘图中添加了另一种颜色。我没有检查切割和标签是否匹配。
df$val_cut <- cut(df$value, breaks = c(-Inf, -2, -1.5, -1, 1, 1.5, 2, Inf),
labels = c("<= -2", "-1.99 to -1.5", "-1.49 to -1", " -0.99 to 0.99", "1 to 1.49", "1.5 to 1.99", ">=2"))
library(ggplot2)
#> 警告:包 'ggplot2' 是在 R 版本 4.2.3 下构建的
ggplot(df, aes(x = variable, y = year, fill = val_cut)) +
geom_raster() +
guides(fill=guide_legend(title="Bohicon")) +
scale_fill_manual(values = c( "brown","burlywood","bisque","aliceblue","cadetblue2","blue", "black"))
创建于2023-06-01,使用 reprex v2.0.2
对于后来的问题...
是的,您可以以条形图的方式呈现数据,尽管 geom_bar
不是首选的方法,因为数据已经被汇总。以下是一种可能的解决方案,使用 geom_col
和 facet_wrap
使数据更易阅读。我假设您想按年查看每月的值,但您可以根据您的需求调整数据。
还添加了一些格式以增加可读性。
ggplot(df, aes(y = value, x = variable, fill = val_cut)) +
geom_col() +
facet_wrap(~year) +
guides(fill=guide_legend(title="Bohicon")) +
scale_fill_manual(values = c( "brown","burlywood","bisque","aliceblue","cadetblue2","blue", "black")) +
labs(x = NULL) +
theme_bw() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "bottom")
创建于2023-06-07,使用 reprex v2.0.2
英文:
I may have missed something but I think you may need another break to get the cuts shown in the table. I've also added another colour in the plot on this account. I've not checked that the cuts and labels match.
df$val_cut <- cut(df$value, breaks = c(-Inf, -2, -1.5, -1, 1, 1.5, 2, Inf),
labels = c("<= -2", "-1.99 to -1.5", "-1.49 to -1", " -0.99 to 0.99", "1 to 1.49", "1.5 to 1.99", ">=2"))
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.2.3
ggplot(df, aes(x = variable, y = year, fill = val_cut)) +
geom_raster()+
guides(fill=guide_legend(title="Bohicon"))+
scale_fill_manual(values = c( "brown","burlywood","bisque","aliceblue","cadetblue2","blue", "black"))
<sup>Created on 2023-06-01 with reprex v2.0.2</sup>
In response to OP's later question...
Yes you could present the data in a bar graph although geom_bar
would not be the preferred approach as the data is already summarised.
Here is a possible solution using geom_col
and facet_wrap
just to make the data readable. I've assumed you want to view monthly values by year, but you can adjust the data as suits your requirements.
Also added some formatting to help with legibility.
ggplot(df, aes(y = value, x = variable, fill = val_cut)) +
geom_col()+
facet_wrap(~year) +
guides(fill=guide_legend(title="Bohicon"))+
scale_fill_manual(values = c( "brown","burlywood","bisque","aliceblue","cadetblue2","blue", "black")) +
labs(x = NULL) +
theme_bw() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "bottom")
<!-- -->
<sup>Created on 2023-06-07 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论