停止geom_rect()改变geom_bar()的透明度

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

Stop geom_rect() changing the transparency of geom_bar()

问题

我想要条形图的颜色是纯黑色。

英文:

I have a bar plot of a precipitation index by date. I have a background to my plot which I created using geom_rect(). I have the alpha level set to 0.5. However, this also seems to make the geom_bar() more transparent too. I'd like the geom_bar to be a solid colour. How can I do this?

This is my ggplot Code

coloursCategories <- data.frame(ymax=c(3, 1.5, 1, -1, -1.5),
                                ymin=c(1.5, 1, -1, -1.5, -2.5),
                                xmin=as.POSIXct(c("1963-06-01")),
                                xmax=as.POSIXct(c("2022-06-01")),
                                col=c('darkblue', 'lightblue', 'white','orange', 'red'),
                                lab=c('Extreme Wet', 'Moderately Wet', 'Normal', 
                                       'Moderately Dry', 'Extreme Drought'))


spiPlot <- dataToExport %>%
  ggplot(aes(x=date, y=value)) +
  geom_bar(stat="identity", colour='black') +
  labs(x='Year', y='Standardised Precipitation Index') +
  scale_x_datetime(
    breaks=seq(min(dataToExport$date), max(dataToExport$date), 
               by= "10 years"), date_labels="%Y") +
  geom_hline(yintercept=0) +
  geom_rect(data=coloursCategories, aes(x=NULL, y=NULL, 
                                        xmin=xmin, xmax=xmax, 
                                        ymin=ymin, ymax=ymax,
                                        fill=col), alpha=0.5) +
  scale_fill_identity() +
  theme_classic() +
  theme(text=element_text(size=12, family='Calibri'))

spiPlot

Sample data

structure(list(index = structure(c(1963.91666666667, 1964, 1964.08333333333, 
1964.16666666667, 1964.25, 1964.33333333333, 1964.41666666667, 
1964.5, 1964.58333333333, 1964.66666666667, 1964.75, 1964.83333333333, 
1964.91666666667, 1965, 1965.08333333333, 1965.16666666667, 1965.25, 
1965.33333333333, 1965.41666666667, 1965.5, 1965.58333333333, 
1965.66666666667, 1965.75, 1965.83333333333, 1965.91666666667, 
1966, 1966.08333333333, 1966.16666666667, 1966.25, 1966.33333333333, 
1966.41666666667, 1966.5, 1966.58333333333, 1966.66666666667, 
1966.75, 1966.83333333333, 1966.91666666667, 1967, 1967.08333333333, 
1967.16666666667, 1967.25, 1967.33333333333, 1967.41666666667, 
1967.5, 1967.58333333333, 1967.66666666667, 1967.75, 1967.83333333333, 
1967.91666666667, 1968), class = "yearmon"), value = c(-1.4037744637113, 
-1.43012169326955, -1.33231632985487, -1.11953908820054, -1.07638630001352, 
-1.21934995263382, -1.55915959975905, -1.1031408997769, -1.27853923566536, 
-1.74140973123897, -1.64037112791479, -2.17142215986623, -1.60415380368278, 
-1.04055338987302, -1.02370766242183, -0.999449393354976, -0.97840943266104, 
-0.70099314893533, -0.73764467735243, -0.88661942377825, -0.910943353224119, 
0.378543041782439, 0.194100529795042, 0.693483953558181, 1.09405958591777, 
0.69346806275544, 1.31704365003076, 0.952695903857758, 1.27908201975684, 
1.20619356890934, 1.77720645147051, 1.69798678398381, 1.86979557916084, 
1.13181813973286, 1.44359069430781, 1.36711870630766, 1.48348016358573, 
1.43745383041713, 1.25544552615309, 1.30276221896374, 0.641738111618475, 
1.40622131651604, 0.934321395797565, 0.37638421225284, 0.311505772081162, 
0.715369781598238, 1.00653244506059, 1.03582100739796, 0.538295986976076, 
0.91933881954487), date = structure(c(-192067200, -189388800, 
-186710400, -184204800, -181526400, -178934400, -176256000, -173664000, 
-170985600, -168307200, -165715200, -163036800, -160444800, -157766400, 
-155088000, -152668800, -149990400, -147398400, -144720000, -142128000, 
-139449600, -136771200, -134179200, -131500800, -128908800, -126230400, 
-123552000, -121132800, -118454400, -115862400, -113184000, -110592000, 
-107913600, -105235200, -102643200, -99964800, -97372800, -94694400, 
-92016000, -89596800, -86918400, -84326400, -81648000, -79056000, 
-76377600, -73699200, -71107200, -68428800, -65836800, -63158400
), class = c("POSIXct", "POSIXt"), tzone = "")), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"), na.action = structure(1:11, names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11"), class = "omit"))

Current output

I'd like the bars to be solid black.

停止geom_rect()改变geom_bar()的透明度

答案1

得分: 1

你可以交换 geom_rectgeom_bar,使柱形图显示在前面,像这样:

library(ggplot2)
library(dplyr)

spiPlot <- dataToExport %>%
  ggplot(aes(x=date, y=value)) +
  geom_bar(stat="identity", colour='black') +
  geom_rect(data=coloursCategories, aes(x=NULL, y=NULL, 
                                        xmin=xmin, xmax=xmax, 
                                        ymin=ymin, ymax=ymax,
                                        fill=col), alpha=0.5) +
  labs(x='年份', y='标准化降水指数') +
  scale_x_datetime(
    breaks=seq(min(dataToExport$date), max(dataToExport$date), 
               by= "10 years"), date_labels="%Y") +
  geom_hline(yintercept=0) +
  scale_fill_identity() +
  theme_classic() +
  theme(text=element_text(size=12, family='Calibri'))

spiPlot

停止geom_rect()改变geom_bar()的透明度

创建于2023-02-08,使用 reprex v2.0.2

英文:

You could swap the geom_rect and geom_bar to have the bars in front like this:

library(ggplot2)
library(dplyr)

spiPlot &lt;- dataToExport %&gt;%
  ggplot(aes(x=date, y=value)) +
  geom_rect(data=coloursCategories, aes(x=NULL, y=NULL, 
                                        xmin=xmin, xmax=xmax, 
                                        ymin=ymin, ymax=ymax,
                                        fill=col), alpha=0.5) +
  geom_bar(stat=&quot;identity&quot;, colour=&#39;black&#39;) +
  labs(x=&#39;Year&#39;, y=&#39;Standardised Precipitation Index&#39;) +
  scale_x_datetime(
    breaks=seq(min(dataToExport$date), max(dataToExport$date), 
               by= &quot;10 years&quot;), date_labels=&quot;%Y&quot;) +
  geom_hline(yintercept=0) +
  scale_fill_identity() +
  theme_classic() +
  theme(text=element_text(size=12, family=&#39;Calibri&#39;))

spiPlot

停止geom_rect()改变geom_bar()的透明度<!-- -->

<sup>Created on 2023-02-08 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年2月8日 20:06:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385562.html
匿名

发表评论

匿名网友

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

确定