1)
library(ggplot2)
library(scales)
ggplot(Data, aes(as.factor(Horizon), Response,fill= Model)) +
geom_bar( stat="identity", width = 0.7, position = position_dodge(width = 0.8)) +
theme(plot.title = element_text(size = 10, face = "bold", lineheight=1,hjust = 0), axis.text.x = element_text( size = rel(1.1), angle = 10),legend.position = "bottom",legend.title = element_blank()) + scale_y_continuous(labels = percent_format()) +
labs(
x = "Horizon"
#y = "Percentages",
#title = gg_title,
#subtitle = gg_title_subtitle
#caption = "Data from fueleconomy.gov"
)
Данные
Input = ("
Horizon Variable Response Shock Country Model
1 GDP 0.000000000 PCOM Brazil 'Model 1'
2 GDP 0.404381850 PCOM Brazil 'Model 1'
3 GDP 0.401069156 PCOM Brazil 'Model 1'
4 GDP 0.368749090 PCOM Brazil 'Model 1'
5 GDP 0.351268777 PCOM Brazil 'Model 1'
6 GDP 0.345947281 PCOM Brazil 'Model 1'
7 GDP 0.347482783 PCOM Brazil 'Model 1'
8 GDP 0.352164160 PCOM Brazil 'Model 1'
9 GDP 0.357781202 PCOM Brazil 'Model 1'
10 GDP 0.363198705 PCOM Brazil 'Model 1'
11 GDP 0.367974083 PCOM Brazil 'Model 1'
12 GDP 0.372078699 PCOM Brazil 'Model 1'
13 GDP 0.375666736 PCOM Brazil 'Model 1'
14 GDP 0.378901315 PCOM Brazil 'Model 1'
15 GDP 0.381878427 PCOM Brazil 'Model 1'
16 GDP 0.384630719 PCOM Brazil 'Model 1'
1 GDP 0.000000000 PCOM Brazil 'Model 2'
2 GDP 0.301533139 PCOM Brazil 'Model 2'
3 GDP 0.308349733 PCOM Brazil 'Model 2'
4 GDP 0.263588570 PCOM Brazil 'Model 2'
5 GDP 0.239982463 PCOM Brazil 'Model 2'
6 GDP 0.235266964 PCOM Brazil 'Model 2'
7 GDP 0.240041605 PCOM Brazil 'Model 2'
8 GDP 0.248219530 PCOM Brazil 'Model 2'
9 GDP 0.256646193 PCOM Brazil 'Model 2'
10 GDP 0.263902054 PCOM Brazil 'Model 2'
11 GDP 0.269612632 PCOM Brazil 'Model 2'
12 GDP 0.273995159 PCOM Brazil 'Model 2'
13 GDP 0.277464105 PCOM Brazil 'Model 2'
14 GDP 0.280368261 PCOM Brazil 'Model 2'
15 GDP 0.282903588 PCOM Brazil 'Model 2'
16 GDP 0.285144263 PCOM Brazil 'Model 2'
1 GDP 0.000000000 PCOM Brazil 'Model 3'
2 GDP 0.034171019 PCOM Brazil 'Model 3'
3 GDP 0.024779691 PCOM Brazil 'Model 3'
4 GDP 0.016802809 PCOM Brazil 'Model 3'
5 GDP 0.011206834 PCOM Brazil 'Model 3'
6 GDP 0.009575322 PCOM Brazil 'Model 3'
7 GDP 0.008935842 PCOM Brazil 'Model 3'
8 GDP 0.008605141 PCOM Brazil 'Model 3'
9 GDP 0.008182777 PCOM Brazil 'Model 3'
10 GDP 0.007498230 PCOM Brazil 'Model 3'
11 GDP 0.006684634 PCOM Brazil 'Model 3'
12 GDP 0.005917865 PCOM Brazil 'Model 3'
13 GDP 0.005320365 PCOM Brazil 'Model 3'
14 GDP 0.004940644 PCOM Brazil 'Model 3'
15 GDP 0.004782973 PCOM Brazil 'Model 3'
16 GDP 0.004831577 PCOM Brazil 'Model 3'
")
Data = read.table(textConnection(Input),header=TRUE)
2)
ggplot(Data,aes(Model, Response, fill=Shock)) +
geom_bar( stat = "identity", position = "stack") +
facet_grid(~ Horizon, scales = "free_x", space = "free_x") +
theme_bw() +
theme(panel.spacing = unit(0,"lines"),
strip.background = element_blank(),plot.title = element_text(size = 10, face = "bold", lineheight=1,hjust = 0), axis.text.x = element_text( size = rel(1.1), angle = 90),legend.position = "bottom") + scale_y_continuous(labels = percent_format())
Данные 2
#Using dput(Data)
Data <- structure(list(Horizon = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L,
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or"),
Response = c(0, 0.40438185, 0.401069156, 0.36874909, 0.351268777,
0.345947281, 0.347482783, 0.35216416, 0.357781202, 0.363198705,
0.367974083, 0.372078699, 0.375666736, 0.378901315, 0.381878427,
0.384630719, 0, 0.301533139, 0.308349733, 0.26358857, 0.239982463,
0.235266964, 0.240041605, 0.24821953, 0.256646193, 0.263902054,
0.269612632, 0.273995159, 0.277464105, 0.280368261, 0.282903588,
0.285144263, 0, 0.034171019, 0.024779691, 0.016802809, 0.011206834,
0.009575322, 0.008935842, 0.008605141, 0.008182777, 0.00749823,
0.006684634, 0.005917865, 0.005320365, 0.004940644, 0.004782973,
0.004831577, 0.1, 0.50438185, 0.501069156, 0.46874909, 0.451268777,
0.445947281, 0.447482783, 0.45216416, 0.457781202, 0.463198705,
0.467974083, 0.472078699, 0.475666736, 0.478901315, 0.481878427,
0.484630719, 0.1, 0.401533139, 0.408349733, 0.36358857, 0.339982463,
0.335266964, 0.340041605, 0.34821953, 0.356646193, 0.363902054,
0.369612632, 0.373995159, 0.377464105, 0.380368261, 0.382903588,
0.385144263, 0.1, 0.134171019, 0.124779691, 0.116802809,
0.111206834, 0.109575322, 0.108935842, 0.108605141, 0.108182777,
0.10749823, 0.106684634, 0.105917865, 0.105320365, 0.104940644,
0.104782973, 0.104831577, 0.2, 0.60438185, 0.601069156, 0.56874909,
0.551268777, 0.545947281, 0.547482783, 0.55216416, 0.557781202,
0.563198705, 0.567974083, 0.572078699, 0.575666736, 0.578901315,
0.581878427, 0.584630719, 0.2, 0.501533139, 0.508349733,
0.46358857, 0.439982463, 0.435266964, 0.440041605, 0.44821953,
0.456646193, 0.463902054, 0.469612632, 0.473995159, 0.477464105,
0.480368261, 0.482903588, 0.485144263, 0.2, 0.234171019,
0.224779691, 0.216802809, 0.211206834, 0.209575322, 0.208935842,
0.208605141, 0.208182777, 0.20749823, 0.206684634, 0.205917865,
0.205320365, 0.204940644, 0.204782973, 0.204831577), Shock = structure(c(3L,
3L, 3L, 3L, 3L, 3L,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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Brazil", class = "factor"),
Model = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Model 1",
"Model 2", "Model 3"), class = "factor")), .Names = c("Horizon",
"Variable", "Response", "Shock", "Country", "Model"),
row.names = c(NA,-144L), class = "data.frame")
Для получения дополнительной информации о маркировке двух переменных в оси X, проверьте здесь . Я не определил switch = x
в facet_grid
, так как метка оси X будет ниже переменной фасета, как показано здесь , что, я считаю, не круто.