我正在尝试制作一个条形图,以可视化方式显示多个列中的值,但我对如何执行操作有些迷失。
我的专栏是:
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)
我尝试了以下
BNG_final %>%
ggplot(aes(Theme_1, Theme_2, Theme_3, Theme_4)) +
geom_histogram()
尝试1-这不起作用-因为主题不在x轴上对齐
(注意,我有19个观察结果)
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")
尝试2-我确实设置正确,但是主题显示为4个深色的列,并且没有描绘每个主题的累积值。
我希望每个主题都有一个栏,以累积总价值。
我希望有人可以帮助我。
谢谢!
答案 0 :(得分:1)
您可以尝试使用group_by
和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")
或者简单地使用
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")