Arrange a two variable Bar Plot in Descending Orde

2019-07-16 14:50发布

I am trying to plot a data frame with two variables by descending order. Both variables are factors. I want to consider the frequency of both variables when plotting, just like a pivot table in excel.

I tried to use tidy to group, count, and sort order of the variables by descending order.

library(tidyverse)

# Create a data frame that simulates the data that needs to be modeled

#Create data frame that will hold data for simulation
df1 = as.data.frame(replicate(2, 
                              sample(c("A", "B", "C", "D", "E","F","G","H","I","J"), 
                                     50, 
                                     rep=TRUE)))

#Replace V2 column with System Nomenclature (Simulated)
df1$V2 <- sample(1:4, replace = TRUE, nrow(df1))

#Make V2 into a Factor
df1$V2 = as.factor(df1$V2)

#Create frequency table
df2 <- df1 %>% 
  group_by(V1, V2) %>%
  summarise(counts = n()) %>%
  ungroup() %>%
  arrange(desc(counts))

#Plot the 2 variable data
ggplot(df2, 
       aes(reorder(x = V1, -counts) , 
           y = counts, 
           fill = V2)) +
 geom_bar(stat = "identity")

I expect to the graph to plot the data in descending order by the frequency of V1 but with the fill of V2. Just like the pivot table feature in excel. I also want to only display the Top-5 by frequency of V1 and fill with V2.

enter image description here

标签: r ggplot2
1条回答
不美不萌又怎样
2楼-- · 2019-07-16 15:08

You can use fct_reorder and fct_rev to achieve what you want

#Create data frame that will hold data for simulation
df1 = as.data.frame(replicate(2, sample(c("A", "B", "C", "D", "E","F","G","H","I","J"), 50, rep=TRUE)))

#Replace V2 column with System Nomenclature (Simulated)
df1$V2 <- sample(1:4, replace = TRUE, nrow(df1))

#Make V2 into a Factor
df1$V2 = as.factor(df1$V2)

#Create frequency table
df2 <- df1 %>% group_by(V1, V2) %>%
    summarise(counts = n()) %>%
    ungroup() %>%
    arrange(desc(counts))

#Plot the 2 variable data.
##fct_reorder rearranges the factors, and fct_rev reverses the order, so it is descending[![enter image description here][1]][1]
ggplot(df2, aes(fct_rev(fct_reorder(V1, counts,fun = sum)) , y = counts, fill = V2)) +
    geom_bar(stat = "identity")

enter image description here

##Keeping only top 5
df2 %>% group_by(V1) %>%
filter(sum(counts) > 5) %>%
ggplot(aes(x = fct_rev(fct_reorder(V1,
                    counts,fun = sum)),
            y = counts, fill = V2)) +
geom_bar(stat = "identity")

enter image description here

查看更多
登录 后发表回答