Hello everyone,
am trying to plot below dataset in ggplot. As you can see I have three different variables. I am plotting TOTAL_REQ,TOTAL_RECD as bar plot (position="dodge") and TOTAL as a line chart (all in one chart) But due to some reason all of them are plotting as bar and I can't pick up or separate variables for plot type. Request to please suggest
REQ_YEAR REQ_MONTH VARIABLE VALUE
1 2019 1 TOTAL_REQ 20
2 2019 2 TOTAL_REQ 16
3 2019 3 TOTAL_REQ 31
4 2019 4 TOTAL_RECD 35
5 2019 5 TOTAL_RECD 36
5 2019 6 TOTAL 36
5 2019 7 TOTAL 36
5 2019 7 TOTAL 36
Since I do not have your data, I invented a somewhat simpler data set to make the graph. Is this the sort of thing you are looking for?
library(ggplot2)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
DF <- data.frame(TIME = rep(seq(1:4), each = 3),
Variable = rep(c("Req", "Recv", "Total"), 4),
Value = c(12, 10, 18, 9, 10, 14, 18, 20, 23, 4, 2, 8))
DF
#> TIME Variable Value
#> 1 1 Req 12
#> 2 1 Recv 10
#> 3 1 Total 18
#> 4 2 Req 9
#> 5 2 Recv 10
#> 6 2 Total 14
#> 7 3 Req 18
#> 8 3 Recv 20
#> 9 3 Total 23
#> 10 4 Req 4
#> 11 4 Recv 2
#> 12 4 Total 8
ReqAndRecv <- DF %>% filter(Variable %in% c("Req", "Recv"))
Total <- DF %>% filter(Variable == "Total")
ggplot() + geom_col(aes(TIME, Value, fill = Variable), data = ReqAndRecv,
position = "dodge") +
geom_line(aes(TIME, Value, color = Variable), data = Total)
Created on 2019-09-16 by the reprex package (v0.2.1)
1 Like
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.