简体   繁体   English

带有 ggplot2 和 shiny R 的多个条形图

[英]Multiple bar graph with ggplot2 and shiny R

I'm trying to plot a multiple bar graph for a customer satisfaction data set using Ggplot on R Shiny.我正在尝试使用 R Shiny 上的 Ggplot 对客户满意度数据集进行 plot 的多条形图。 Satisfaction varies from 1 to 5, where 1 being the lowest and 5 being the highest.满意度从 1 到 5 不等,其中 1 为最低,5 为最高。 This is how it looks like: (This is a sample of the original data set)这是它的样子:(这是原始数据集的样本)

Satisfaction_Level <- c(4   ,4  ,5  ,5  ,3  ,4  ,4  ,4  ,4  ,5  ,1  ,4  ,1  ,3  ,4  ,4  ,1  ,4  ,4  ,1  ,4  ,4  ,4  ,4  ,2  ,4  ,2  ,4  ,3  ,1)

Location <- c("C"   ,"C"    ,"C"    ,"B"    ,"C"    ,"C"    ,"C"    ,"A"    ,"B"    ,"C"    ,"C"    ,"C"    ,"C"    ,"B"    ,"C"    ,"C"    ,"C"    ,"B"    ,"C"    ,"B"    ,"C"    ,"C"    ,"C"    ,"B"    ,"B"    ,"B"    ,"B"    ,"B"    ,"B"    ,"B")

With the below code, I was able to generate this graph:使用下面的代码,我能够生成这个图表:

总体满意度条形图

#satisfaction counts
satisCounts <- data.frame(table(mydata[,1]))
colnames(satisCounts) <- c("satisLevel", "satisCount")
satisCounts$perc3 <- as.character(round(100* satisCounts$satisCount / sum(satisCounts$satisCount)), 2)
satisCounts$lab3 <- paste(satisCounts$satisCount, paste("(",satisCounts$perc3,"%)",sep=""),sep=" ")

output$graph3 <- renderPlot({
  satisCounts %>%
    ggplot(aes(x=satisLevel, y=satisCount)) +
    geom_bar(stat = "identity", aes(fill=satisLevel)) +
    labs(y= "Customer count", x="Satisfaction Level") +
    scale_fill_manual(values = c("#de425b", "#f2955a", "#ffe48f", "#a9b957", "#488f31")) +
    labs(fill = "Satisfaction Level") +
    geom_text(aes(label=lab3), vjust=-.5)
})

But what I actully want is to make this a multiple bar graph where all the satisfaction levels broken down by the location.但我真正想要的是制作一个多重条形图,其中所有满意度水平都按位置细分。 Could you please help me fix this?你能帮我解决这个问题吗? Thank you!谢谢!

From your question is is not clear how the final plot should look like.从您的问题来看,尚不清楚最终的 plot 应该是什么样子。 One option would be to use facetting.一种选择是使用刻面。 Another option would be to use a dodged bar chart.另一种选择是使用闪避的条形图。 But the basis idea is to first compue counts by loacation and satisfaction levels.但基本思想是首先按位置和满意度来计算计数。 Have a look at this.看看这个。

library(dplyr)
library(ggplot2)

Satisfaction_Level<-c(4 ,4  ,5  ,5  ,3  ,4  ,4  ,4  ,4  ,5  ,1  ,4  ,1  ,3  ,4  ,4  ,1  ,4  ,4  ,1  ,4  ,4  ,4  ,4  ,2  ,4  ,2  ,4  ,3  ,1)

Location <- c("C"  ,"C"    ,"C"    ,"B"    ,"C"    ,"C"    ,"C"    ,"A"    ,"B"    ,"C"    ,"C"    ,"C"    ,"C"    ,"B"    ,"C"    ,"C"    ,"C"    ,"B"    ,"C"    ,"B"    ,"C"    ,"C"    ,"C"    ,"B"    ,"B"    ,"B"    ,"B"    ,"B"    ,"B"    ,"B")

df <- data.frame(
  satisLevel = Satisfaction_Level,
  Location = Location,
  stringsAsFactors = FALSE
)

#satisfaction counts
# satisCounts <- data.frame(table(mydata[,1]))
# colnames(satisCounts) <- c("satisLevel", "satisCount")
# satisCounts$perc3 <- as.character(round(100* satisCounts$satisCount / sum(satisCounts$satisCount)), 2)
# satisCounts$lab3 <- paste(satisCounts$satisCount, paste("(",satisCounts$perc3,"%)",sep=""),sep=" ")

satisCounts <- df %>% 
  count(Location, satisLevel, name = "satisCount") %>% 
  mutate(satisLevel = factor(satisLevel))

satisCounts$perc3 <- as.character(round(100* satisCounts$satisCount / sum(satisCounts$satisCount)), 2)
satisCounts$lab3 <- paste(satisCounts$satisCount, paste("(",satisCounts$perc3,"%)",sep=""),sep=" ")

# Option 1: Use facetting
satisCounts %>%
  ggplot(aes(x=satisLevel, y=satisCount)) +
  geom_bar(stat = "identity", aes(fill=satisLevel)) +
  labs(y= "Customer count", x="Satisfaction Level") +
  scale_fill_manual(values = c("#de425b", "#f2955a", "#ffe48f", "#a9b957", "#488f31")) +
  labs(fill = "Satisfaction Level") +
  geom_text(aes(label=lab3), vjust=-.5) + 
  facet_wrap(~ Location, ncol = 1)

# Option 2: Dodged bar chart
satisCounts %>%
  ggplot(aes(x=Location, y=satisCount, fill=satisLevel)) +
  geom_bar(stat = "identity", position = position_dodge2(preserve = "single")) +
  labs(y= "Customer count", x="Satisfaction Level") +
  scale_fill_manual(values = c("#de425b", "#f2955a", "#ffe48f", "#a9b957", "#488f31")) +
  labs(fill = "Satisfaction Level") +
  geom_text(aes(label=lab3), vjust=-.5, position = position_dodge2(.9, preserve = "single"))

Created on 2020-04-05 by the reprex package (v0.3.0)reprex package (v0.3.0) 于 2020 年 4 月 5 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM