多个变量的条形图
Barchart for multiple variables
[在此处输入图片描述][1]
我有一个 table 来自问卷调查,其中 390 人回答 activity 的可能性为不太可能、可能或非常可能。 table 看起来如下(总共 390 行)
Anniversary
Babyshower
Engagement
Memorial
Unlikely
Likely
Very likely
Unlikely
Likely
Unlikely
Likely
Very likely
Very likely
Very likely
Unlikely
Likely
我想要的是每列的条形图,由不太可能、可能和非常可能的出现分隔。我可以为一列绘制条形图,但是我想要一个图表,我可以在同一张图中看到每个单独的列。有没有办法在 ggplot 中做到这一点?
非常感谢你的帮助。
谢谢。
library(ggplot2)
#creating example dataset
dfx <- data.frame(
Anniversary = c(sample(c("likely", "unlikely", "very_likely"), 20, replace = TRUE)),
Annual_album = c(sample(c("likely", "unlikely", "very_likely"), 20, replace = TRUE)),
Babyshower = c(sample(c("likely", "unlikely", "very_likely"), 20, replace = TRUE)),
Childhoodmemories = c(sample(c("likely", "unlikely", "very_likely"), 20, replace = TRUE)),
stringsAsFactors = FALSE
)
#creating a function to transform any dataset to a grouped dataset based on dataset variables(here activities)
grouped_df <- function(dataset){
#creating a dataframe with each response count per activities
df_table <- as.data.frame(apply(dataset,2, table))
# creating a vector containing activities
activity <- rep(colnames(df_table), each = nrow(df_table))
# creating a vector containing responses(likely, unlikely, very_likely)
response <- rep(rownames(df_table), times = length(colnames(df_table)))
# creating a vector containing number of responses per activities(vote_num)
vote_num <- NULL
for (i in seq_along(colnames(df_table))){
name <- paste0(colnames(df_table)[i], "_num")
name <- assign(name, df_table[,i])
vote_num <- append(vote_num, name)
}
# creating final dataframe with 3 columns(activities, responses, vote_num)
df_final <- data.frame(
activity = activity,
response = response,
vote_num = vote_num,
stringsAsFactors = FALSE
)
return(df_final) #retuning dataframe
}
my_dataset <- grouped_df(dfx) #assigning the function to a variable
# creating ggplot of grouped barchart
ggplot(my_dataset, aes(fill = response, y = vote_num, x = activity)) +
geom_bar(position = "dodge", stat = "identity")
它创建了一个分组条形图,如下所示:
你的意思是这样的吗?
@Falimus 在创建图后的问题的响应中,显示的是 (1,2,3) 而不是标签:
上面的代码在我的计算机上使用不同的数据类型工作,但我写了下面的代码,也许它解决了@Falimus 的遗留问题:
#In the ggplot part(last line), write this code instead
ggplot(my_dataset, aes(fill = response, y = vote_num, x = activity)) +
geom_bar(position = "dodge", stat = "identity") + scale_fill_discrete(labels=c("likely", "unlikely", "very_likely")) #the orders is from above the legend
您在找这样的东西吗?
df<-data.frame(Anniversary = c("Unlikely", "Likely", "Very Likely"),
Babyshower = c("Likely", "Unlikely", "Very Likely"),
Engagement = c("Very Likely", "Likely", "Unlikely"))
df %>%
pivot_longer(., cols = c("Anniversary", "Babyshower", "Engagement")) %>%
group_by(name, value) %>%
summarise(total = n()) %>%
ggplot(.) +
geom_col(
mapping = aes(x = name, y = total, fill = value), width = 0.5,
position = position_dodge(width = 0.6))
[在此处输入图片描述][1]
我有一个 table 来自问卷调查,其中 390 人回答 activity 的可能性为不太可能、可能或非常可能。 table 看起来如下(总共 390 行)
Anniversary | Babyshower | Engagement | Memorial |
---|---|---|---|
Unlikely | Likely | Very likely | Unlikely |
Likely | Unlikely | Likely | Very likely |
Very likely | Very likely | Unlikely | Likely |
我想要的是每列的条形图,由不太可能、可能和非常可能的出现分隔。我可以为一列绘制条形图,但是我想要一个图表,我可以在同一张图中看到每个单独的列。有没有办法在 ggplot 中做到这一点?
非常感谢你的帮助。
谢谢。
library(ggplot2)
#creating example dataset
dfx <- data.frame(
Anniversary = c(sample(c("likely", "unlikely", "very_likely"), 20, replace = TRUE)),
Annual_album = c(sample(c("likely", "unlikely", "very_likely"), 20, replace = TRUE)),
Babyshower = c(sample(c("likely", "unlikely", "very_likely"), 20, replace = TRUE)),
Childhoodmemories = c(sample(c("likely", "unlikely", "very_likely"), 20, replace = TRUE)),
stringsAsFactors = FALSE
)
#creating a function to transform any dataset to a grouped dataset based on dataset variables(here activities)
grouped_df <- function(dataset){
#creating a dataframe with each response count per activities
df_table <- as.data.frame(apply(dataset,2, table))
# creating a vector containing activities
activity <- rep(colnames(df_table), each = nrow(df_table))
# creating a vector containing responses(likely, unlikely, very_likely)
response <- rep(rownames(df_table), times = length(colnames(df_table)))
# creating a vector containing number of responses per activities(vote_num)
vote_num <- NULL
for (i in seq_along(colnames(df_table))){
name <- paste0(colnames(df_table)[i], "_num")
name <- assign(name, df_table[,i])
vote_num <- append(vote_num, name)
}
# creating final dataframe with 3 columns(activities, responses, vote_num)
df_final <- data.frame(
activity = activity,
response = response,
vote_num = vote_num,
stringsAsFactors = FALSE
)
return(df_final) #retuning dataframe
}
my_dataset <- grouped_df(dfx) #assigning the function to a variable
# creating ggplot of grouped barchart
ggplot(my_dataset, aes(fill = response, y = vote_num, x = activity)) +
geom_bar(position = "dodge", stat = "identity")
它创建了一个分组条形图,如下所示:
你的意思是这样的吗?
@Falimus 在创建图后的问题的响应中,显示的是 (1,2,3) 而不是标签:
上面的代码在我的计算机上使用不同的数据类型工作,但我写了下面的代码,也许它解决了@Falimus 的遗留问题:
#In the ggplot part(last line), write this code instead
ggplot(my_dataset, aes(fill = response, y = vote_num, x = activity)) +
geom_bar(position = "dodge", stat = "identity") + scale_fill_discrete(labels=c("likely", "unlikely", "very_likely")) #the orders is from above the legend
您在找这样的东西吗?
df<-data.frame(Anniversary = c("Unlikely", "Likely", "Very Likely"),
Babyshower = c("Likely", "Unlikely", "Very Likely"),
Engagement = c("Very Likely", "Likely", "Unlikely"))
df %>%
pivot_longer(., cols = c("Anniversary", "Babyshower", "Engagement")) %>%
group_by(name, value) %>%
summarise(total = n()) %>%
ggplot(.) +
geom_col(
mapping = aes(x = name, y = total, fill = value), width = 0.5,
position = position_dodge(width = 0.6))