geom_raster基于特定的离散值着色

huangapple go评论88阅读模式
英文:

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:
geom_raster基于特定的离散值着色

I tried the following code (variable = SPI) but it didn't work:

scale_fill_gradient(colours = c( &quot;brown&quot;,&quot;burlywood&quot;,&quot;bisque&quot;,&quot;aliceblue&quot;,&quot;cadetblue2&quot;,&quot;blue&quot;), 
                      breaks=c(-2,-1.5,-1,1,1.5,2), labels = format(c(&quot;-2&quot;,&quot;-1.5&quot;,&quot;-1&quot;,&quot;1&quot;,&quot;1.5&quot;,&quot;2&quot;)))

Example:

year &lt;- 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 &lt;- reshape2::melt(df, id.vars = &quot;year&quot;)

df$year &lt;- factor(df$year, levels = (unique(df$year)))
df$variable &lt;- factor(df$variable, levels = (unique(df$variable)))

library(ggplot2)
e1 &lt;- ggplot(df, aes(x = variable, y = year, fill = value)) +
  geom_raster()+
  guides(fill=guide_legend(title=&quot;Bohicon&quot;))+
  scale_fill_gradient(colours = c( &quot;brown&quot;,&quot;burlywood&quot;,&quot;bisque&quot;,&quot;aliceblue&quot;,&quot;cadetblue2&quot;,&quot;blue&quot;), 
                      breaks=c(-2,-1.5,-1,1,1.5,2), labels = format(c(&quot;-2&quot;,&quot;-1.5&quot;,&quot;-1&quot;,&quot;1&quot;,&quot;1.5&quot;,&quot;2&quot;)))+
  theme(legend.position=&quot;bottom&quot;)

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")

绘图:

geom_raster基于特定的离散值着色

英文:

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 &lt;- 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=&quot;Bohicon:&quot;)) +
  scale_fill_manual(values  =c( &quot;brown&quot;,&quot;burlywood&quot;,&quot;bisque&quot;,&quot;aliceblue&quot;,&quot;cadetblue2&quot;,&quot;blue&quot;),
                    labels = format(c(&quot;-2&quot;,&quot;-1.5&quot;,&quot;-1&quot;,&quot;1&quot;,&quot;1.5&quot;,&quot;2&quot;))) + 
  theme(legend.position=&quot;bottom&quot;)

Plot:

geom_raster基于特定的离散值着色

答案2

得分: 1

我可能漏掉了一些内容,但我认为您可能需要另一个断点来获得表格中显示的切割。我还在此帐户中的绘图中添加了另一种颜色。我没有检查切割和标签是否匹配。

df$val_cut &lt;- cut(df$value, breaks = c(-Inf, -2, -1.5, -1, 1, 1.5, 2, Inf), 
                  labels = c(&quot;&lt;= -2&quot;, &quot;-1.99 to -1.5&quot;, &quot;-1.49 to -1&quot;, &quot; -0.99 to 0.99&quot;, &quot;1 to 1.49&quot;, &quot;1.5 to 1.99&quot;, &quot;&gt;=2&quot;))

library(ggplot2)
#&gt; 警告:包 &#39;ggplot2&#39; 是在 R 版本 4.2.3 下构建的
ggplot(df, aes(x = variable, y = year, fill = val_cut)) +
  geom_raster() +
  guides(fill=guide_legend(title=&quot;Bohicon&quot;)) +
  scale_fill_manual(values = c( &quot;brown&quot;,&quot;burlywood&quot;,&quot;bisque&quot;,&quot;aliceblue&quot;,&quot;cadetblue2&quot;,&quot;blue&quot;, &quot;black&quot;)) 

geom_raster基于特定的离散值着色

创建于2023-06-01,使用 reprex v2.0.2

对于后来的问题...
是的,您可以以条形图的方式呈现数据,尽管 geom_bar 不是首选的方法,因为数据已经被汇总。以下是一种可能的解决方案,使用 geom_colfacet_wrap 使数据更易阅读。我假设您想按年查看每月的值,但您可以根据您的需求调整数据。

还添加了一些格式以增加可读性。

ggplot(df, aes(y = value, x = variable, fill = val_cut)) +
  geom_col() +
  facet_wrap(~year) +
  guides(fill=guide_legend(title=&quot;Bohicon&quot;)) +
  scale_fill_manual(values = c( &quot;brown&quot;,&quot;burlywood&quot;,&quot;bisque&quot;,&quot;aliceblue&quot;,&quot;cadetblue2&quot;,&quot;blue&quot;, &quot;black&quot;)) +
  labs(x = NULL) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90),
        legend.position = &quot;bottom&quot;)

geom_raster基于特定的离散值着色

创建于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 &lt;- cut(df$value, breaks = c(-Inf, -2, -1.5, -1, 1, 1.5, 2, Inf), 
                  labels = c(&quot;&lt;= -2&quot;, &quot;-1.99 to -1.5&quot;, &quot;-1.49 to -1&quot;, &quot; -0.99 to 0.99&quot;, &quot;1 to 1.49&quot;, &quot;1.5 to 1.99&quot;, &quot;&gt;=2&quot;))

library(ggplot2)
#&gt; Warning: package &#39;ggplot2&#39; 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=&quot;Bohicon&quot;))+
  scale_fill_manual(values = c( &quot;brown&quot;,&quot;burlywood&quot;,&quot;bisque&quot;,&quot;aliceblue&quot;,&quot;cadetblue2&quot;,&quot;blue&quot;, &quot;black&quot;)) 

geom_raster基于特定的离散值着色

<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=&quot;Bohicon&quot;))+
  scale_fill_manual(values = c( &quot;brown&quot;,&quot;burlywood&quot;,&quot;bisque&quot;,&quot;aliceblue&quot;,&quot;cadetblue2&quot;,&quot;blue&quot;, &quot;black&quot;)) +
  labs(x = NULL) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90),
        legend.position = &quot;bottom&quot;)

geom_raster基于特定的离散值着色<!-- -->

<sup>Created on 2023-06-07 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月1日 20:49:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382042.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定