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

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

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

  1. coloursCategories <- data.frame(ymax=c(3, 1.5, 1, -1, -1.5),
  2. ymin=c(1.5, 1, -1, -1.5, -2.5),
  3. xmin=as.POSIXct(c("1963-06-01")),
  4. xmax=as.POSIXct(c("2022-06-01")),
  5. col=c('darkblue', 'lightblue', 'white','orange', 'red'),
  6. lab=c('Extreme Wet', 'Moderately Wet', 'Normal',
  7. 'Moderately Dry', 'Extreme Drought'))
  8. spiPlot <- dataToExport %>%
  9. ggplot(aes(x=date, y=value)) +
  10. geom_bar(stat="identity", colour='black') +
  11. labs(x='Year', y='Standardised Precipitation Index') +
  12. scale_x_datetime(
  13. breaks=seq(min(dataToExport$date), max(dataToExport$date),
  14. by= "10 years"), date_labels="%Y") +
  15. geom_hline(yintercept=0) +
  16. geom_rect(data=coloursCategories, aes(x=NULL, y=NULL,
  17. xmin=xmin, xmax=xmax,
  18. ymin=ymin, ymax=ymax,
  19. fill=col), alpha=0.5) +
  20. scale_fill_identity() +
  21. theme_classic() +
  22. theme(text=element_text(size=12, family='Calibri'))
  23. spiPlot

Sample data

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

  1. library(ggplot2)
  2. library(dplyr)
  3. spiPlot <- dataToExport %>%
  4. ggplot(aes(x=date, y=value)) +
  5. geom_bar(stat="identity", colour='black') +
  6. geom_rect(data=coloursCategories, aes(x=NULL, y=NULL,
  7. xmin=xmin, xmax=xmax,
  8. ymin=ymin, ymax=ymax,
  9. fill=col), alpha=0.5) +
  10. labs(x='年份', y='标准化降水指数') +
  11. scale_x_datetime(
  12. breaks=seq(min(dataToExport$date), max(dataToExport$date),
  13. by= "10 years"), date_labels="%Y") +
  14. geom_hline(yintercept=0) +
  15. scale_fill_identity() +
  16. theme_classic() +
  17. theme(text=element_text(size=12, family='Calibri'))
  18. 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:

  1. library(ggplot2)
  2. library(dplyr)
  3. spiPlot &lt;- dataToExport %&gt;%
  4. ggplot(aes(x=date, y=value)) +
  5. geom_rect(data=coloursCategories, aes(x=NULL, y=NULL,
  6. xmin=xmin, xmax=xmax,
  7. ymin=ymin, ymax=ymax,
  8. fill=col), alpha=0.5) +
  9. geom_bar(stat=&quot;identity&quot;, colour=&#39;black&#39;) +
  10. labs(x=&#39;Year&#39;, y=&#39;Standardised Precipitation Index&#39;) +
  11. scale_x_datetime(
  12. breaks=seq(min(dataToExport$date), max(dataToExport$date),
  13. by= &quot;10 years&quot;), date_labels=&quot;%Y&quot;) +
  14. geom_hline(yintercept=0) +
  15. scale_fill_identity() +
  16. theme_classic() +
  17. theme(text=element_text(size=12, family=&#39;Calibri&#39;))
  18. 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:

确定