I am trying to make a bar chart that visualizes the values across several columns and I am a bit lost on how to do it.
My columns are:
Theme_1 <- c(0,15023,15614,0,0,145618,204656,2065,11564,0)
Theme_2 <- c(0,10264,0,10485,0,15460,20000,2000,0,15640)
Theme_3 <- c(2564,26904,0,16048,0,1980,0,10564,1894,19614)
Theme_4 <- c(0,0,0,10189,1105,1361,1780,20458,2000,0)
I have tried the following
BNG_final %>% ggplot(aes(Theme_1, Theme_2, Theme_3, Theme_4)) + geom_histogram()
Attempt 1- This did not work - as the themes are not lined up on the x-axis
(NB. I have 19 observations)
Theme_df <- data.frame(Q=1:19, CP=c(BNG_final$Theme_1), CRG=c(BNG_final$Theme_2), Edu = c(BNG_final$Theme_3), Health = c(BNG_final$Theme_4)) Theme_graph_1 <- Theme_df %>% gather(key = "Themes", value = "Level") ggplot(data = Theme_graph_1, aes(x = Themes))+ geom_bar() + ggtitle("Thematic spread")
Attempt 2- I did get the right set up, but the themes are visualized as 4 dark columns and does not portray the accumulated values for each theme.
I want a bar for each theme, which accumulates the total value.
I hope that someone can help me with this.
Thank you!!
You can try to calculate the sums beforehand using group_by
and summarise
library(tidyverse)
tibble(Theme_1, Theme_2, Theme_3, Theme_4) %>%
gather(k, v) %>%
group_by(k) %>%
summarise(Sum=sum(v)) %>%
ggplot(aes(k, Sum, fill=k)) +
geom_col(position = "dodge")
Or simply use
tibble(Theme_1, Theme_2, Theme_3, Theme_4) %>%
gather(k, v) %>%
ggplot(aes(k, v, fill=k)) +
stat_summary(fun.y="sum", geom="bar")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.