I have been trying to make a stacked barplot on a set of thematic variables:
我一直试图在一组主题变量上制作堆积的条形图:
Theme 1 <- c(0,1,1,0,0,1,2,2,1,0)
Theme 2 <- c(0,1,0,1,0,1,2,2,0,1)
Theme 3 <- c(2,2,0,1,0,1,0,1,1,1)
Theme 3 <- c(0,0,0,1,1,1,1,2,2,0)
where: 0 = No, 1 = Yes and 2 = Missing
其中:0 =否,1 =是,2 =缺失
I want to make a stacked plot of these themes are against different regions in my dataframe, so I can see which regions focus on what themes.
我想在这些数据框架中对这些主题的不同区域进行叠加绘图,因此我可以看到哪些区域专注于哪些主题。
list of regions:
地区列表:
Region_abb <- c(ESA, WCA, MENA, ASIA)
I have tried making a matrix:
我试过制作一个矩阵:
Themes_matrix <- matrix(c(Theme 1), nrow = length(Theme 1))
(Themes_matrix_app <- cbind(Themes_matrix, Theme 2, Theme 3, Theme 4))
(## But now I am missing Theme 1 as name in my matrix!, how do I get it back?)
(##但是现在我在主题1中缺少主题1作为名称!我该如何取回它?)
barplot(Themes_matrix_app, main = "Theme Overview",
xlab = "Themes",
col=c("darkblue","orange", "red"), #representing the different levels for my theme variables
legend = rownames(Themes_matrix_app))
(##But this doesn't plot the different levels (0,1,2) and everything is in black!)
(##但这不会绘制不同的级别(0,1,2),一切都是黑色的!)
So I tried:
所以我尝试过:
p2 <- ggplot (data = MY_df, aes(Themes_matrix_app, value = Themes_matrix_app, fill = Region_abb))+
geom_bar(stat="identity")
(## This just flat out didn't work)
(##这只是扁平的不起作用)
My goal is reaching what was portrayed on: How to plot an histogram in R with several variables?
我的目标是达到所描绘的内容:如何在R中绘制具有多个变量的直方图?
I have tried coping the coding from the first example, but I just didn't get it.
我试过应对第一个例子中的编码,但我只是没有得到它。
I hope that my fairly long question makes sense and that someone can help me move on from here.
我希望我相当长的问题是有道理的,有人可以帮我从这里继续前进。
Thank you
谢谢
2 个解决方案
#1
1
Here is an answer using ggplot2. First to use ggplot your data needs to bee in tidy format (https://cran.r-project.org/web/packages/tidyr/vignettes/tidy-data.html). So using the data you provided:
这是使用ggplot2的答案。首先使用ggplot你的数据需要整齐的格式(https://cran.r-project.org/web/packages/tidyr/vignettes/tidy-data.html)。所以使用您提供的数据:
library(tidyverse)
df <- data.frame(Q = 1:10, Theme1 = c(0,1,1,0,0,1,2,2,1,0),
Theme2 = c(0,1,0,1,0,1,2,2,0,1),
Theme3 = c(2,2,0,1,0,1,0,1,1,1),
Theme4 = c(0,0,0,1,1,1,1,2,2,0))
tidy_df <- df %>%
gather(key = "Theme", value = "Answer", 2:5)
Then plotting the data using ggplot:
然后使用ggplot绘制数据:
ggplot(data = tidy_df, aes(x = Theme , fill = as.factor(Answer)))+
geom_bar()
#2
#1
1
Here is an answer using ggplot2. First to use ggplot your data needs to bee in tidy format (https://cran.r-project.org/web/packages/tidyr/vignettes/tidy-data.html). So using the data you provided:
这是使用ggplot2的答案。首先使用ggplot你的数据需要整齐的格式(https://cran.r-project.org/web/packages/tidyr/vignettes/tidy-data.html)。所以使用您提供的数据:
library(tidyverse)
df <- data.frame(Q = 1:10, Theme1 = c(0,1,1,0,0,1,2,2,1,0),
Theme2 = c(0,1,0,1,0,1,2,2,0,1),
Theme3 = c(2,2,0,1,0,1,0,1,1,1),
Theme4 = c(0,0,0,1,1,1,1,2,2,0))
tidy_df <- df %>%
gather(key = "Theme", value = "Answer", 2:5)
Then plotting the data using ggplot:
然后使用ggplot绘制数据:
ggplot(data = tidy_df, aes(x = Theme , fill = as.factor(Answer)))+
geom_bar()