使用单个函数制作表格并绘制比例

时间:2019-06-27 19:06:53

标签: r function plot nested

我希望使用单个函数来生成以下数据集的比例数据框并创建结果的条形图:

# Data
Id <- c(1,2,3,4,5,6,7,8,9,10)
Type <- c("Beginner", "Expert", "Intermediate", "Beginner", 
  "Professional", "Expert", "Intermediate", "Professional", 
  "Professional", "Expert")
Response<- c(1,1,2,2,1,2,1,2,1,1)
Successful <- data.frame(Id, Type, Response)
Successful

# Successful
Id  Type             Response    
1   Beginner         1
2   Expert           1
3   Intermediate     2
4   Beginner         2
5   Professional     1
6   Expert           2
7   Intermediate     1
8   Professional     2
9   Professional     1
10  Expert           1

# Function 1: creating a proportional data frame
StoreDF <- function(dataset, variable1, variable2){
as.data.frame(round(100* prop.table(table(dataset[[variable1]], 
                    dataset[[variable2]]),2), 1))
}

DFRespType <- StoreDF(Successful, "Response", "Type")
DFRespType

# Function 2: plotting the results
PropCompareBarPlot <- function(data, plottitle, xtitle){
   ggplot(data, aes(x=Var2, y=Freq, fill= Var1)) +
   geom_col(aes(fill=Var1), colour="Black") +
   ggtitle(plottitle) + 
   theme(plot.title = element_text(hjust=0.5)) +
   theme(legend.title = element_blank()) +
   xlab(xtitle) + ylab("Proportion") +
   scale_fill_manual(values = c("red", "green")) +
}

PropCompareBarPlot(DFRespType, "Responses Provided vs type of applicant", 
"Type/Level of training")

有什么方法可以将这两个功能合而为一吗?

非常感谢您提供的任何帮助

2 个答案:

答案 0 :(得分:0)

只需将参数组合在一起,然后将 dataset data 对象合并为新的中间对象 plot_data

# FUNCTION
PropCompareBarPlot <- function(dataset, variable1, variable2, plottitle, xtitle){

       plot_data <- as.data.frame(round(100 * prop.table(table(dataset[[variable1]], 
                                                            dataset[[variable2]])
                                                         , 2)
                                        , 1)
                                  )

       ggplot(plot_data, aes(x=Var2, y=Freq, fill= Var1)) +
          geom_col(aes(fill=Var1), colour="Black") +
          ggtitle(plottitle) + 
          theme(plot.title = element_text(hjust=0.5)) +
          theme(legend.title = element_blank()) +
          xlab(xtitle) + ylab("Proportion") +
          scale_fill_manual(values = c("red", "green")) +
}

# FUNCTION CALL BY NAME OR POSITION
PropCompareBarPlot(dataset = Successful, 
                   variable1 = "Response", 
                   variable2 = "Type",
                   plottitle = "Responses Provided vs type of applicant", 
                   xtitle = "Type/Level of training")

PropCompareBarPlot(Successful, "Response", "Type",
                   "Responses Provided vs type of applicant", "Type/Level of training")

答案 1 :(得分:0)

另一种方法是直接将ggplot功能与scale软件包一起使用。无需使用传递函数:

library(scales)
library(ggplot2)

plottitle = "Responses Provided vs type of applicant"
xtitle = "Type/Level of training"
ggplot(Successful, aes(x=factor(Type),fill=factor(Response)))+
  geom_bar(position = "fill", colour = "black") +
  scale_fill_manual(values = c("red", "green")) + ggtitle(plottitle) + 
  theme(plot.title = element_text(hjust=0.5)) +
  theme(legend.title = element_blank()) +
  xlab(xtitle) + ylab("Proportion") +
  scale_y_continuous(labels = percent)

它为您提供这种类型的结果:

enter image description here

我希望对您有帮助