我在这里尝试了堆叠条形图的建议,但似乎无法使它工作。
我试图绘制的数据:
Technology TodayScenario WindScenario BiomassScenario生物质……
要实现此目的,您需要首先重构数据,以便有三列: Technology , Scenario 和 Percentage 。就像是:
Technology
Scenario
Percentage
Technology Scenario Percentage Biomass TodayScenario 0.130 Fossil gas TodayScenario 0.0965 Biomass WindScenario 0.0646 Fossil gas WindScenario 0.00309 Biomass BiomassScenario 0.182 Fossil gas BiomassScenario 0
您可以使用 tidyr::gather() 为了那个原因。
tidyr::gather()
library(tidyr) my_df %>% gather(Scenario, Percentage, c(TodayScenario, WindScenario, BiomassScenario)) %>% ggplot(aes(Scenario, Percentage, fill = Technology)) + geom_bar(stat = 'identity')
切换 x 和 y 并关闭支架 ggplot() :
x
y
ggplot()
ggplot(Distribution, aes(x = Technology, y = WindScenario)) + geom_bar(stat = "identity")
如果你想要垂直轴上的条形,你可以添加 coord_flip() 到了情节
coord_flip()
ggplot(my_df, aes(x = Technology, y = WindScenario)) + geom_bar(stat = "identity", position = "stack")
为了获得包含数据的堆积条形图,您需要将数据组织成三列 - 技术,值和方案 - 就像这样(您可以将其复制并粘贴到控制台中):
structure(list(Technology = structure(c(1L, 1L, 1L, 3L, 3L, 3L, 2L, 2L, 2L, 5L, 5L, 5L, 7L, 7L, 7L, 8L, 8L, 8L, 6L, 6L, 6L, 4L, 4L, 4L), .Label = c("Biomass", "Coal", "Fossil gas", "Offshore", "Oil", "Onshore", "PV", "Waste"), class = "factor"), Values = c(0.13, 0.0646, 0.182, 0.0965, 0.00309, 0, 0.218, 0, 0, 0.00696, 0, 0, 0.0328, 0.0245, 0.0266, 0.042, 0, 0, 0.323, 0.311, 0.337, 0.15, 0.597, 0.454), Scenarios = structure(c(2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L), .Label = c("Biomass Scenario", "Today Scenario", "Wind Scenario" ), class = "factor")), class = "data.frame", row.names = c(NA, -24L))
按要素排序方案数据:
df$Scenarios <- factor(df$Scenarios,levels = c("Today Scenario", "Wind Scenario", "Biomass Scenario"))
正确排列后,您现在可以在ggplot2中输出以下堆积条形图:
library(ggplot2) ggplot(df, aes(fill=Technology, y=Values, x=Scenarios)) + geom_bar( stat="identity")