使用重新调整的数据可能会更容易绘制您想要绘制的图。考虑每年一排及以下/以上,而不是每年一排。
# Setup Data
require(tidyverse)
releaseDate <- 2014:2021
belowRetail <- c(24.20635, 25.09804, 35.63403, 31.06996, 27.76025, 28.59097, 31.00559, 30.89888)
overRetail <- c(75.79365, 74.90196, 64.36597, 68.93004, 72.23975, 71.40903, 68.99441, 69.10112)
retail <- tibble(releaseDate = releaseDate, belowRetail = belowRetail, overRetail = overRetail)
您可以使用 dplyr 中的 pivot_longer 来重塑数据。
retail <- pivot_longer(data = retail, cols = -releaseDate, names_to = "name")
然后,您可以使用 geom_bar,在美学 (aes) 中指定名称。另请注意,必须添加 position = "fill" 和 stat = "identity"。第一个选项使所有条形图为 100%,第二个选项使用数据值而不是默认计数。
ggplot(data = retail) +
geom_bar(aes(x = releaseDate, y = value, fill = name), position = "fill", stat = "identity")
这是它的样子。
Here is a useful source that you might want to consult.